Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL如何对两个日期之间一小时内的事件求和并在一行中显示_C#_Sql_Sql Server 2005_View_Count - Fatal编程技术网

C# SQL如何对两个日期之间一小时内的事件求和并在一行中显示

C# SQL如何对两个日期之间一小时内的事件求和并在一行中显示,c#,sql,sql-server-2005,view,count,C#,Sql,Sql Server 2005,View,Count,我正在用C和SQL server 2005开发一个报告, 我只需要展示一下我们每小时的命中率。这张桌子很大。 输出应如下所示: Row# | Date | Time | Hit Count ----------------------------- 1 | 07/05/2012 | 8:00 | 3 2 | 07/05/2012 | 9:00 | 4 3 | 07/05/2012 | 10:00 | 0 4 | 07/05/2012

我正在用C和SQL server 2005开发一个报告, 我只需要展示一下我们每小时的命中率。这张桌子很大。 输出应如下所示:

Row# | Date | Time | Hit Count ----------------------------- 1 | 07/05/2012 | 8:00 | 3 2 | 07/05/2012 | 9:00 | 4 3 | 07/05/2012 | 10:00 | 0 4 | 07/05/2012 | 11:00 | 5 "HitTime": 07/05/2012 08:02:24 07/05/2012 08:12:21 07/05/2012 08:23:00 07/05/2012 09:01:00 07/05/2012 09:08:14 07/05/2012 09:12:31 07/05/2012 09:22:27 我的桌子是这样的:

Row# | Date | Time | Hit Count ----------------------------- 1 | 07/05/2012 | 8:00 | 3 2 | 07/05/2012 | 9:00 | 4 3 | 07/05/2012 | 10:00 | 0 4 | 07/05/2012 | 11:00 | 5 "HitTime": 07/05/2012 08:02:24 07/05/2012 08:12:21 07/05/2012 08:23:00 07/05/2012 09:01:00 07/05/2012 09:08:14 07/05/2012 09:12:31 07/05/2012 09:22:27 等等 正如你在HitTime字段中看到的,我只有日期和时间,我需要显示在同一日期,例如从8:00到8:59,我得到了多少点击,应该是一整天,从那天开始的第一秒到一天结束的第二秒。

这个怎么样

DECLARE @current_date DATETIME

SET @current_date = '2012-05-07';

WITH    hours (hr) AS
        (
        SELECT  0
        UNION ALL
        SELECT  hr + 1
        FROM    hours
        WHERE   hr < 23
        )
SELECT  ROW_NUMBER() OVER (ORDER BY hr) AS rn,
        @current_date AS [date],
        CONVERT(VARCHAR(5), DATEADD(hour, h.hr, @current_date), 108) AS [time],
        COUNT(hs.hittime) AS hitcount
FROM    hours h
LEFT JOIN
        hits hs
ON      hs.hittime >= DATEADD(hour, h.hr, @current_date)
        AND hs.hittime < DATEADD(hour, h.hr + 1, @current_date)
GROUP BY
        hr
;WITH aggregation(hit_date, hit_time)
AS
(
SELECT DATEADD(dd, DATEDIFF(dd, 0, hittime), 0)
     , DATEPART(hour, hittime)
FROM test
)
SELECT ROW_NUMBER() OVER (ORDER BY hit_date, hit_time) AS rn
     , CONVERT(VARCHAR,hit_date,101) as [date]
     , CAST(hit_time AS VARCHAR) + ':00' as [time]
     , COUNT(*) as hit_count
FROM aggregation
  GROUP BY hit_date
         , hit_time
如果您想要上午/下午,那么:

WITH hit_count AS(
select CONVERT(VARCHAR,hit_time,101)as [date], (substring(convert(varchar,hit_time,100), 12, 4)+'00'+substring(convert(varchar,hit_time,100),18,2)) as [time] from hit
)
select date,[time], count(*) as hit from hit_count group by [time],[date]
GO

可能与+1重复,但OP使用的是SQL Server 2005,它不允许使用日期类型变量,您不能在同一语句中声明和设置它。@KM:看来AJAX轮询时间太慢了,所以:完全正确。错过了10点的那排。以及样本数据下面的大部分文本。谢谢Manish,但我猜结果不正确,我的意思是命中计数器不正确。太多了。请给我解释一下这个问题,例如什么是101?2我如何将时间显示为PM/AM,或者我甚至可以使用24小时。@Pouria我已经编辑了我的答案,现在您将获得所需的输出,并以24小时格式显示时间,在查询中谈论101或108这些是日期时间格式。也许这个链接对你有用。