我又不会写SQL了,谁来帮帮我,谢谢!
create table t
(
日期 char(10),
时间段 char(2),
时间 datetime,
数量 int
)
insert into t select '2012-3-1','06','2012-03-01 06:10:00',100
insert into t select '2012-3-1','06','2012-03-01 06:20:00',100
insert into t select '2012-3-1','06','2012-03-01 06:30:00',100
insert into t select '2012-3-1','06','2012-03-01 06:40:00',100
insert into t select '2012-3-1','06','2012-03-01 06:50:00',100
insert into t select '2012-3-1','07','2012-03-01 07:00:00',100
insert into t select '2012-3-1','07','2012-03-01 07:10:00',200
insert into t select '2012-3-1','07','2012-03-01 07:20:00',200
insert into t select '2012-3-1','07','2012-03-01 07:30:00',200
insert into t select '2012-3-1','07','2012-03-01 07:40:00',200
insert into t select '2012-3-1','07','2012-03-01 07:50:00',200
insert into t select '2012-3-1','08','2012-03-01 08:00:00',200
/*
得到如下结果,注:每个时间段都是从10分钟开始,到整点结束
也就是说,如果要查询06点的数据,就是从06:10---07:00这个区间统计
如果要查询07点的数据,就是从07:10---08:00这个区间统计
日期 时间段 数量总和
2012-03-01 06 600
2012-03-02 07 1200
*/
drop table t
[解决办法]
create table t(日期 char(10),时间段 char(2),时间 datetime,数量 int)insert into t select '2012-03-01','06','2012-03-01 06:10:00',100insert into t select '2012-03-01','06','2012-03-01 06:20:00',100insert into t select '2012-03-01','06','2012-03-01 06:30:00',100insert into t select '2012-03-01','06','2012-03-01 06:40:00',100insert into t select '2012-03-01','06','2012-03-01 06:50:00',100insert into t select '2012-03-01','06','2012-03-01 07:00:00',100insert into t select '2012-03-01','07','2012-03-01 07:10:00',200insert into t select '2012-03-01','07','2012-03-01 07:20:00',200insert into t select '2012-03-01','07','2012-03-01 07:30:00',200insert into t select '2012-03-01','07','2012-03-01 07:40:00',200insert into t select '2012-03-01','07','2012-03-01 07:50:00',200insert into t select '2012-03-01','07','2012-03-01 08:00:00',200insert into t select '2012-03-01','12','2012-03-01 12:10:00',300insert into t select '2012-03-01','12','2012-03-01 12:20:00',300insert into t select '2012-03-01','12','2012-03-01 12:30:00',300insert into t select '2012-03-01','12','2012-03-01 12:40:00',300insert into t select '2012-03-01','12','2012-03-01 12:50:00',300insert into t select '2012-03-01','12','2012-03-01 13:00:00',300insert into t select '2012-03-01','23','2012-03-01 23:10:00',400insert into t select '2012-03-01','23','2012-03-01 23:20:00',400insert into t select '2012-03-01','23','2012-03-01 23:30:00',400insert into t select '2012-03-01','23','2012-03-01 23:40:00',400insert into t select '2012-03-01','23','2012-03-01 23:50:00',400insert into t select '2012-03-02','00','2012-03-02 00:00:00',400insert into t select '2012-03-02','00','2012-03-02 00:10:00',500insert into t select '2012-03-02','00','2012-03-02 00:20:00',500insert into t select '2012-03-02','00','2012-03-02 00:30:00',500insert into t select '2012-03-02','00','2012-03-02 00:40:00',500insert into t select '2012-03-02','00','2012-03-02 00:50:00',500insert into t select '2012-03-02','01','2012-03-02 01:00:00',500insert into t select '2012-03-02','05','2012-03-02 05:10:00',600insert into t select '2012-03-02','05','2012-03-02 05:20:00',600insert into t select '2012-03-02','05','2012-03-02 05:30:00',600insert into t select '2012-03-02','05','2012-03-02 05:40:00',600insert into t select '2012-03-02','05','2012-03-02 05:50:00',600insert into t select '2012-03-02','06','2012-03-02 06:00:00',600select 日期,时间段,SUM(数量) 数量 from (select 日期,时间,数量,case when DATEPART(MI,时间)=0 and DATEPART(hh,时间)<>0 then right('0'+ltrim(DATEPART(HH,时间)-1),2) when DATEPART(MI,时间)=0 and DATEPART(hh,时间)=0 then '23'else right('0'+ltrim(DATEPART(HH,时间)),2) end as 时间段 from t)a group by 日期,时间段 order by 1/*日期 时间段 数量2012-03-01 06 6002012-03-01 07 12002012-03-01 12 18002012-03-01 23 20002012-03-02 00 30002012-03-02 05 36002012-03-02 23 400*/
[解决办法]
create table t(日期 char(10),时间段 char(2),时间 datetime,数量 int)insert into t select '2012-03-01','06','2012-03-01 06:10:00',100insert into t select '2012-03-01','06','2012-03-01 06:20:00',100insert into t select '2012-03-01','06','2012-03-01 06:30:00',100insert into t select '2012-03-01','06','2012-03-01 06:40:00',100insert into t select '2012-03-01','06','2012-03-01 06:50:00',100insert into t select '2012-03-01','06','2012-03-01 07:00:00',100insert into t select '2012-03-01','07','2012-03-01 07:10:00',200insert into t select '2012-03-01','07','2012-03-01 07:20:00',200insert into t select '2012-03-01','07','2012-03-01 07:30:00',200insert into t select '2012-03-01','07','2012-03-01 07:40:00',200insert into t select '2012-03-01','07','2012-03-01 07:50:00',200insert into t select '2012-03-01','07','2012-03-01 08:00:00',200insert into t select '2012-03-01','12','2012-03-01 12:10:00',300insert into t select '2012-03-01','12','2012-03-01 12:20:00',300insert into t select '2012-03-01','12','2012-03-01 12:30:00',300insert into t select '2012-03-01','12','2012-03-01 12:40:00',300insert into t select '2012-03-01','12','2012-03-01 12:50:00',300insert into t select '2012-03-01','12','2012-03-01 13:00:00',300insert into t select '2012-03-01','23','2012-03-01 23:10:00',400insert into t select '2012-03-01','23','2012-03-01 23:20:00',400insert into t select '2012-03-01','23','2012-03-01 23:30:00',400insert into t select '2012-03-01','23','2012-03-01 23:40:00',400insert into t select '2012-03-01','23','2012-03-01 23:50:00',400insert into t select '2012-03-02','00','2012-03-02 00:00:00',400insert into t select '2012-03-02','00','2012-03-02 00:10:00',500insert into t select '2012-03-02','00','2012-03-02 00:20:00',500insert into t select '2012-03-02','00','2012-03-02 00:30:00',500insert into t select '2012-03-02','00','2012-03-02 00:40:00',500insert into t select '2012-03-02','00','2012-03-02 00:50:00',500insert into t select '2012-03-02','01','2012-03-02 01:00:00',500insert into t select '2012-03-02','05','2012-03-02 05:10:00',600insert into t select '2012-03-02','05','2012-03-02 05:20:00',600insert into t select '2012-03-02','05','2012-03-02 05:30:00',600insert into t select '2012-03-02','05','2012-03-02 05:40:00',600insert into t select '2012-03-02','05','2012-03-02 05:50:00',600insert into t select '2012-03-02','06','2012-03-02 06:00:00',600select 日期,right(时间段+100,2) 时间段,SUM(数量)as 数量 from (select case when DATEPART(hour,时间)=0 and DATEPART(MI,时间)=0 then dateadd(day,-1,日期) else 日期 end 日期,case when DATEPART(mi,时间)=0 then case when DATEPART(hour,时间)=0 then 23 else right(DATEPART(hour,时间)-1+100,2)end else 时间段 end 时间段,数量 from t )a group by 日期,时间段/*日期 时间段 数量2012-03-02 00:00:00.000 00 30002012-03-02 00:00:00.000 05 36002012-03-01 00:00:00.000 06 6002012-03-01 00:00:00.000 07 12002012-03-01 00:00:00.000 12 18002012-03-01 00:00:00.000 23 2400*/