1. <small id='kVADd'></small><noframes id='kVADd'>

      <tfoot id='kVADd'></tfoot>
        <i id='kVADd'><tr id='kVADd'><dt id='kVADd'><q id='kVADd'><span id='kVADd'><b id='kVADd'><form id='kVADd'><ins id='kVADd'></ins><ul id='kVADd'></ul><sub id='kVADd'></sub></form><legend id='kVADd'></legend><bdo id='kVADd'><pre id='kVADd'><center id='kVADd'></center></pre></bdo></b><th id='kVADd'></th></span></q></dt></tr></i><div id='kVADd'><tfoot id='kVADd'></tfoot><dl id='kVADd'><fieldset id='kVADd'></fieldset></dl></div>
        <legend id='kVADd'><style id='kVADd'><dir id='kVADd'><q id='kVADd'></q></dir></style></legend>
          <bdo id='kVADd'></bdo><ul id='kVADd'></ul>

      1. 计算2个日期之间每个日期的记录数

        Calculate the number of records for each date between 2 dates(计算2个日期之间每个日期的记录数)
          <tbody id='88rVY'></tbody>

          <i id='88rVY'><tr id='88rVY'><dt id='88rVY'><q id='88rVY'><span id='88rVY'><b id='88rVY'><form id='88rVY'><ins id='88rVY'></ins><ul id='88rVY'></ul><sub id='88rVY'></sub></form><legend id='88rVY'></legend><bdo id='88rVY'><pre id='88rVY'><center id='88rVY'></center></pre></bdo></b><th id='88rVY'></th></span></q></dt></tr></i><div id='88rVY'><tfoot id='88rVY'></tfoot><dl id='88rVY'><fieldset id='88rVY'></fieldset></dl></div>
        1. <legend id='88rVY'><style id='88rVY'><dir id='88rVY'><q id='88rVY'></q></dir></style></legend>

          <small id='88rVY'></small><noframes id='88rVY'>

            <bdo id='88rVY'></bdo><ul id='88rVY'></ul>

              <tfoot id='88rVY'></tfoot>
                • 本文介绍了计算2个日期之间每个日期的记录数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我必须创建一个查询来返回多轴图表的结果.我需要计算为 2 个日期之间的每个日期创建的 Id 数量.我试过这个:

                  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
                  

                  当然,问题在于返回错误的 ;WITH

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

                  操作数类型冲突:datetime2 与 int 不兼容.

                  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()
                   )
                  

                  哪个返回:

                  问题是我不知道如何替换日期范围.

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

                  推荐答案

                  无需重新发明轮子 - 有许多递归 CTE 日历表的示例,类似于以下内容.

                  No need to reinvent the wheel - there are many examples of recursive CTE calendar tables out there, similar to below.

                  DECLARE @StartDate date = '01-Nov-2020', @EndDate date = '22-Feb-2021';
                  
                  WITH Date_Range_T (d_range) AS (
                      SELECT @StartDate AS d_range
                      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 R ON R.CreatedDate = d_range
                  GROUP BY d_range
                  ORDER BY d_range ASC
                  -- Set to the max number of days you require
                  OPTION (MAXRECURSION 366);
                  

                  评论:

                  • 为什么使用 datetime2 作为 date?
                  • 你确定要<结束日期还是<=?
                  • 您是否熟悉 between 的工作原理 - 它并不总是很直观.
                  • 为所有表格设置别名以提高可读性.
                  • 分号终止所有语句.
                  • 一致的大小写使查询更易于阅读.
                  • 对日期字符串使用明确的日期格式.
                  • Why use a datetime2 for a date?
                  • Do you definitely want < the end date or <=?
                  • Are you familiar with how between works - its not always intuitive.
                  • Alias all tables for better readability.
                  • Semi-colon terminate all statements.
                  • Consistent casing makes the query easier to read.
                  • Use an unambiguous date format for date strings.

                  这篇关于计算2个日期之间每个日期的记录数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)

                  <small id='dXypY'></small><noframes id='dXypY'>

                  • <i id='dXypY'><tr id='dXypY'><dt id='dXypY'><q id='dXypY'><span id='dXypY'><b id='dXypY'><form id='dXypY'><ins id='dXypY'></ins><ul id='dXypY'></ul><sub id='dXypY'></sub></form><legend id='dXypY'></legend><bdo id='dXypY'><pre id='dXypY'><center id='dXypY'></center></pre></bdo></b><th id='dXypY'></th></span></q></dt></tr></i><div id='dXypY'><tfoot id='dXypY'></tfoot><dl id='dXypY'><fieldset id='dXypY'></fieldset></dl></div>
                      <tbody id='dXypY'></tbody>
                        <bdo id='dXypY'></bdo><ul id='dXypY'></ul>
                          • <legend id='dXypY'><style id='dXypY'><dir id='dXypY'><q id='dXypY'></q></dir></style></legend>

                            <tfoot id='dXypY'></tfoot>