如果日期和时间字段的日期条目类似于' 2017-12-04 21:30:00.000'和持续时间是' 1800'和' 2017-12-04 21:30:22.000'已经' 22'然后我们的查询应显示记录为' 2017-12-04 21:30:00.000'作为' 1800'但是应该在2017-12-04 21:30:00.000'但我的查询显示于2017-12-04 21:00:00.000,持续时间为' 1822' (总和1800 + 22)。我有这个问题link以不同的方式,但没有得到正确的答案因此在这里提出这个问题,因为我无法以适当的方式进行修改。
查询:
SELECT Interval=(CASE WHEN datepart(MINUTE,[DateTime]) = 0 and datepart(SECOND,DateTime)=0 THEN
CAST(CONVERT(VARCHAR(30),DATEADD(HOUR,-1,[DateTime]),101) + ' '+ cast(format(DATEPART(HOUR,DATEADD(HOUR,-1,[DateTime])),'0#') as varchar)+':30:00' as DateTime)
ELSE (CAST(CONVERT(VARCHAR(30),[DateTime],101) +' ' + (case when datepart(MINUTE,[DateTime])<=30 and
datepart(SECOND,[DateTime])<59
then cast(format(DATEPART(HOUR,[DateTime]),'0#') as varchar)+':00:00'
else cast(format(DATEPART(HOUR,[DateTime]),'0#') as varchar)+':30:00' end) as DateTime)) END),
ID,Code,Duration=SUM(Duration) FROM Table
WHERE [DateTime] >= '2017-12-04 00:00:00' and [DateTime] <= '2017-12-04 23:59:59'
GROUP BY (CASE WHEN datepart(MINUTE,[DateTime]) = 0 and datepart(SECOND,DateTime)=0 THEN
CAST(CONVERT(VARCHAR(30),DATEADD(HOUR,-1,[DateTime]),101) + ' '+ cast(format(DATEPART(HOUR,DATEADD(HOUR,-1,[DateTime])),'0#') as varchar)+':30:00' as DateTime)
ELSE (CAST(CONVERT(VARCHAR(30),[DateTime],101) +' ' + (case when datepart(MINUTE,[DateTime])<=30 and
datepart(SECOND,[DateTime])<59 then cast(format(DATEPART(HOUR,[DateTime]),'0#') as varchar)+':00:00'
else cast(format(DATEPART(HOUR,[DateTime]),'0#') as varchar)+':30:00' end) as DateTime)) END),
ID,Code
Order by Interval
实际表格数据。
DateTime ID Code Duration
2017-12-12 00:30:00 1 12 1800
2017-12-12 00:30:37 1 12 37
2017-12-12 01:00:00 1 12 1793
2017-12-12 01:30:00 1 12 1800
2017-12-12 01:30:59 1 12 59
如果我运行查询,则预期结果是,
DateTime ID Code Duration
2017-12-12 00:00:00 1 12 1837
2017-12-12 00:30:00 1 12 1800
2017-12-12 01:00:00 1 12 1800
2017-12-12 01:30:00 1 12 59
DateTime ID Code Duration
2017-12-12 00:00:00 1 12 1800
2017-12-12 00:30:00 1 12 1800
2017-12-12 01:00:00 1 12 1800
2017-12-12 01:30:00 1 12 59
答案 0 :(得分:0)
You can use date functions to round [DateTime] into 30 minute intervals, but the result does not match your expected result, which appears to be wrong
DateTime ID Code Duration
2017-12-12 00:30:00 1 12 1800 1800 + 337 = 1837 2017-12-12 00:30
2017-12-12 00:30:37 1 12 37
2017-12-12 01:00:00 1 12 1793 1793 2017-12-12 01:00
2017-12-12 01:30:00 1 12 1800 1800 + 59 = 1859 2017-12-12 01:30
2017-12-12 01:30:59 1 12 59
MS SQL Server 2014 Schema Setup:
CREATE TABLE Table1
([DateTime] datetime, [ID] int, [Code] int, [Duration] int)
;
INSERT INTO Table1
([DateTime], [ID], [Code], [Duration])
VALUES
('2017-12-12 00:30:00', 1, 12, 1800),
('2017-12-12 00:30:37', 1, 12, 37),
('2017-12-12 01:00:00', 1, 12, 1793),
('2017-12-12 01:30:00', 1, 12, 1800),
('2017-12-12 01:30:59', 1, 12, 59)
;
Query 1:
select
dateadd(minute, (datediff(minute, [DateTime], 0) / 30) * 30, 0)
, sum(duration)
from table1
group by
dateadd(minute, (datediff(minute, [DateTime], 0) / 30) * 30, 0)
| | |
|----------------------|------|
| 1782-01-19T22:30:00Z | 1859 |
| 1782-01-19T23:00:00Z | 1793 |
| 1782-01-19T23:30:00Z | 1837 |