I have a table T
like this :
ID | brand
2 | 100
2 | 300
2 | 300
1 | 100
1 | 200
I want to find which ID,brand combination occur more than 50% for an ID. For ex : here the result should come as
ID | brand
2 | 300
as (2,300) occurs 2 out of 3 times in ID 2. In ID1, both combinations are 50% times so no row for ID 1.
Now I approached it like this:
First I found count of each ID,brand combination :
(select id,brand,count(*) c from T group by id,brand) as F
Then I list all IDs which satisfy above mentioned criteria
select ID from F group by ID having max(c)/count(*) > 0.5
It works perfectly, but the problem is I can't get corresponding brand because when in above query, I give
select ID, brand ...
Then it always gives first brand of the table.
Here is the sql fiddle