现在有一个表,里面有字段tm和字段bb,tm中是2014-01-01 00:00:00  每隔一个小时的时间,一直到2014-12-31 23:00:00,bb中是1到100随机的数字,怎么样写sql语句能显示出来
每天中bb最大的那一行,
例如 
 2014-01-01 11:00:00   99  (假定1月1日11点的99是1月1日每个时段中最大的那个数字,下同)
2014-01-02  17:00:00   89
这样的呢   

解决方案 »

  1.   

    SELECT b.* FROM 
    (SELECT CONVERT(VARCHAR(10),tm,120) AS tm,MAX(bb) AS bb  FROM tb GROUP BY CONVERT(VARCHAR(10),tm,120)) a LEFT JOIN tb b ON a.tm=CONVERT(VARCHAR(10),b.tm,120) AND a.bb=b.bb
      

  2.   


    if OBJECT_ID('tb') is not null
       drop table tb
    go
    create table tb
    (
     tm datetime,
     bb int
    )
    go
    insert into tb
    select '2014-01-01 00:00:00',1
    union all
    select '2014-01-01 01:00:00',2
    union all
    select '2014-01-01 01:00:00',3
    union all
    select '2014-01-01 02:00:00',5
    union all
    select '2014-01-01 02:00:00',8
    go
    select * from tb a where not exists(select 1 from tb where tm=a.tm and bb>a.bb)