Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I am building a chat site and for presenting the chats to the audiences, I have 3 Dropdownlists - Sports (Default is All Sports), Day/Month/Year, Users online/Total Users.

Now, if by default all sports is selected and I pick 1 month and Total users, the expected result should be

My query is

SQL
SELECT DISTINCT roo.[Sports], 
                roo.[Name], 
                COUNT(DISTINCT chu.ChatUserLogId) AS TotalUsers, 
                COUNT(DISTINCT liu.[LoggedInUserID]) AS UserOnline

 FROM Room AS roo 

 LEFT JOIN LoggedInUser AS liu ON roo.RoomID = liu.RoomID 
 LEFT JOIN ChatUserLog AS chu ON roo.RoomID = chu.RoomID 
               AND chu.LoggedInTime >= DATEADD(DAY,-30,GETDATE()) 

 GROUP BY roo.[Sports], roo.[Name]   
 ORDER BY TotalUsers DESC


One person suggested that with my method I am actually multiplying the row because of the two joins & so I need to aggregate first, then join.

So in the end, I tried this query too

with agg_ChatUserLog as (select RoomId, count(*) as cnt_user_tot from ChatUserLog WHERE LoggedInTime >= DATEADD(DAY,-30,GETDATE()) group by RoomId),                                            

       agg_LoggedInUser as (select RoomId, count(*) as cnt_user_logged from LoggedInUser group by RoomId)

select Sports, Name, cnt_user_tot, cnt_user_logged from Room r 

       left outer join agg_ChatUserLog acu on acu.RoomId = r.RoomId 

       left outer join agg_LoggedInUser alu on alu.RoomId = r.RoomId;


But this is also multiplying the results.

Where am I making the mistake in both the query?

Thanks in advance and have a nice day ahead.

What I have tried:

I guess, the issue is with "GROUP BY" items too... So, I am trying to modify and try them out too in

with agg_ChatUserLog as (select RoomId, count(*) as cnt_user_tot from ChatUserLog WHERE LoggedInTime >= DATEADD(DAY,-30,GETDATE()) group by RoomId), agg_LoggedInUser as (select RoomId, count(*) as cnt_user_logged from LoggedInUser group by RoomId) select Sports, Name, cnt_user_tot, cnt_user_logged from Room r left outer join agg_ChatUserLog acu on acu.RoomId = r.RoomId left outer join agg_LoggedInUser alu on alu.RoomId = r.RoomId GROUP BY cnt_user_tot ORDER BY cnt_user_tot DESC
Posted
Updated 24-Oct-16 19:42pm
v3

1 solution

It's probably your GROUP BY clause - it creates groups based on both of the columns separately, not as a combination.
So what it does is crazy a set of groups based on the first column, and then separate them based on the second column.
This may help to explain what it does: SQL GROUP By and the "Column 'name' is invalid in the select list because..." error[^]
I can't suggest a solution directly because I'm not sure exactly what you are inputting and expecting to receive without actual examples of input and output data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900