2014-09-26 58 views
0

這裏是我的情況:如何從分析查詢中排除選擇分區?

對於ID = 1,cheese_year_seqno的,其行有XX供應商代碼,所以我想 到exclue所有的SeqNo但keept的行可用於排名。 如果在給定的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' 
)