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

解决方案 »

  1.   


    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',200with m
    as(
    select *,case when DATEPART(MI,时间)=0 then '0'+ltrim(DATEPART(HH,时间)-1)
    else '0'+ltrim(DATEPART(HH,时间)) end as 时段 from t
    )select 日期,时段 as 时间段,SUM(数量) 数量  from m
    group by 日期,时段/*
    日期 时间段 数量
    2012-3-1   06 600
    2012-3-1   07 1200
    */
      

  2.   


    --MSSQL200版本:
    select 日期,时间段,SUM(数量) 数量  from (select 日期,时间,数量,
    case when DATEPART(MI,时间)=0 then '0'+ltrim(DATEPART(HH,时间)-1)
    else '0'+ltrim(DATEPART(HH,时间)) end as 时间段 from t
    )a  group by 日期,时间段/*
    日期 时间段 数量
    2012-3-1   06 600
    2012-3-1   07 1200
    */
      

  3.   


    create table t
    (
    date_time char(10),
    time_dur char(2),
    times datetime,
    qty 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
    -------------------------------
    select date_time,time_dur,SUM(qty)as qty from (
    select date_time,case when DATEPART(mi,times)=0 then right(DATEPART(hour,times)-1+100,2)  else time_dur end time_dur,
    qty
     from t )a
     group by date_time,time_dur
    /*
    date_time time_dur qty
    2012-3-1   06 600
    2012-3-1   07 1200
    */
      

  4.   


    --使用NTILE(n)over()函数
    select 日期,MIN(时间段) 时间段,SUM(数量) 数量 from(
    select NTILE(2)over(order by 时间) as TitleId,
    * from t)a group by TitleId,日期
    /*
    日期 时间段 数量
    2012-3-1   06 600
    2012-3-1   07 1200
    */
      

  5.   

    问题一:right(时间段,2)即可问题二:最后加order by 时间
      

  6.   

    select 日期,时间段,SUM(数量) 数量  from (select 日期,时间,数量,
    case when DATEPART(MI,时间)=0 then '0'+ltrim(DATEPART(HH,时间)-1)
    else '0'+ltrim(DATEPART(HH,时间)) end as 时间段 from t
    )a  group by 日期,时间段-------------------------------------------------------
    right(时间段,2)即可在何处加right
      

  7.   

    then 和else之间的那一段:right(then和else之间的内容,2)
      

  8.   

    then 和else之间的那一段:right(then和else之间的内容,2)
      

  9.   

    select 日期,时间段,SUM(数量) 数量  from (select 日期,时间,数量,
    case when DATEPART(MI,时间)=0 then right('0'+ltrim(DATEPART(HH,时间)-1),2)
    else '0'+ltrim(DATEPART(HH,时间)) end as 时间段 from t
    )a  group by 日期,时间段
    --------------------------------------------------------
    是这个样子吗?可是加完了不对.都睡觉了吗,那明天再说吧,写了长长的例子。
      

  10.   

    select 日期,时间段,SUM(数量) 数量  from (select 日期,时间,数量,
    case when DATEPART(MI,时间)=0 then right('0'+ltrim(DATEPART(HH,时间)-1),2)
    else right('0'+ltrim(DATEPART(HH,时间)),2) end as 时间段 from t
    )a  group by 日期,时间段
    --这样就好了
      

  11.   

    谢啦,这么晚还没睡,这个语句还有点问题,就是在23点到00点的时候有问题,请看
    create table t
    (
    日期   char(10),
    时间段   char(2),
    时间   datetime,
    数量   int
    )insert into t select '2012-03-01','06','2012-03-01 06:10:00',100
    insert into t select '2012-03-01','06','2012-03-01 06:20:00',100
    insert into t select '2012-03-01','06','2012-03-01 06:30:00',100
    insert into t select '2012-03-01','06','2012-03-01 06:40:00',100
    insert into t select '2012-03-01','06','2012-03-01 06:50:00',100
    insert into t select '2012-03-01','06','2012-03-01 07:00:00',100
    insert into t select '2012-03-01','07','2012-03-01 07:10:00',200
    insert into t select '2012-03-01','07','2012-03-01 07:20:00',200
    insert into t select '2012-03-01','07','2012-03-01 07:30:00',200
    insert into t select '2012-03-01','07','2012-03-01 07:40:00',200
    insert into t select '2012-03-01','07','2012-03-01 07:50:00',200
    insert into t select '2012-03-01','07','2012-03-01 08:00:00',200
    insert into t select '2012-03-01','12','2012-03-01 12:10:00',300
    insert into t select '2012-03-01','12','2012-03-01 12:20:00',300
    insert into t select '2012-03-01','12','2012-03-01 12:30:00',300
    insert into t select '2012-03-01','12','2012-03-01 12:40:00',300
    insert into t select '2012-03-01','12','2012-03-01 12:50:00',300
    insert into t select '2012-03-01','12','2012-03-01 13:00:00',300
    insert into t select '2012-03-01','23','2012-03-01 23:10:00',400
    insert into t select '2012-03-01','23','2012-03-01 23:20:00',400
    insert into t select '2012-03-01','23','2012-03-01 23:30:00',400
    insert into t select '2012-03-01','23','2012-03-01 23:40:00',400
    insert into t select '2012-03-01','23','2012-03-01 23:50:00',400
    insert 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',500
    insert into t select '2012-03-02','00','2012-03-02 00:20:00',500
    insert into t select '2012-03-02','00','2012-03-02 00:30:00',500
    insert into t select '2012-03-02','00','2012-03-02 00:40:00',500
    insert into t select '2012-03-02','00','2012-03-02 00:50:00',500
    insert into t select '2012-03-02','01','2012-03-02 01:00:00',500
    insert into t select '2012-03-02','05','2012-03-02 05:10:00',600
    insert into t select '2012-03-02','05','2012-03-02 05:20:00',600
    insert into t select '2012-03-02','05','2012-03-02 05:30:00',600
    insert into t select '2012-03-02','05','2012-03-02 05:40:00',600
    insert into t select '2012-03-02','05','2012-03-02 05:50:00',600
    insert into t select '2012-03-02','06','2012-03-02 06:00:00',600/*
    得到如下结果,注:每个时间段都是从10分钟开始,到整点结束
    也就是说,如果要查询06点的数据,就是从06:10---7:00这个区间统计
    如果要查询07点的数据,就是从07:10---8:00这个区间统计要求结果如下:日期               时间段             数量总和
    2012-03-01         06                  600
    2012-03-01         07                  1200
    2012-03-01         12                  1800
    2012-03-01         23                  2400
    2012-03-02         00                  3000
    2012-03-02         05                  3600
    */
    /*
    select 日期,right(时间段,2)时间段,SUM(数量) 数量  from (select 日期,时间,数量,
    case when DATEPART(MI,时间)=0 then right('0'+ltrim(DATEPART(HH,时间)-1),2)
    else '0'+ltrim(DATEPART(HH,时间)) end as 时间段 from t
    )a  group by 日期,right(时间段,2)order by 日期,right(时间段,2) 
    */
    /*
    ----------------------------------------------------------------------------------
    上面那段代码的结果是这样子的:
    在23点的时间段里面数量应该2400,那个400跑到下面了,时间段也变成-1了
    2012-03-01 06 600
    2012-03-01 07 1200
    2012-03-01 12 1800
    2012-03-01 23 2000
    2012-03-02 00 3000
    2012-03-02 05 3600
    2012-03-02 -1 400
    */select 日期,时间段,SUM(数量) 数量 from (select 日期,时间,数量,
    case when DATEPART(MI,时间)=0 then right('0'+ltrim(DATEPART(HH,时间)-1),2)
    else right('0'+ltrim(DATEPART(HH,时间)),2) end as 时间段 from t
    )a group by 日期,时间段drop table t
      

  12.   

    select 日期,时间段,SUM(数量) 数量 from (select 日期,时间,数量,
    case when DATEPART(MI,时间)=0 then right('0'+ltrim(DATEPART(HH,时间)-1),2) when 
    DATEPART(hh,时间)=0 then ltrim(23)
    else right('0'+ltrim(DATEPART(HH,时间)),2) end as 时间段 from t
    )a group by 日期,时间段
      

  13.   


    create table t
    (
    日期 char(10),
    时间段 char(2),
    时间 datetime,
    数量 int
    )insert into t select '2012-03-01','06','2012-03-01 06:10:00',100
    insert into t select '2012-03-01','06','2012-03-01 06:20:00',100
    insert into t select '2012-03-01','06','2012-03-01 06:30:00',100
    insert into t select '2012-03-01','06','2012-03-01 06:40:00',100
    insert into t select '2012-03-01','06','2012-03-01 06:50:00',100
    insert into t select '2012-03-01','06','2012-03-01 07:00:00',100
    insert into t select '2012-03-01','07','2012-03-01 07:10:00',200
    insert into t select '2012-03-01','07','2012-03-01 07:20:00',200
    insert into t select '2012-03-01','07','2012-03-01 07:30:00',200
    insert into t select '2012-03-01','07','2012-03-01 07:40:00',200
    insert into t select '2012-03-01','07','2012-03-01 07:50:00',200
    insert into t select '2012-03-01','07','2012-03-01 08:00:00',200
    insert into t select '2012-03-01','12','2012-03-01 12:10:00',300
    insert into t select '2012-03-01','12','2012-03-01 12:20:00',300
    insert into t select '2012-03-01','12','2012-03-01 12:30:00',300
    insert into t select '2012-03-01','12','2012-03-01 12:40:00',300
    insert into t select '2012-03-01','12','2012-03-01 12:50:00',300
    insert into t select '2012-03-01','12','2012-03-01 13:00:00',300
    insert into t select '2012-03-01','23','2012-03-01 23:10:00',400
    insert into t select '2012-03-01','23','2012-03-01 23:20:00',400
    insert into t select '2012-03-01','23','2012-03-01 23:30:00',400
    insert into t select '2012-03-01','23','2012-03-01 23:40:00',400
    insert into t select '2012-03-01','23','2012-03-01 23:50:00',400
    insert 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',500
    insert into t select '2012-03-02','00','2012-03-02 00:20:00',500
    insert into t select '2012-03-02','00','2012-03-02 00:30:00',500
    insert into t select '2012-03-02','00','2012-03-02 00:40:00',500
    insert into t select '2012-03-02','00','2012-03-02 00:50:00',500
    insert into t select '2012-03-02','01','2012-03-02 01:00:00',500
    insert into t select '2012-03-02','05','2012-03-02 05:10:00',600
    insert into t select '2012-03-02','05','2012-03-02 05:20:00',600
    insert into t select '2012-03-02','05','2012-03-02 05:30:00',600
    insert into t select '2012-03-02','05','2012-03-02 05:40:00',600
    insert into t select '2012-03-02','05','2012-03-02 05:50:00',600
    insert into t select '2012-03-02','06','2012-03-02 06:00:00',600
    select 日期,时间段,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 600
    2012-03-01 07 1200
    2012-03-01 12 1800
    2012-03-01 23 2000
    2012-03-02 00 3000
    2012-03-02 05 3600
    2012-03-02 23 400
    */
      

  14.   


    create table t
    (
    日期 char(10),
    时间段 char(2),
    时间 datetime,
    数量 int
    )
    insert into t select '2012-03-01','06','2012-03-01 06:10:00',100
    insert into t select '2012-03-01','06','2012-03-01 06:20:00',100
    insert into t select '2012-03-01','06','2012-03-01 06:30:00',100
    insert into t select '2012-03-01','06','2012-03-01 06:40:00',100
    insert into t select '2012-03-01','06','2012-03-01 06:50:00',100
    insert into t select '2012-03-01','06','2012-03-01 07:00:00',100
    insert into t select '2012-03-01','07','2012-03-01 07:10:00',200
    insert into t select '2012-03-01','07','2012-03-01 07:20:00',200
    insert into t select '2012-03-01','07','2012-03-01 07:30:00',200
    insert into t select '2012-03-01','07','2012-03-01 07:40:00',200
    insert into t select '2012-03-01','07','2012-03-01 07:50:00',200
    insert into t select '2012-03-01','07','2012-03-01 08:00:00',200
    insert into t select '2012-03-01','12','2012-03-01 12:10:00',300
    insert into t select '2012-03-01','12','2012-03-01 12:20:00',300
    insert into t select '2012-03-01','12','2012-03-01 12:30:00',300
    insert into t select '2012-03-01','12','2012-03-01 12:40:00',300
    insert into t select '2012-03-01','12','2012-03-01 12:50:00',300
    insert into t select '2012-03-01','12','2012-03-01 13:00:00',300
    insert into t select '2012-03-01','23','2012-03-01 23:10:00',400
    insert into t select '2012-03-01','23','2012-03-01 23:20:00',400
    insert into t select '2012-03-01','23','2012-03-01 23:30:00',400
    insert into t select '2012-03-01','23','2012-03-01 23:40:00',400
    insert into t select '2012-03-01','23','2012-03-01 23:50:00',400
    insert 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',500
    insert into t select '2012-03-02','00','2012-03-02 00:20:00',500
    insert into t select '2012-03-02','00','2012-03-02 00:30:00',500
    insert into t select '2012-03-02','00','2012-03-02 00:40:00',500
    insert into t select '2012-03-02','00','2012-03-02 00:50:00',500
    insert into t select '2012-03-02','01','2012-03-02 01:00:00',500
    insert into t select '2012-03-02','05','2012-03-02 05:10:00',600
    insert into t select '2012-03-02','05','2012-03-02 05:20:00',600
    insert into t select '2012-03-02','05','2012-03-02 05:30:00',600
    insert into t select '2012-03-02','05','2012-03-02 05:40:00',600
    insert into t select '2012-03-02','05','2012-03-02 05:50:00',600
    insert into t select '2012-03-02','06','2012-03-02 06:00:00',600
    select 日期,
    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 3000
    2012-03-02 00:00:00.000 05 3600
    2012-03-01 00:00:00.000 06 600
    2012-03-01 00:00:00.000 07 1200
    2012-03-01 00:00:00.000 12 1800
    2012-03-01 00:00:00.000 23 2400*/
      

  15.   


    select convert(varchar(10),日期,120)日期,
    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 3000
    2012-03-02 05 3600
    2012-03-01 06 600
    2012-03-01 07 1200
    2012-03-01 12 1800
    2012-03-01 23 2400
    */