0

寻找有关此 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 的第一次出现

4

2 回答 2

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
;
于 2013-08-13T11:19:00.433 回答
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;

似乎可以解决问题

于 2013-08-13T11:33:24.287 回答