表名为:MZGH,
字段为  BRID ,  sj
663978 2012-03-21 9:36:21
664049 2012-03-21 8:57:43
664049 2012-03-21 9:00:39
664055 2012-03-21 8:17:03
664055 2012-03-21 8:18:53
664055 2012-03-21 8:22:34
664234 2012-03-21 10:25:40
664234 2012-03-21 10:48:33
664623 2012-03-21 8:14:51
664623 2012-03-21 8:14:52
664623 2012-03-21 8:14:52
665413 2012-03-21 10:17:51
665413 2012-03-21 10:25:49
666198 2012-03-21 15:20:20
666198 2012-03-21 15:23:47
666268 2012-03-21 15:14:26
666268 2012-03-21 15:15:13
666268 2012-03-21 15:16:58
666268 2012-03-21 15:19:20
每个BRID,只需取 时间最小的 一行记录 ,
如何写语句?

解决方案 »

  1.   

    select BRID,min(sj) as minsj
    from  MZGH
    group by BRID
      

  2.   

    SELECT BRID, MIN(sj) AS minsj FROM MZGH GROUP BY BRID
      

  3.   

    --如果只有两个字段
    select brid , min(sj) sj from mzgh group by brid--如果有多个字段
    select t.* from mzgh t where sj = (select min(sj) from mzgh where bird = t.bird )
    select t.* from mzgh t where not exists (select 1 from mzgh where bird = t.bird and sj < t.sj)
      

  4.   

    可以分两步进行,第一步select min(sj)from MZGH 可以查出最小的时间,第二步select*from MZGH where sj=所查出的最小时间
      

  5.   

    select BRID,min(sj) as minsj
    from MZGH
    group by BRID
      

  6.   

    select BRAID,min(sj) as minsj from MZGH 
    group by BRAID
      

  7.   


    select BRID,min(sj) from MZGH group by BRID