Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have coded a chatroom using asp.net, C# & MS SQL. I have two Dropdowns for selecting Sports and then another for Sorting the chatrooms based on Users online/total users.

It should not repeat the sport twice, which has online users.

Thanks in advance and have a nice day ahead.

What I have tried:

I have coded a chatroom using asp.net, C# & MS SQL. I have two Dropdowns for selecting Sports and then another for Sorting the chatrooms based on Users online/total users.

<b>When I select "All sports" (default value) and total users, the query works fine
Posted
Updated 11-Oct-16 18:26pm
v16
Comments
Maciej Los 30-Sep-16 4:08am    
You should post sample data...
Bigprey 30-Sep-16 5:30am    
Now, you could find the data. I have elaborated further details in the question now.
Thanks and have a nice day ahead.
Maciej Los 30-Sep-16 5:36am    
Please, improve your question and get expected output in <pre>...here your data...</pre> tags.
Do not forget to add input data (not expected output) to check what's wrong with your queries.
Have a nice day too ;)
David_Wimbley 30-Sep-16 10:06am    
Without the raw sample data to run your queries against this might be a bit tricky. Something in your data is obviously different and is why the DISTINCT returns it two times.

Can you post schema/sample data...like the insert statements...so we can run your queries against the DB and replicate? Someone else may be able to eyeball it but its just guessing at what your code is doing without that for me.
Bigprey 1-Oct-16 4:45am    
Please check it out now. I have added the Insert queries too and you would find it easy to understand now. Looking forward for your help.

Thanks and have a nice day ahead.

1 solution

I would try something like that:

SQL
SELECT roo.Sports As Sport, roo.[Name] AS ChatRoomName, COUNT(DISTINCT liu.[LoggedInUserID]) AS OnlineUsers, COUNT(DISTINCT chu.ChatUserLogId) AS TotalUsers
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,-1,GETDATE()
GROUP BY roo.Sports, roo.[Name]


For further details, please see:
Visual Representation of SQL Joins[^]
Aggregate Functions (Transact-SQL)[^]
COUNT (Transact-SQL)[^]
 
Share this answer
 
Comments
Bigprey 13-Oct-16 0:28am    
Hi,

I have put this question little bit wrong and since I don't know how to delete it, I have created a new one at http://www.codeproject.com/Questions/1138117/MS-SQL-query-error-in-my-chatroom-code-when-any-us. The query you gave was almost fitting but could you modify it according to the question at this new url

Thanks in advance and have a nice day ahead.

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