问题描述
- SQL 多时间段查询 求SQL大牛帮忙
-
表数据如下
code val date
1 1 2014-07-16 08:03:01
1 2 2014-07-16 08:10:00
1 3 2014-07-16 08:15:11
1 5 2014-07-16 08:40:01
1 2 2014-07-16 09:01:09
1 6 2014-07-16 09:20:21
1 3 2014-07-16 09:50:30
1 1 2014-07-16 10:20:22
1 2 2014-07-16 10:33:32
1 6 2014-07-16 11:40:01
1 3 2014-07-16 12:30:222 1 2014-07-16 08:03:01
2 2 2014-07-16 08:10:00
2 3 2014-07-16 09:15:11
2 5 2014-07-16 09:40:01
2 2 2014-07-16 10:01:09
2 6 2014-07-16 11:20:21
2 3 2014-07-16 11:50:303 1 2014-07-16 08:20:22
3 2 2014-07-16 09:33:32
3 6 2014-07-16 10:40:01
3 3 2014-07-16 11:30:22分别求出每个code 1小时数据内val总和,2小时数据内val总和,
3小时数据内val总和,6小时数据内val总和假设当前时间为2014-07-16 12:00:00
解决方案
select sum(val),code,h from (select code,val,datediff(hh,'2014-07-16 12:00:00',date as h) from t) as a
group by code,h 以此为蓝本就出来了
时间: 2024-10-22 02:53:01