表中有三个字段
F1,F2,F3
记录之间F1和F2有可能重复,F3不会,
比如
F1     F2     F3
1      11      1
2      22      2
2      22      3
1      11      4
3      33      5对于F1和F2重复的,我只想要一条,任何一条都可以。
比如结果为
1     11     1
2     22     2
3     33     5如果用postgresql里的distinct on很好写,但是不知道标准的sql怎么写?不能用函数,不能用临时表,只有一条sql语句。

解决方案 »

  1.   


    select * from tbname a
    where not exists (
    select 1 from tbname
    where F1=a.F1 and F2=a.F2 and F3<a.F3
    )
      

  2.   


    --or:
    select * from tbname a
    where F3= (
    select min(F3) from tbname
    where F1=a.F1 and F2=a.F2
    )
      

  3.   


    --or:
    select * from tbname a
    where F3= (
    select top 1 F3 from tbname
    where F1=a.F1 and F2=a.F2 and F3<a.F3
    order by F3
    )
      

  4.   


    SELECT F1,F2,MIN(F3) F3 FROM kdp_12345  GROUP BY F1,F2 
      

  5.   

    直接分组,用max(),min()即可.select f1,f2,max(f3) f3 from tb group by f1,f2
    select f1,f2,min(f3) f3 from tb group by f1,f2
      

  6.   

    sorry,字符型也可以做max()。谢谢大家的回答。