在查询结果中查找第一次出现的ID

时间:2013-08-13 11:14:36

标签: oracle oracle11g

寻找这个Oracle(11g)查询的一点帮助......

我有以下SQL

SELECT id, test_date 
  FROM test_data 
 WHERE id IN (7,27,28,32,34,44) 
 GROUP BY id, test_date ORDER BY test_date ASC;

这给了我以下

ID / TEST_DATE
---------------
27 / 16-AUG-06
44 / 18-AUG-06
28 / 19-AUG-06
44 / 23-JUN-08
27 / 27-JUN-08
28 / 27-JUN-08
07 / 21-APR-09
07 / 11-MAR-10
44 / 02-OCT-10
27 / 08-OCT-10
34 / 04-APR-11
07 / 11-APR-11
07 / 18-MAR-12
32 / 30-JUN-13

我正在寻找的是以下结果

ID / TEST_DATE
---------------
27 / 16-AUG-06
44 / 18-AUG-06
28 / 19-AUG-06
07 / 21-APR-09
34 / 04-APR-11
32 / 30-JUN-13

基本上是每个ID的第一次出现

2 个答案:

答案 0 :(得分:1)

SELECT id, min(test_date)
  FROM test_data 
 WHERE id IN (7,27,28,32,34,44) 
 GROUP BY id 
 ORDER BY 2 ASC
;

答案 1 :(得分:0)

SELECT t1.id, (select t2.test_date from test_data t2 where t2.id = t1.id and rownum = 1) test_date
  FROM test_data t1
 WHERE t1.id IN (7,27,28,32,34,44) 
 GROUP BY t1.id  ORDER BY t1.id ASC;

似乎要做的伎俩