我有一个表中存储的温度测量,每个度量都有实际的日期/时间,但从逻辑上来说,测量日从中午到中午(而不是午夜 - 午夜)运行。 例如,在09/18/2011 09:00:00完成的测量将属于09/18/2011“测量日”,但 在09/18/2011 13:00:00完成的测量已属于09/19/2011年“措施日”。TSQL使用时间在哪里条款
所以现在我想要做一些统计查询,就像在每个“测量日” 09 - 11年间的平均温度,这是通过:
-- Select the AVG temp between 09-11
select MeasureDate, AVG(Temp) from @d
where Time between '09:00:00' and '11:00:00'
Group by MeasureDate
但是如果我想23之间的平均温度:00-01:00我必须使用不同的查询(没有时间在这里工作“之间”):
-- Select the AVG temp between 23:00-01:00
select MeasureDate, AVG(Temp) from @d
where (Time>='23:00:00' or Time<='01:00:00')
Group by MeasureDate
有一个简单的办法有相同的查询,以支持两个场景? 理想情况下,我想有一个函数,它将@从/ @作为参数,并将结果作为表返回。
下面是一个可以运行的完整示例。 Thx!
DECLARE @d TABLE ([Date] DATE, [Time] TIME(0), Temp DECIMAL(6,3), [MeasureDate] DATE);
INSERT @d VALUES
('2011-09-18','09:00:00',38.15, '2011-09-18'),
('2011-09-18','10:00:00',38.20, '2011-09-18'),
('2011-09-18','11:00:00',38.22, '2011-09-18'),
('2011-09-18','23:00:00',38.22, '2011-09-19'), -- Note the difference here between Date and MeasureDate
('2011-09-19','00:00:00',38.17, '2011-09-19'),
('2011-09-19','01:00:00',38.32, '2011-09-19'),
('2011-09-19','09:00:00',39.15, '2011-09-19'),
('2011-09-19','10:00:00',39.20, '2011-09-19'),
('2011-09-19','11:00:00',39.22, '2011-09-19'),
('2011-09-19','23:00:00',39.22, '2011-09-20'), -- Note the difference here between Date and MeasureDate
('2011-09-20','00:00:00',39.17, '2011-09-20'),
('2011-09-20','01:00:00',39.32, '2011-09-20');
-- Select the AVG temp between 09-11
select MeasureDate, AVG(Temp) from @d
where Time between '09:00:00' and '11:00:00'
Group by MeasureDate
-- Select the AVG temp between 23:00-01:00
select MeasureDate, AVG(Temp) from @d
where (Time>='23:00:00' or Time<='01:00:00')
Group by MeasureDate
这实际上并不是您的问题的答案,但为避免重复计算报告中的值,您应该不会使用BETWEEN,而是使用不等式:'WHERE Time> = '09:00:00'AND时间<'11:00:00'' – Tao 2011-12-19 17:29:41