日期:2014-05-17 浏览次数:20720 次
WITH t AS ( SELECT 1 id,1001 com_id,2001 product_id FROM dual UNION ALL SELECT 2,1001,2002 FROM dual UNION ALL SELECT 3,1001,2003 FROM dual UNION ALL SELECT 4,1002,2004 FROM dual UNION ALL SELECT 5,1002,2005 FROM dual UNION ALL SELECT 6,1003,2008 FROM dual ) SELECT id,com_id,product_id FROM ( SELECT t.*,Row_Number() over (PARTITION BY com_id ORDER BY dbms_random.random)rn FROM t ) WHERE rn=1 ORDER BY com_id;
------解决方案--------------------
由于楼主也没有说取值的大小规则,所以推测奇数ID的公司取时间最大值,偶数ID的公司取时间最小值,故代码为:
with t as (select 1001 id,2001 year from dual union all select 1001 ,2002 from dual union all select 1001 ,2003 from dual union all select 1002 ,2004 from dual union all select 1002 ,2005 from dual union all select 1003 ,2008 from dual) select id,case when mod(id, 2) = 0 then max(year) else min(year) end from t group by id order by id;