Sql server 计算所有日期(非特定日期)的发生次数

Sql server 计算所有日期(非特定日期)的发生次数,sql-server,datetime,count,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_d

我有一段代码,它每小时统计一组事件的发生次数。我已将其设置为计算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');

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

您是否需要使用小时步长计算特定日期的发生次数?如果是这样,那么下面是一个合成示例:

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转换代码符合以下要求:

它只计算一天的原因是
where
子句指定了一天。如果您删除
where
子句并添加一个
orderby
,我认为您将完成所需的任务。如果您所需的输出与此呈现的不同,请告知我,我可以相应地更新查询:

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

你的定位日期是日期时间,不是日期,对吗?如果是这样的话,那么类似的方法应该会起作用:

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

这是一个很好的起点。我不确定为什么您发布的查询在跨越1个以上日期的日期范围内不起作用。在我看来,这应该适用于任何日期范围。我写了“WHERE locate_received_date介于'2016-12-01 00:00:00.000'和'2016-12-01 23:59:59.999'之间”,只指定了一天。我需要这个命令更加通用,以便它可以在数据库中的所有日期运行。我正在编辑我的问题以添加我的表格删除WHERE子句…嗨,Yuriy!谢谢你的回复。我的代码不起作用,因为我无法将locate_received_date馈送到SUBSTRING参数中。它不带日期数据类型我换了帖子。一般来说,将数据从日期转换为字符串或从字符串转换为日期对哪一方来说并不重要