I need to return the Cartesian product of this query and a new column that calculates the average of the count columns.
select site_topics.description as topic,
site_subscriptions.site_user_id as user1,
count(*) as count1
from (
(site_topics_to_subscriptions
inner join site_subscriptions
on site_subscriptions.id = site_subscription_id)
inner join site_topics
on site_topics.id = site_topics_to_subscriptions.topic_id
)
group by user1,topic
order by user1, count1 desc;
Current results:
-----------------------------------
topic |user1 |count1
-----------------------------------
Gaming |1 |3
Photography |1 |3
Art |1 |1
Gaming |2 |2
Photography |2 |1
Art |2 |1
Desired output
-----------------------------------------------------
topic |user1 |count1 |user2 |count2 |Average
-----------------------------------------------------
Gaming |1 |3 |2 |2 |0.66
Photography |1 |3 |2 |1 |0.33
Art |1 |1 |2 |0 |0
Gaming |2 |2 |1 |3 |0.66
Photography |2 |1 |1 |3 |0.33
'Average' a column that vidies the smaller count by the lager count of that row. Also the same rows are not paired with each other. ie there won't be a row that has user1=1 and user2=1