0

I have got two different tables

  • dogs (id , name)

  • dogs_pref (id_dog, preference(only one) )

  • preferences (one dog can have 0 , 1 , 2 preferences in text format)

The task is creating two different requests

1st : Create the request which showed dogs with preferences (one or more) 2nd : Create the request which showed quantity of preferences per dog

1st:

SELECT dogs.Name, dogs_pref.id_dog, dogs_pref.preference
FROM dogs
LEFT JOIN dogs_pref
ON dogs.Id=dogs_pref.id_dog
where dogs_pref.preference >= 1
ORDER BY dogs.Id; 

2nd:

Here is the big question how to write it in simplest way ????

user2451093
  • 9
  • 1
  • 3

1 Answers1

0
SELECT dogs.id, sum(to_number(dogs_pref.preference))
FROM dogs
LEFT JOIN dogs_pref
ON dogs.Id=dogs_pref.id_dog
group by dogs.id;
Olesya Razuvayevskaya
  • 1,148
  • 1
  • 7
  • 10