表名 test
tnumber,ttype,tvalue,ttime
编号 类型 值 时间
1000 A 111 2008-1-1
1000 A 112 2008-1-2
1000 B 101 2008-1-2
1000 B 103 2008-1-3
1000 B 104 2008-1-4
1001 B 112 2008-1-5
1001 B 105 2008-1-4
1001 A 106 2008-1-1
1001 A 107 2008-1-2
1002 A 108 2008-1-2
1002 B 109 2008-1-2
1002 A 110 2008-1-1
需要的结果
1000 A 112 2008-1-2
1000 B 104 2008-1-4
1001 A 107 2008-1-2
1001 B 112 2008-1-5
1002 A 108 2008-1-2
1002 B 109 2008-1-2
tnumber,ttype,tvalue,ttime
编号 类型 值 时间
1000 A 111 2008-1-1
1000 A 112 2008-1-2
1000 B 101 2008-1-2
1000 B 103 2008-1-3
1000 B 104 2008-1-4
1001 B 112 2008-1-5
1001 B 105 2008-1-4
1001 A 106 2008-1-1
1001 A 107 2008-1-2
1002 A 108 2008-1-2
1002 B 109 2008-1-2
1002 A 110 2008-1-1
需要的结果
1000 A 112 2008-1-2
1000 B 104 2008-1-4
1001 A 107 2008-1-2
1001 B 112 2008-1-5
1002 A 108 2008-1-2
1002 B 109 2008-1-2
group by tnumber,ttype,ttime
楼主是想取group by tnumber, ttype的tvalue的前两名吧
from(
select tunmber,ttype,max(tvalue)
from test
group by tnumber,ttype)a,test b
where a.tnumber=b.tnumber
and a.ttype=b.ttype
and a.tvalue=b.tvalue
select tnumber,ttype,ttime
from ( select tnumber,ttype,ttime,rownum() over (partition by tnumber,ttype order by tnumber,ttype,tvalue desc) rn)
where rn=1;
利用rownum() over(...)函数的好处就是如果你还想取第二第三第四...名的话也可以。