I am using below query in SQL Server to find distinct
number of logins made in last 7 days(excluding today's date):
SELECT TOP (7) CONVERT(date, LoginTime) AS ActivityDate, COUNT(DISTINCT LoginID) AS UserCount
FROM Login
WHERE CONVERT(date, LoginTime) < CONVERT(date, GETDATE())
GROUP BY CONVERT(date, LoginTime)
ORDER BY ActivityDate DESC;
It generates following output:
ActivityDate | UserCount
----------------------
2019-02-21 | 2
2019-02-20 | 3
2019-02-19 | 2
2019-02-15 | 2
2019-02-14 | 1
2019-02-13 | 2
2019-02-12 | 3
My expectation is to have all last 7 days in a sequence (not like as current output where date 2019-02-16
, 2019-02-17
and 2019-02-18
are missing after 2019-02-19
). I need that, if a date is missing it must be displayed with 0 count.
My expected output is as below:
ActivityDate | UserCount
----------------------
2019-02-21 | 2
2019-02-20 | 3
2019-02-19 | 2
2019-02-18 | 0
2019-02-17 | 0
2019-02-16 | 0
2019-02-15 | 2