2

I am trying to count users created in a month, let’s say January 2015. Then I am trying to count users created as at January 31st, 2015. Then I am trying to count distinct user IDs that logged in during a month, which would also be January 2015. Then I am trying to count the total logins belonging to the distinct user IDs that logged in during January 2015.

The data is stored in 2 tables – users and auditlogins. The primary key for both the users and auditlogins table is userid.

If I wrote each query separately, I would have the following queries:

SELECT 'Jan 2015' AS 'Month',
COUNT(users.userid) AS 'Total at month end' 
FROM users
WHERE users.datecreated < ('2015-02-01 00:00:00.000') 

SELECT 'Jan 2015' AS 'Month',
COUNT(users.userid)  AS 'User IDs created in Month'
FROM users
WHERE users.datecreated > ('2015-01-01 00:00:00.000') AND users.datecreated < ('2015-02-01 00:00:00.000') 

SELECT 'Jan 2015' AS 'Month',       
COUNT(DISTINCT auditlogins.userid) AS 'Unique User Logins in Month'
FROM auditlogins
WHERE auditlogins.logintime > ('2015-01-01 00:00:00.000') AND auditlogins.logintime < ('2015-02-01 00:00:00.000')

SELECT 'Jan 2015' AS 'Month',       
COUNT(auditlogins.userid) AS 'Total Logins in Month'
FROM auditlogins
WHERE auditlogins.logintime > ('2015-01-01 00:00:00.000') AND auditlogins.logintime < ('2015-02-01 00:00:00.000')

What I really want is everything in one row. My final output would look like this:

Month Total at month end User IDs created in Month Unique User Logins in Month Total Logins in Month Jan-15 19,908 1,647 4,127 24,881

How do I do this? I have tried CASE WHEN and joining the tables, but I cannot seem to get a distinct count of userids logging on within a date range.

Your help is greatly appreciated as this is frustrating me to no end!

1 Answer 1

1

Here's a method using COUNT(DISTINCT) and SUM with case statements.

DECLARE @MonthEnd DATE = '1/31/2015', @MonthDesc VARCHAR(50) = 'Jan 2015'
SELECT
    @MonthDesc AS [Month],
    COUNT(DISTINCT CASE WHEN users.datecreated < @MonthEnd THEN users.userid ELSE NULL END) AS [Total at month end],
    COUNT(DISTINCT CASE WHEN MONTH(users.datecreated) = MONTH(@MonthEnd)
        AND YEAR(users.datecreated) = YEAR(@MonthEnd) THEN users.userid ELSE NULL END) AS [User IDs created in Month],
    COUNT(DISTINCT CASE WHEN MONTH(logins.logintime) = MONTH(@MonthEnd)
        AND YEAR(logins.logintime) = YEAR(@MonthEnd) THEN users.userid ELSE NULL END) AS [Unique User Logins in Month],
    SUM(CASE WHEN MONTH(logins.logintime) = MONTH(@MonthEnd)
        AND YEAR(logins.logintime) = YEAR(@MonthEnd) THEN 1 ELSE 0 END) AS [Total Logins in Month]
FROM users users
    LEFT OUTER JOIN auditlogins logins
        ON users.userid = logins.userid

Here is the query applied to sample data:

DECLARE @users TABLE (
    userid INT,
    datecreated DATETIME
)
DECLARE @auditlogins TABLE (
    userid INT,
    logintime DATETIME
)
INSERT @users VALUES (1, '1/1/2015'), (2, '12/1/2014')
INSERT @auditlogins VALUES (1, '1/1/2015'), (1, '1/15/2015'), (1, '2/1/2015'), (2, '1/15/2015')

DECLARE @MonthEnd DATE = '1/31/2015', @MonthDesc VARCHAR(50) = 'Jan 2015'
SELECT
    @MonthDesc AS [Month],
    COUNT(DISTINCT CASE WHEN users.datecreated < @MonthEnd THEN users.userid ELSE NULL END) AS [Total at month end],
    COUNT(DISTINCT CASE WHEN MONTH(users.datecreated) = MONTH(@MonthEnd)
        AND YEAR(users.datecreated) = YEAR(@MonthEnd) THEN users.userid ELSE NULL END) AS [User IDs created in Month],
    COUNT(DISTINCT CASE WHEN MONTH(logins.logintime) = MONTH(@MonthEnd)
        AND YEAR(logins.logintime) = YEAR(@MonthEnd) THEN users.userid ELSE NULL END) AS [Unique User Logins in Month],
    SUM(CASE WHEN MONTH(logins.logintime) = MONTH(@MonthEnd)
        AND YEAR(logins.logintime) = YEAR(@MonthEnd) THEN 1 ELSE 0 END) AS [Total Logins in Month]
FROM @users users
    LEFT OUTER JOIN @auditlogins logins
        ON users.userid = logins.userid

This yields output:

Month      Total at month end User IDs created in Month Unique User Logins in Month Total Logins in Month
---------- ------------------ ------------------------- --------------------------- ---------------------
Jan 2015   2                  1                         2                           3
1
  • Hi Jason - thanks so much! I knew my expected values, so when I read and ran your query, I was thrilled when the numbers matched perfectly. I added a few more conditions for the users counts and all is great. Now I need to apply this new COUNT(DISTINCT CASE knowledge to other areas of the system I am analyzing.
    – Tony
    May 1, 2015 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.