Oracle 11g 如何编写查询以获得所需的结果?具体来说,如何加入REQUIREMENTS表中的最新记录?
选择标准:最近的细节和最近的要求
with
detail as (
select 'TAXI' code, '4920' numb, 1990 dstart, 'BLUE' taxi_name from dual union all
select 'TAXI' code, '4920' numb, 2000 dstart, 'PINK' taxi_name from dual union all
select 'TAXI' code, '4920' numb, 2008 dstart, 'CYAN' taxi_name from dual),
requirement as (
select 'TAXI' code, '4920' numb, 2 seqno, 2000 rstart, 25,min_age,'CDL',permit from dual union all
select 'TAXI' code, '4920' numb, 4 seqno, 2000 rstart, 27 min_age, 'CAR' permit from dual union all
select 'TAXI' code, '4920' numb, 6 seqno, 2000 rstart, 29 min_age, 'HAZ' permit from dual union all
select 'TAXI' code, '4920' numb, 2 seqno, 2007 rstart, 24 min_age, 'CDL' permit from dual union all
select 'TAXI' code, '4920' numb, 4 seqno, 2007 rstart, 26 min_age, 'VAN' permit from dual union all
select 'TAXI' code, '4920' numb, 6 seqno, 2007 rstart, 28 min_age, 'HAZ' permit from dual
)
select code,numb,taxi_name, dstart, ranking from (
select code, numb, taxi_name,dstart, rank() over (partition by code, numb
order by dstart desc ) ranking
from detail)
where ranking =1
产量
CODE NUMB NAME YEAR
---- ---- ----- -------
TAXI 4920 CYAN 2008
DESIRED OUTPUT
--------------------------
code numb name dstart sqno rstart min_age permit
TAXI 4920 CYAN 2008 2 2007 24 CDL
TAXI 4920 CYAN 2008 4 2007 26 VAN
TAXI 4920 CYAN 2008 6 2007 28 HAZ