Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Individual sports ---- 30 day ----- Online users

SQL
SELECT RoomId, Name, ISNULL((SELECT COUNT(ChatUserLogId) From ChatUserLog WHERE RoomId = Room.RoomId AND LoggedInTime >= DATEADD(DAY,-30,GETDATE())), 0) TotalUsers, ISNULL((SELECT COUNT(LoggedInUserId) From LoggedInUser WHERE RoomID = Room.RoomID), 0) UserOnline From Room Where Sports = @Sports ORDER BY UserOnline DESC";


Individual sports ---- 30 day ----- Total users

SQL
SELECT RoomId, Name, ISNULL((SELECT COUNT(ChatUserLogId) From ChatUserLog WHERE RoomId = Room.RoomId AND LoggedInTime >= DATEADD(DAY,-30,GETDATE())), 0) TotalUsers, ISNULL((SELECT COUNT(LoggedInUserId) From LoggedInUser WHERE RoomID = Room.RoomID), 0) UserOnline From Room Where Sports = @Sports ORDER BY TotalUsers DESC";


What changes do I have to make to the query for getting results of all sports and the top 3 alone in each of the sport?

The Table details are

SQL
CREATE TABLE [dbo].[Room] (
        [RoomID]        INT           IDENTITY (6666, 1) NOT NULL,
        [Name]          VARCHAR (100) NOT NULL,
        [Sports]        VARCHAR (50)  NOT NULL,
        [CreatedDate]   DATETIME      DEFAULT (getdate()) NOT NULL,
        [CreatedBy]     VARCHAR (50)  NOT NULL,
        [CreatedUserID] INT           NOT NULL,
        PRIMARY KEY CLUSTERED ([RoomID] ASC)
    );
    
    CREATE TABLE [dbo].[ChatUserLog] (
        [ChatUserLogId] BIGINT   IDENTITY (1, 1) NOT NULL,
        [MId]           INT      NOT NULL,
        [RoomId]        INT      NOT NULL,
        [LoggedInTime]  DATETIME DEFAULT (getdate()) NOT NULL,
        [LogOutTime]    DATETIME DEFAULT (getdate()) NULL,
        PRIMARY KEY CLUSTERED ([ChatUserLogId] ASC)
    );
     
    CREATE TABLE [dbo].[LoggedInUser] (
        [LoggedInUserID] INT           IDENTITY (1, 1) NOT NULL,
        [MId]            INT           NOT NULL,
        [RoomID]         INT           NOT NULL,
        [NickName]       VARCHAR (50)  NOT NULL,
        [Icon]           VARCHAR (MAX) NOT NULL,
        CONSTRAINT [PK_LoggedInUser] PRIMARY KEY CLUSTERED ([LoggedInUserID] ASC)
    );


What I have tried:

I tried 2 left joins but it just multiplies the 3 tables and gives the same results coming up n number of times. So, it doesn't work. Help me to just modify this
Posted
Updated 8-Nov-16 22:56pm
v3
Comments
Herman<T>.Instance 9-Nov-16 4:37am    
Are you aware of the Row_Number() function?

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