有一日期型字段和其他字段,要求将每天的最后一条记录取出来,最好是用一条SELECT语句,多谢了,例:
date   A  06-04-05 15:00:00   20
06-04-05 18:00:00   20
06-04-06 13:00:00   20
06-04-06 17:00:00   30
...........数据包括全年的,要求返回如下结果集06-04-05 18:00:00   20
06-04-06 17:00:00   30.....

解决方案 »

  1.   

    select * from tbl a where right(date1,8)=
    (select max(right(date1,8)) from tbl where left(date1,8)=left(a.date1,8))
      

  2.   

    declare  @a  table(date datetime,a int)
    insert @a select '06-04-05 15:00:00','   20'
    union all select '06-04-05 18:00:00','   20'
    union all select '06-04-06 13:00:00 ','  20'
    union all select '06-04-06 17:00:00 ','  30'
    select * from @a
    select max(date) as date,max(a) as a 
    from @a
    group by convert(char(10),date,112)
      

  3.   

    select * from 表 a
    where not exists 
    (select 1 from 表 where a=a.a and date<a.date)
      

  4.   

    declare @t table(date varchar(20),A int)
    insert @T  
    select '06-04-05 15:00:00',   20 union all
    select '06-04-05 18:00:00',   20 union all
    select '06-04-06 13:00:00',   20 union all
    select '06-04-06 17:00:00',   30select * from @t g 
    where not exists(select 1 from @t where left([date],8)=left(g.[date],8) 
     and [date]>g.[date])
      

  5.   

    select * from 表 where date in (select max(date) from 表 
    group by convert(char(8),date,112))
      

  6.   

    可能不是很好,但查询无问题,多条记录时间相同时可能每天会多于一条记录
    select * from 表 
    where date in (select date  from 
      (select convert(char(10),date,120),max(date) as date from 表 
        group by convert(char(10,date,120)) A)
      

  7.   

    select * from 表 a
    where not exists 
    (select * from 表 where convert(char(10),date,120)=convert(char(10),a.date,120) and date<a.date)
      

  8.   

    SELECT * 
    FROM table1
    WHERE DATE IN (
    SELECT MAX(DATE)
    FROM table1
    GROUP BY convert(nvarchar(20),DATE,112))
      

  9.   


    select *
    from table
    where  date in
    (
     select max(date)
     from table 
     group by convert(varchar(10),date,120))