我想获得以下结果。请帮助Oracle SQL查询。
结果:
ID | APP_NUMBER | DATE
1 | ABC4100 | 10/02/2016
11 | ABC1001 | 10/04/2016
34 | ABC2001 | 9/15/2016
上述结果来自以下两个表:
表1:
ROW | ID | APP_NUMBER | DATE
1 | 1 | ABC4000 | 01/01/2016
2 | 1 | ABC4100 | 01/02/2016
3 | 1 | ABC4100 | 01/03/2016
4 | 11 | ABC1001 | 04/01/2016
5 | 34 | ABC2001 | 05/01/2016
表2:
APP_NUMBER | DATE
ABC4000 | 01/01/2016
ABC4100 | 08/02/2016
ABC4100 | 09/15/2016
ABC4100 | 10/02/2016
ABC1001 | 10/01/2016
ABC1001 | 10/02/2016
ABC1001 | 10/03/2016
ABC1001 | 10/04/2016
ABC2001 | 09/14/2016
ABC2001 | 09/15/2016
答案 0 :(得分:1)
试试这个:
select id,
app_number,
"date"
from (
select distinct table_1.id,
table_1.app_number,
table_2."date" ,
rank() over (partition by id order by table_2."date" desc) r
from table_1,
table_2
where table_1.APP_NUMBER=table_2.APP_NUMBER
)
where r=1
order by id;
答案 1 :(得分:1)
这将为您提供所需的结果。
with table2(APP_NUMBER, DT) as ( select 'ABC4000',to_date ('01/01/2016','mm/dd/yyyy') from dual
UNION ALL
select 'ABC4100',to_date ('08/02/2016','mm/dd/yyyy') from dual
UNION ALL
select 'ABC4100',to_date ('09/15/2016','mm/dd/yyyy') from dual
UNION ALL
select 'ABC4100',to_date ( '10/02/2016','mm/dd/yyyy') from dual
UNION ALL
select 'ABC1001',to_date ( '10/01/2016','mm/dd/yyyy') from dual
UNION ALL
select 'ABC1001',to_date ('10/02/2016','mm/dd/yyyy') from dual
UNION ALL
select 'ABC1001',to_date ( '10/03/2016','mm/dd/yyyy') from dual
UNION ALL
select 'ABC1001',to_date ('10/04/2016','mm/dd/yyyy') from dual
UNION ALL
select 'ABC2001',to_date ( '09/14/2016','mm/dd/yyyy') from dual
UNION ALL
select 'ABC2001',to_date ( '09/15/2016','mm/dd/yyyy') from dual ),
table1(ROWN,ID,APP_NUMBER,DTE) as ( select '1', '1','ABC4000',to_date( '01/01/2016','mm/dd/yyyy') from dual
UNION ALL
select '2', '1','ABC4100',to_date( '01/02/2016','mm/dd/yyyy') from dual
UNION ALL
select '3', '1','ABC4100',to_date( '01/03/2016','mm/dd/yyyy') from dual
UNION ALL
select '4', '11','ABC1001',to_date( '04/01/2016','mm/dd/yyyy') from dual
UNION ALL
select '5', '34','ABC2001',to_date( '05/01/2016','mm/dd/yyyy') from dual
)
select max(t1.id),t2.app_number,max(t2.dt)
from table2 t2
inner join table1 t1
ON t1.APP_NUMBER = t2.APP_NUMBER
where t1.APP_NUMBER <> 'ABC4000'
group by t2.APP_NUMBER ,t1.id
order by t1.id ;
答案 2 :(得分:0)
SELECT t1.ID, t1.APP_NUMBER, MAX(t2.Date)
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.APP_NUMBER = t2.APP_NUMBER
WHERE t1.APP_NUMBER IN ('ABC4100', 'ABC1001', 'ABC2001')
ORDER BY t1.ID
这应该有效,但是如果它没有,你可能想尝试t2.APP_NUMBER。
答案 3 :(得分:-1)
试试这个 -
SELECT ID,t1.APP_NUMBER,MAX(t2.Date)
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.APP_NUMBER=t2.APP_NUMBER
GROUP BY ID,t1.APP_NUMBER
答案 4 :(得分:-2)
您可以使用LEFT JOIN连接两个表中的结果,如下例所示:
Select * from Table1 t1 LEFT JOIN Table2 t2 ON (t1.APP_NUMBER = t2.APP_NUMBER);