sampling_datetime          site_id            DUST
2008-5-1 1:00:00            4005             50.63
2008-5-1 2:00:00            4005             50.63
2008-5-1 3:00:00            4005             50.63
2008-5-1 4:00:00            4005             50.63
2008-5-1 5:00:00            4005             50.63
2008-5-1 6:00:00            4005             50.63
2008-5-1 7:00:00            4005             50.63
2008-5-1 8:00:00            4005             50.63
2008-5-2 1:00:00            4005             52.42
2008-5-2 2:00:00            4005             52.42
2008-5-2 3:00:00            4005             52.42
2008-5-2 4:00:00            4005             52.42
2008-5-2 5:00:00            4005             52.42
2008-5-2 6:00:00            4005             52.42
2008-5-2 7:00:00            4005             52.42
2008-5-2 8:00:00            4005             52.42我想得到是 
sampling_datetime          site_id            DUST
2008-5-1                    4005             405.64
2008-5-2                    4005             419.36

解决方案 »

  1.   

    --建立测试环境
    set nocount on
    create table test(sampling_datetime varchar(20),site_id varchar(20),DUST float)
    insert into test select '2008-5-1 1:00:00','4005','50.63'
    insert into test select '2008-5-1 2:00:00','4005','50.63'
    insert into test select '2008-5-1 3:00:00','4005','50.63'
    insert into test select '2008-5-1 4:00:00','4005','50.63'
    insert into test select '2008-5-1 5:00:00','4005','50.63'
    insert into test select '2008-5-1 6:00:00','4005','50.63'
    insert into test select '2008-5-1 7:00:00','4005','50.63'
    insert into test select '2008-5-1 8:00:00','4005','50.63'
    insert into test select '2008-5-2 1:00:00','4005','52.42'
    insert into test select '2008-5-2 2:00:00','4005','52.42'
    insert into test select '2008-5-2 3:00:00','4005','52.42'
    insert into test select '2008-5-2 4:00:00','4005','52.42'
    insert into test select '2008-5-2 5:00:00','4005','52.42'
    insert into test select '2008-5-2 6:00:00','4005','52.42'
    insert into test select '2008-5-2 7:00:00','4005','52.42'
    insert into test select '2008-5-2 8:00:00','4005','52.42'
      
    go
    --测试
    select convert(varchar(10),cast(sampling_datetime as datetime),120)
    ,site_id,sum(dust) from test
    group by  convert(varchar(10),cast(sampling_datetime as datetime),120)
    ,site_id--删除测试环境
    drop table test
     set nocount off
      

  2.   

    select convert(char(10),sampling_datetime,21) as sampling_datetime,
           site_id,sum(dust) as dust
    from tableA
    group by convert(char(10),sampling_datetime,21),site_id