SQL: daily report with user counts

I am trying to get a 30-day user report that will return the date and total number of users as an account created on that date, and I did this with this request

 Select count(*)  As [Count] ,  
        (SELECT CONVERT(date, AddDate)) As [Date] 
 from Users 
 WHERE AddDate  >= (SELECT DateAdd(month, -1, Convert(date, GetDate()))) 
 Group By CONVERT(date, AddDate)

it gives me only the dates on which any user is created, but I want to show all 30 days if it has count 0 .

The same case that I want to do with the monthly report. I get the months in which users are created, now I want to change it to get the last 12 months from this month and their total number of users . For this I use this query

 Select count(*)  As [Count] ,
        (Select DATEPART( month , DateAdd( month , DATEPART(mm,AddDate) , -1 ) )) as Month
 from Users 
 WHERE AddDate  >= (SELECT DateAdd(YEAR, -1, Convert(date, GetDate())))
 Group By DATEPART(mm,AddDate)
+4
5

CTE:

With NumberSequence ( Number ) as
(
    Select 1 as Number
    union all
    Select Number + 1
    from NumberSequence
    where Number <= 30
)
, CalendarCTE as
(
    select cast(dateadd(dd, -30 + Number,getdate()) as Date) as CalDate
    from Numbersequence
)

select CalDate, count(U1.*) as CountUsers
from CalendarCTE
left join Users U1
on CalDate = convert(date, U1.AddDate)
group by CalDate
+3

, Calendar table Left Join

SELECT Count(u.adddate) AS [Count], 
       c.dates  AS [Date] 
FROM   calendar_table C 
       LEFT JOIN users U 
              ON c.dates = CONVERT(DATE, adddate) 
WHERE  c.dates >= Dateadd(month, -1, CONVERT(DATE, Getdate())) 
GROUP  BY c.dates 

/ , .

SQL Server

Calender 100 Sql

+2

script:

WITH CTEDates
AS
(
  SELECT CAST(GetDate() as date) AS [date]
  UNION ALL
  SELECT DATEADD(dd, 1, [date])
  FROM CTEDates
  WHERE DAY([date]) <= 30
)
Select count(*)  As [Count] ,CONVERT(date, AddDate) As [Date] 
from CTEDates 
LEFT JOIN Users ON CTEDates.date=CONVERT(date, AddDate)
WHERE AddDate  >= DateAdd(month, -1, GetDate())
Group By CONVERT(date, AddDate)
+2

CTE 30- . .

DECLARE @CurrentTime DATETIME = GETDATE()
;WITH CTE AS
    (
      SELECT CONVERT(DATE, @CurrentTime) AS [Date]

      UNION ALL

      SELECT  DATEADD(dd, -1, Date)
      FROM    CTE
      WHERE   DATEADD(dd, 29, Date) > @CurrentTime
    )
SELECT COUNT(U.AddDate) AS [Count]
, CTE.[Date] AS [Date]
FROM CTE
LEFT JOIN users U
ON CTE.Date = CONVERT(Date, AddDate)
GROUP BY CTE.Date

CTE , .

.

+1
source
DECLARE @StartDate Datetime
DECLARE @EndDate Datetime

CREATE TABLE #tMyCalanderDate (dtDate Datetime Primary key)

SELECT @StartDate = '01-Sep-2016'
SELECT @EndDate  =  '30-Sep-2016'

WHILE @StartDate <= @EndDate
BEGIN
    INSERT INTO #tMyCalanderDate (dtDate)
    SELECT @StartDate 

    SELECT @StartDate = DATEADD(day,1,@StartDate)

END

SELECT count(A.UserID)  As [Count] , B.dtDate As [Date] 
FROM Users AS A
RIGHT JOIN #tMyCalanderDate AS B ON CONVERT(date, A.AddDate) = CONVERT(date, B.dtDate)  
WHERE CONVERT(date, A.AddDate) BETWEEN @StartDate AND @EndDate
Group By CONVERT(date, B.dtDate)
+1
source

Source: https://habr.com/ru/post/1656679/


All Articles