有一张表t_warn 其中有四个字段如下,其他字段省略:id        stationId        warnType           startTime
1            2                6              2011-03-06 22:20:01.140
2            2                1              2011-03-06 22:20:01.140
3            1                1              2011-03-06 22:20:01.140
4            3                5              2011-03-06 22:30:01.217
5            4                3              2011-03-06 22:30:01.217
id为自增字段  stationId为站点     warnType为告警类型      startTime为告警时间请问怎么写SQL语句查询出每个站点最新的那条告警

解决方案 »

  1.   


    select *
    from tb t
    where id = (select top 1 id from tb where stationid = t.stationid order by starttime desc)
      

  2.   


    select *
    from tb t
    where not exists (select 1 from tb where stationid = t.stationid and starttime > t.starttime)
      

  3.   


    select *
    from tb t
    where starttime = (select max(starttime) from tb where stationid = t.stationid)
    推荐第一个,第二第三个,如果最大的时间有两条,那么这两条都会出来,如果同一站点最大时间是不重复的,那么三个都可以。
      

  4.   

    select *
    from tb t
    where id = (select top 1 id from tb 
                    where stationid = t.stationid order by starttime desc)
      

  5.   


    select *
    from tb t
    where id = (select top 1 id from tb where stationid = t.stationid order by starttime desc,id desc)
    貌似楼主时间相同的也有啊!再根据id排个序!
      

  6.   


    select *
    from tb t
    where not exists (select 1 from tb 
             where stationid = t.stationid and (starttime > t.starttime or (starttime = t.starttime and id > t.id)))
      

  7.   

    select
     *
    from
     tb t
    where
     starttime = (select max(starttime) from tb where stationid = t.stationid)
      

  8.   

    http://topic.csdn.net/u/20080626/00/43d0d10c-28f1-418d-a05b-663880da278a.html?85256
      

  9.   

    select
     *
    from
     tb t
    where
     starttime = (select max(starttime) from tb where stationid = t.stationid)
      

  10.   


    CREATE TABLE t_warn
    (
    id INT IDENTITY(1,1),
    stationId INT,
    warnType INT,
    startTime DATETIME
    )INSERT INTO t_warn
    SELECT 2, 6, '2011-03-06 22:20:01.140'
    UNION ALL
    SELECT 2 ,1 ,'2011-03-06 22:19:01.140'
    UNION ALL
    SELECT 1 ,1 ,'2011-03-06 22:20:01.140'
    UNION ALL
    SELECT 3 ,5 ,'2011-03-06 22:30:01.217'
    UNION ALL
    SELECT 4 ,3 ,'2011-03-06 22:30:01.217'
    UNION ALL
    SELECT 4 ,1 ,'2011-03-06 22:20:01.217'SELECT * FROM t_warnSELECT id,stationId,warnType,startTime FROM (
    SELECT *,ROW_NUMBER() OVER(PARTITION BY stationId ORDER BY startTime desc) num FROM t_warn) a
    WHERE a.num=1
    ORDER BY idSELECT * FROM t_warn a
    WHERE NOT EXISTS(SELECT * FROM t_warn WHERE stationId=a.stationId AND startTime>a.startTime)id          stationId   warnType    startTime
    ----------- ----------- ----------- -----------------------
    1           2           6           2011-03-06 22:20:01.140
    3           1           1           2011-03-06 22:20:01.140
    4           3           5           2011-03-06 22:30:01.217
    5           4           3           2011-03-06 22:30:01.217(4 row(s) affected)