I have a table representing a Ternary relation between Offers, Profiles, and Skills.
Any given Offer can have more than one profile, and more than 1 skill associated.
Something like this:
ternary_relationship table
id_Offer - id_Profile - id_Skill
1 - 1 - 1
1 - 1 - 2
1 - 1 - 3
1 - 2 - 1
2 - 1 - 1
2 - 1 - 2
2 - 1 - 3
2 - 2 - 1
Offer table
Offer - business_name
1 - business-1
1 - business-1
1 - business-1
1 - business-1
2 - business-2
2 - business-2
2 - business-2
2 - business-2
I want to make a query filtering by a profile and only count Offer once, no matter how many skills it has associated.
I was doing something like the following query:
SELECT business_name, COUNT(*)
FROM Offer INNER JOIN
ternary_relationship
ON Offer.id_Offer = ternary_relationship.id_Offer AND
id_Profile = '1'
GROUP BY business_name
ORDER BY COUNT(*) DESC;
I have seen several possible solutions but I am not being able to make anyone work for my case. Neither when I group by for both name an id_Offer nor when I only filter by id_Offer it works, either. I always get duplicated entries somewhere.