0

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
4

2 に答える 2

1

さて、あなたは本質的に考えを持っています。2つのテーブルに適用する必要があります。

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
于 2012-12-12T20:14:16.727 に答える
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

SQL FiddlewithDemoを参照してください

結果は次のとおりです。

| 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 |
于 2012-12-12T20:17:35.627 に答える