2011-04-20 36 views
0

你好,我試圖加入另一個連接表後的表。我的預期產出是讓所有的代碼CODE1,CODE2和CODE3顯示,就像下表:如何在另一個連接之後加入表格?

channel_division_group staff_id the_code total update_dt 
---------------------- -------- -------- ----- --------- 
CH3     101  CODE1 1  03-Mar-11 
CH3     101  CODE1 1  03-Mar-11 
CH3     101  CODE2 1  03-Mar-11 
CH3     101  CODE3 1  03-Mar-11 

但實際結果與CODE3行不見了:

channel_division_group staff_id the_code total update_dt 
---------------------- -------- -------- ----- --------- 
CH3     101  CODE1 1  03-Mar-11 
CH3     101  CODE1 1  03-Mar-11 
CH3     101  CODE2 1  03-Mar-11 

這是給你參考源代碼:

select channel_division_group, staff_id, the_code, total, update_dt 
    from (
     select 'CODE1' the_code from dual 
     union all 
     select 'CODE2' the_code from dual 
     union all 
     select 'CODE3' the_code from dual 
    ) 
    left outer join (
     select a.staff_id, a.code, update_dt, 
      case when m.update_dt is null then 0 
         else count(*) 
      end total, 
      case a.channel_division_group 
       when 'CH1' then 'CH1' 
       when 'CH2' then 'CH2' 
       else 'CH3' 
      end tableC 
     from (
      select code, staff.staff_id, staff.channel_division_group 
      from (
       select 'CODE1' code, '1' seq from dual 
       union all 
       select 'CODE2' code, '2' seq from dual 
       union all 
       select 'CODE3' code, '3' seq from dual 
      ), code_staff staff 
     ) a 
     left outer join tableM m 
     on a.code = m.decision and to_char(a.staff_id)=m.approval_id 
     group by a.staff_id, a.code, update_dt, a.channel_division_group 
     order by a.channel_division_group, a.staff_id 
    ) app 
    on the_code=app.code and staff_id=app.staff_id 
    where update_dt between trunc(to_date('13-MAR-11'), 'MONTH') and trunc(to_date('13-MAR-11')) 
    group by channel_division_group, staff_id, the_code, total, update_dt 
    order by staff_id; 

如果我刪除where子句的說法,CODE3將顯示但這並不日期內進行過濾。當一個連接與where子句連接在一起時,這可以完成嗎?

THanks @!

回答

1

你要複雜的加入如下所示的使用「with statement」來進行sql連接。

with 
    temp_t1 as (
       select dummy c1 from dual 
      ) 
    ,temp_t2 as (
       select dummy c1 from dual 
      ) 
select * 
from temp_t1 a 
    ,temp_t2 b 

它幫助你。

相關問題