得到每个STATIONID的前两条是不?
一条SQL不好实现吧. 
我感觉得整个存储过程

解决方案 »

  1.   


    select * from 表 a 
    where [time] in(select top 2 [time] from 表 where STATIONID=a.STATIONID)
      

  2.   

    --测试--测试数据
    create table 表(STATIONID int,TIME datetime,ICVALUE int,CVALUE int)
    insert 表 select 2520,'2003/04/28 22:07:48',-3514,31
    union all select 2520,'2003/04/28 22:13:49',-36,34
    union all select 2520,'2003/04/28 22:41:35',-4248,46
    union all select 2520,'2003/04/28 22:50:28',-3847,1176
    union all select 2530,'2003/04/28 23:02:10',-3208,3
    union all select 2530,'2003/04/28 23:15:10',-57,18
    union all select 2530,'2003/04/28 23:15:57',-3599,1303
    union all select 2530,'2003/04/28 23:20:31',-4436,22
    union all select 2540,'2003/04/28 23:38:05',-4655,425
    union all select 2540,'2003/04/28 23:51:42',-3907,24
    union all select 2540,'2003/04/29 00:02:28',-4228,14
    union all select 2540,'2003/04/28 23:02:28',-4226,12
    go--查询
    select * from 表 a 
    where [time] in(select top 2 [time] from 表 where STATIONID=a.STATIONID)
    go--删除测试
    drop table 表/*--测试结果STATIONID   TIME                        ICVALUE     CVALUE   
    ----------- --------------------------- ----------- ---------
    2520        2003-04-28 22:07:48.000     -3514       31
    2520        2003-04-28 22:13:49.000     -36         34
    2530        2003-04-28 23:02:10.000     -3208       3
    2530        2003-04-28 23:15:10.000     -57         18
    2540        2003-04-28 23:38:05.000     -4655       425
    2540        2003-04-28 23:51:42.000     -3907       24(所影响的行数为 6 行)
    --*/
      

  3.   

    --用临时表
    select *
    into #t
    from 表
    where TIME not in(select min(TIME) from 表 group by STATIONID)
    order by STATIONID,TIMEselect *
    from 表
    where TIME in(select min(TIME) from 表 group by STATIONID)
    union
    select *
    from #t
    where TIME in(select min(TIME) from #t  group by STATIONID)
    order by STATIONID,TIME drop table #t
      

  4.   

    select top 2 * from 表  where STATIONID = '2520'
    UNION ALL 
    select top 2 * from 表  where STATIONID = '2530'
    UNION ALL 
    select top 2 * from 表  where STATIONID = '2540'或者SELECT   * from 表  a where  ICVALUE  IN(select top 2   ICVALUE  from 表  where STATIONID =a.STATIONID )