2014-09-26 3 views
0

가 여기 내 시나리오입니다 : ID = 1의 경우분석 쿼리에서 선택 파티션을 제외하는 방법은 무엇입니까?

, 201111의 cheese_year_seqno, 1 행은 XX의 공급 업체 코드가 그래서 나는 모든 201111 SEQNO을 exclue하는 을 좋아하지만 을 keept 것 201222 행의 순위를 지정할 수 있습니다. 주어진 year_seqno에 공급 업체가 XX 인 경우 모든 행을 순위 지정에 사용할 수 있도록 설정하십시오.

ID = 2는 XX의 공급 업체 코드가 없으므로 모든 행이 순위 지정이 가능해야합니다.

with cheese_row as 
(
select 1 as cheese_id, '201111' as cheese_year_seqno, 2 as cheese_lot, 1 as cheese_batch, 'AA' as cheese_vendor,trunc(sysdate-356) as cheese_batch_date from dual union all 
select 1 as cheese_id, '201111' as cheese_year_seqno, 2 as cheese_lot, 2 as cheese_batch, 'BB' as cheese_vendor,trunc(sysdate-356) as cheese_batch_date from dual union all 
select 1 as cheese_id, '201111' as cheese_year_seqno, 2 as cheese_lot, 3 as cheese_batch, 'XX' as cheese_vendor,trunc(sysdate-350) as cheese_batch_date from dual union all 
select 1 as cheese_id, '201222' as cheese_year_seqno, 1 as cheese_lot, 1 as cheese_batch, 'AA' as cheese_vendor,trunc(sysdate-856) as cheese_batch_date from dual union all 
select 1 as cheese_id, '201222' as cheese_year_seqno, 1 as cheese_lot, 2 as cheese_batch, 'DD' as cheese_vendor,trunc(sysdate-830) as cheese_batch_date from dual union all 
select 2 as cheese_id, '201333' as cheese_year_seqno, 2 as cheese_lot, 3 as cheese_batch, 'CC' as cheese_vendor,trunc(sysdate-300) as cheese_batch_date from dual union all 
select 2 as cheese_id, '201333' as cheese_year_seqno, 1 as cheese_lot, 1 as cheese_batch, 'AA' as cheese_vendor,trunc(sysdate-301) as cheese_batch_date from dual union all 
select 2 as cheese_id, '201444' as cheese_year_seqno, 1 as cheese_lot, 1 as cheese_batch, 'DD' as cheese_vendor,trunc(sysdate-290) as cheese_batch_date from dual 

) 
select cheese_id, 
     cheese_year_seqno, 
     cheese_lot, 
     cheese_batch, 
     cheese_vendor, 
     cheese_batch_date, 
     rank() over (partition by cheese_id 
         order by cheese_batch_date desc, 
           cheese_batch desc, 
           cheese_lot desc) as ch_rank1 
    from cheese_row 

/* If a cheese_year_seqno has cheese_vendor = XX then exclude the whole 
    cheese_year_seqno, but return all other batch seqno. 
    Rank the remaining cheese_year_seqno rows. 
    In this case the 20111 year_seqno has an XX as a cheese_vendor, 
    therefore return and rank only the two rows with 201222 year_seqno.  
*/  

원하는 결과 :

Return 
ID SEQNO LOT BA VEN DATE  RNK1 
---- -------- ---- ---- ----- ----------- ------ 
1 201222 1 2 DD 17-JUN-12 1 
1 201222 1 2 AA 22-MAY-12 2 
2 201444 1 1 DD 09-DEC-13 1 
2 201333 2 3 CC 29-NOV-13 2 
2 201333 1 1 AA 28-NOV-13 3 

답변

2

는 올해가 자격이 있는지 결정하기 위해 두 번째 분석 기능을 사용하여 그 필터링 :

with cheese_row as(
     select 1 as cheese_id, '201111' as cheese_year_seqno, 2 as cheese_lot, 1 as cheese_batch, 'AA' as cheese_vendor,trunc(sysdate-356) as cheese_batch_date from dual union all 
     select 1 as cheese_id, '201111' as cheese_year_seqno, 2 as cheese_lot, 2 as cheese_batch, 'BB' as cheese_vendor,trunc(sysdate-356) as cheese_batch_date from dual union all 
     select 1 as cheese_id, '201111' as cheese_year_seqno, 2 as cheese_lot, 3 as cheese_batch, 'XX' as cheese_vendor,trunc(sysdate-350) as cheese_batch_date from dual union all 
     select 1 as cheese_id, '201222' as cheese_year_seqno, 1 as cheese_lot, 1 as cheese_batch, 'AA' as cheese_vendor,trunc(sysdate-856) as cheese_batch_date from dual union all 
     select 1 as cheese_id, '201222' as cheese_year_seqno, 1 as cheese_lot, 2 as cheese_batch, 'DD' as cheese_vendor,trunc(sysdate-830) as cheese_batch_date from dual union all 
     select 2 as cheese_id, '201333' as cheese_year_seqno, 2 as cheese_lot, 3 as cheese_batch, 'CC' as cheese_vendor,trunc(sysdate-300) as cheese_batch_date from dual union all 
     select 2 as cheese_id, '201333' as cheese_year_seqno, 1 as cheese_lot, 1 as cheese_batch, 'AA' as cheese_vendor,trunc(sysdate-301) as cheese_batch_date from dual union all 
     select 2 as cheese_id, '201444' as cheese_year_seqno, 1 as cheese_lot, 1 as cheese_batch, 'DD' as cheese_vendor,trunc(sysdate-290) as cheese_batch_date from dual 
    ) 
select cheese_id, cheese_year_seqno, cheese_lot, cheese_batch, cheese_vendor, cheese_batch_date, 
     rank() over (partition by cheese_id 
         order by cheese_batch_date desc, 
           cheese_batch desc, 
           cheese_lot desc) as ch_rank1 
from (select cr.*, 
      sum(case when cheese_vendor = 'XXX' then 1 else 0 end) over (partition by cheese_year_seqno) as XXXFlag 
     from cheese_row 
    ) cr 
where XXXFlag = 0; 
1

과 같이 WHERE 절을 추가

where cheese_year_seqno NOT IN (
    select cheese_year_seqno from cheese_row where cheese_vendor = 'XX' 
) 
관련 문제