日期:2014-05-17 浏览次数:20823 次
select A,max(B) B from Tab group by A
------解决方案--------------------
with cte as( SELECT 'a' A, '1' B FROM DUAL UNION ALL SELECT 'a', '2' FROM DUAL UNION ALL SELECT 'a' ,'3' FROM DUAL UNION ALL SELECT 'b' ,'1' FROM DUAL UNION ALL SELECT 'b' ,'2' FROM DUAL UNION ALL SELECT 'c' ,'1' FROM DUAL ) SELECT A,B FROM( SELECT ROW_NUMBER()OVER(PARTITION BY A ORDER BY DBMS_RANDOM.VALUE()) ID, A, B FROM CTE) WHERE ID=2
------解决方案--------------------
--补充二楼的写法
with t as (
select 'a' as A, 1 as B from dual
union all
select 'a', 2 from dual
union all
select 'a', 3 from dual
union all
select 'b', 1 from dual
union all
select 'b', 2 from dual
union all
select 'c', 1 from dual
)
select A,max(B) B
from t
group by A
having count(1) >1
A B
- ----------------------
a 3
b 2