计算所有日期(不是特定日期)的发生次数

时间:2016-12-02 21:04:46

标签: sql-server datetime count

我有这段代码可以按小时计算一组事件的发生次数。我将它设置为计算locate_received_date为2016-12-01的所有小时的计数(注意:locate_received_date数据类型为DATETIME,YYYY-MM-DD hh:mm:ss格式)。我手动输入日期以计算前一天的日期。代码摘录如下:

SELECT CAST(locate_received_date as date) AS 'ForDate', 
       DATEPART(hh, locate_received_date) AS 'OnHour', 
       COUNT (*) AS 'HourCount'
FROM   MyTable
WHERE  locate_received_date BETWEEN '2016-12-01 00:00:00.000' AND '2016-12-01 23:59:59.999'
GROUP BY CAST(locate_received_date as date), DATEPART(hh, locate_received_date);

现在,表“MyTable”在许多不同的日期都有数百行的数据。我需要为所有不同日期运行计数,而不仅仅是手动输入特定日期。我以为我可以用某种方式嵌套以下内容:

DISTINCT CAST(locate_received_date AS DATE)

或者也许使用某种FOR循环来做这件事?

编辑: 这是创建表的脚本。确实没有任何其他专栏值得创建。表中没有ID或任何内容。该表正在更新,每次更新时,locate_received_date都会更新,并显示日期和时间。我需要先根据小时数(我的脚本所做的)计算,也要根据天数计算。

 CREATE Table myTable(
   Locate_Received_Date date); 


INSERT INTO myTable(Locate_Received_Date) VALUES ('2016-12-01 15:14:07.000');
INSERT INTO myTable(Locate_Received_Date) VALUES ('2016-12-01 15:13:37.000');
INSERT INTO myTable(Locate_Received_Date) VALUES ('2016-12-02 15:13:37.000');
INSERT INTO myTable(Locate_Received_Date) VALUES ('2016-12-01 15:13:07.000');
INSERT INTO myTable(Locate_Received_Date) VALUES ('2016-12-02 15:12:08.000');
INSERT INTO myTable(Locate_Received_Date) VALUES ('2016-12-02 15:12:07.000');
INSERT INTO myTable(Locate_Received_Date) VALUES ('2016-11-28 15:11:37.000');
INSERT INTO myTable(Locate_Received_Date) VALUES ('2016-12-02 15:11:08.000');
INSERT INTO myTable(Locate_Received_Date) VALUES ('2016-12-02 15:11:07.000');
INSERT INTO myTable(Locate_Received_Date) VALUES ('2016-11-28 15:10:37.000');
INSERT INTO myTable(Locate_Received_Date) VALUES ('2016-11-29 15:10:08.000');

任何帮助将不胜感激。感谢

3 个答案:

答案 0 :(得分:0)

您是否需要计算特定日期的小时步数?如果是这样,那么这里是合成的例子:

with a as (select convert(datetime,'2016-12-01 00:01:00.000',120) s 
        union all
        select convert(datetime,'2016-12-01 01:02:02.000',120) s 
        union all
        select convert(datetime,'2016-12-01 00:03:03.000',120) s 
        union all
        select convert(datetime,'2016-12-01 03:04:04.000',120) s 
        union all
        select convert(datetime,'2016-12-01 00:05:05.000',120) s 
        union all
        select convert(datetime,'2016-12-01 06:00:00.010',120) s )
        ,
    b as(select convert(varchar(10), s,120) s
        , a.s dt
        , DATEPART(hh,s)onhour 
            from a )
select count(*)cnt, s, onhour
  from b
group by s, onhour

应用于您的数据将是:

with a as (select convert(varchar(10), locate_received_date,120)s
            , locate_received_date
            , DATEPART(hh,locate_received_date)onhour 
            from MyTable    
            )
select count(*)cnt, s, onhour
  from a
group by s, onhour

您必须记住的主要内容是MS SQL Server转换代码:https://msdn.microsoft.com/en-us/library/ms187928.aspx

答案 1 :(得分:0)

它只计算一天的原因是where子句指定了一天。如果删除where子句并添加order by,我认为您将完成所需的工作。如果您想要的输出与渲染的输出不同,请告诉我,我可以相应地更新查询:

SELECT CAST(locate_received_date as date) AS 'ForDate', 
       DATEPART(hh, locate_received_date) AS 'OnHour', 
       COUNT (*) AS 'HourCount'
FROM   MyTable
GROUP BY CAST(locate_received_date as date), DATEPART(hh, locate_received_date)
ORDER BY CAST(locate_received_date as date), DATEPART(hh, locate_received_date);

答案 2 :(得分:0)

您的locate_received_date是日期时间,而不是日期,对吗?如果是这样,那么这样的事情应该有效:

select count(Locate_Received_Date)  from myTable
group by  
convert(varchar(10), cast(Locate_Received_Date as date), 112) + '_' +     
convert(varchar(2), DATEPART(hh, Locate_Received_Date))