0

我正在使用微软的 sql 服务器。我正在尝试按全职排序我的结果。

我的原始数据如下所示:

TimeStamp                           TotalOffered
2012-04-16 08:00:00                 18
2012-04-16 08:30:00                 34
2012-04-16 09:00:00                 30
2012-04-16 09:30:00                 68

我按小时块排序,例如 08:00:00 期间的数据总和为 52,09:00:00 的数据总和为 98,我将其按天细分。

我的代码是:

select datepart(hour,[TimeStamp]), SUM([TotalOffered])
from [my table]
group by 
datepart(hour,[sus_CallPerformance_TimeStamp]), 
dateadd(d, 0, datediff(d, 0, [sus_CallPerformance_TimeStamp]))

我试图让数据显示完整的时间戳而不是小时。

目前结果显示为:

8   52
9   98

我希望结果显示为:

2012-04-16 08:00:00   52
2012-04-16 09:00:00   98

谢谢你

4

3 回答 3

2
SELECT  DATEADD(HOUR, DATEPART(HOUR, TimeStamp), DATEDIFF(DAY, 0, TimeStamp)) [TimeStamp],
        SUM(TotalOffered) [TotalOffered]
FROM    [My Table]
GROUP BY DATEADD(HOUR, DATEPART(HOUR, TimeStamp), DATEDIFF(DAY, 0, TimeStamp))
ORDER BY [TimeStamp]
于 2012-04-18T12:57:34.257 回答
-1

试试这个:

declare @t table (ts datetime, od int)
insert into @t (ts, od) values ('2012-04-16 08:00:00', 18)
insert into @t (ts, od) values ('2012-04-16 08:30:00', 34)
insert into @t (ts, od) values ('2012-04-16 09:00:00', 30)
insert into @t (ts, od) values ('2012-04-16 09:30:00', 68)

select * from @t


select 
    dateadd(hour, datepart(hour, ts), cast(floor(cast(ts as real)) as datetime)) as solution,
    sum(od)
    from @t
    group by dateadd(hour, datepart(hour, ts), cast(floor(cast(ts as real)) as datetime))
    order by dateadd(hour, datepart(hour, ts), cast(floor(cast(ts as real)) as datetime))
于 2012-04-18T13:06:55.717 回答
-1

DateTime 字段可以转换为浮点值,其中整数表示日期,小数表示时间。如果将日期乘以 24,则整数将代表小时,如下所示:

SELECT CAST(FLOOR(CAST(TimeStamp AS FLOAT) * 24)/24.0 AS DateTime) AS Hr, SUM(TotalOffered) AS Total
FROM [my table]
GROUP BY FLOOR(CAST(TimeStamp AS FLOAT) * 24)
ORDER BY FLOOR(CAST(TimeStamp AS FLOAT) * 24)
于 2012-04-18T13:12:09.887 回答