Oracle 11g 如何编写查询以获得所需结果?具体来说,如何加入要求表中的最新记录?
选择标准: 最新细节和最新要求
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
答案 0 :(得分:1)
嗯,你基本上有这个想法。您只需将其应用于两个表:
select code,numb,taxi_name, dstart, ranking
from (select code, numb, taxi_name,dstart,
rank() over (partition by code, numb order by dstart desc) as ranking
from detail
) d join
(select r.*,
rank() over (partition by code, numb order by rstart desc) as ranking
from requirement
) r
on d.code = r.code and
d.ranking = 1 and
r.ranking = 1
答案 1 :(得分:1)
您的完整查询可能是这样的:
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, seqno, rstart, min_age, permit
from
(
select d.code, d.numb, d.taxi_name, d.dstart, rank() over (partition by d.code, d.numb order by d.dstart desc ) ranking,
seqno, rstart, min_age, permit
from detail d
inner join
(
select code, numb, seqno, rstart, min_age, permit,
rank() over (partition by code, numb order by rstart desc ) ranking
from requirement
) r
on d.code = r.code
and d.numb = r.numb
and r.ranking = 1
)
where ranking =1
结果是:
| CODE | NUMB | TAXI_NAME | DSTART | SEQNO | 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 |