Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

sql - Calculate the number of records for each date between 2 dates

I have to create a query to return results for a multi-axis chart. I need to count the number of Ids created for each date between 2 dates. I tried this:

 DECLARE @StartDate datetime2(7) = '11/1/2020',
         @EndDate datetime2(7) = '2/22/2021'

 ;WITH Date_Range_T(d_range) AS 
     (
       SELECT DATEADD(DAY, DATEDIFF(DAY, 0, @EndDate) - @StartDate, 0) 
       UNION ALL SELECT DATEADD(DAY, 1, d_range) 
       FROM Date_Range_T 
       WHERE DATEADD(DAY, 1, d_range) < @EndDate
     )

  SELECT d_range, COUNT(Id) as Total 
  FROM Date_Range_T 
       LEFT JOIN [tbl_Support_Requests] on ([tbl_Support_Requests].CreatedDate Between @StartDate AND @EndDate) 
  GROUP BY d_range ORDER BY d_range ASC

Of course, the problem is with the ;WITH which returns the error

Operand type clash: datetime2 is incompatible with int.

The above works if I give it a specific number of days from the current date like:

 ;WITH Date_Range_T(d_range) AS 
 (
   SELECT DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) - 6, 0) 
   UNION ALL SELECT DATEADD(DAY, 1, d_range) 
   FROM Date_Range_T 
   WHERE DATEADD(DAY, 1, d_range) < GETDATE()
 )

Which returns:

enter image description here

The problem is that I cannot figure out how to substitute the date range.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Improving on Dale K's answer, I suggest you use a tally table or function, as this is usually more performant.

I have used Itzik Ben-Gan's well-known one below:

DECLARE @StartDate date = '2020-11-01', @EndDate date = '2021-02-22';

  WITH
    L0 AS ( SELECT 1 AS c 
            FROM (VALUES(1),(1),(1),(1),(1),(1),(1),(1),
                        (1),(1),(1),(1),(1),(1),(1),(1)) AS D(c) ),
    L1 AS ( SELECT 1 AS c FROM L0 AS A CROSS JOIN L0 AS B ),
    L2 AS ( SELECT 1 AS c FROM L1 AS A CROSS JOIN L1 AS B ),
    Nums AS ( SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rownum
              FROM L2 )
    Date_Range_T (d_range) AS (
      SELECT TOP(DATEDIFF(day, @StartDate, @EndDate) + 1)
          DATEADD(day, rownum - 1, @StartDate) AS d_range,
          DATEADD(day, rownum, @StartDate) AS d_rangeNext
      FROM Nums
    )
SELECT d_range, COUNT(Id) AS Total 
FROM Date_Range_T 
LEFT JOIN tbl_Support_Requests R
    ON R.CreatedDate >= T.d_range AND R.CreatedDate < T.d_rangeNext
GROUP BY d_range
ORDER BY d_range ASC

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...