2016-03-07 84 views
-1

你好我有查詢如下 這個執行時間很長。 請向我介紹了嘗試不同的解決方案 。還有我的查詢(Oracle 10g中)查詢沒有聯盟關鍵選擇

SELECT * 
    FROM (WITH T AS (SELECT COR_ID, COR_COD, WEI1, DAYS, 'WEI1' WEI_TYPE 
        FROM (TABLE)) 
     SELECT * 
      FROM T PIVOT(MAX(WEI1) FOR(DAYS) IN('01' DAY_01, 
               '02' DAY_02, 
               '03' DAY_03, 
               '04' DAY_04, 
               '05' DAY_05, 
               '06' DAY_06, 
               '07' DAY_07, 
               '08' DAY_08))) 
UNION 
    FROM (WITH T AS (SELECT COR_ID, COR_COD, WEI2, DAYS, 'WEI2' WEI_TYPE 
        FROM (TABLE)) 
     SELECT * 
      FROM T PIVOT(MAX(WEI2) FOR(DAYS) IN('01' DAY_01, 
               '02' DAY_02, 
               '03' DAY_03, 
               '04' DAY_04, 
               '05' DAY_05, 
               '06' DAY_06, 
               '07' DAY_07, 
               '08' DAY_08))) 
UNION 
    FROM (WITH T AS (SELECT COR_ID, COR_COD, WEI3, DAYS, 'WEI3' WEI_TYPE 
        FROM (TABLE)) 
     SELECT * 
      FROM T PIVOT(MAX(WEI3) FOR(DAYS) IN('01' DAY_01, 
               '02' DAY_02, 
               '03' DAY_03, 
               '04' DAY_04, 
               '05' DAY_05, 
               '06' DAY_06, 
               '07' DAY_07, 
               '08' DAY_08))) 


沒有工會這個查詢如何改變?從這個導致
that result from this:

這樣: to this:
請幫我

回答

0

您有一個表與幾個衛列和不同行的日子。而你希望得到一個結果,其中包含幾個日期列,併爲weis分隔行。所以這是一個轉換,行到列列到行。

您的查詢看起來適合於此任務。只有,您正在使用UNION它應該是UNION ALLUNION ALL將記錄粘合在一起,這就是你想要的。 UNION做同樣的事情,但也尋找重複刪除。但是,您的三個部分查詢不會生成重複項 - 它們至少在wei_type之間有所不同。將UNION更改爲UNION ALL,因此DBMS不必執行額外的工作,這會導致查詢速度稍快。

這是一個替代查詢,既不使用UNION也不使用UNION ALL在您的表上。它與weis交叉連接以產生這些行,然後聚集/樞軸從日行到列。

select 
    cor_id, cor_cod, wei_type, day_01, day_02, day_03, day_04, day_05, day_06, day_07, day_08 
from 
(
    select 
    t.cor_id, t.cor_cod, t.days, w.wei_type, 
    case w.wei_type when 'WEI1' then t.wei1 when 'WEI2' then t.wei2 else t.wei3 end as wei 
    from thetable t 
    cross join 
    (
    select 'WEI1' as wei_type from dual 
    union all 
    select 'WEI2' as wei_type from dual 
    union all 
    select 'WEI3' as wei_type from dual 
) w 
) 
pivot 
(
    max(wei) for(days) in 
    (
    '01' day_01, '02' day_02, '03' day_03, '04' day_04, 
    '05' day_05, '06' day_06, '07' day_07, '08' day_08 
    ) 
) 
order by cor_id, cor_cod, wei_type; 

這裏的條件聚合代替PIVOT也是一樣的。

select 
    cor_id, cor_cod, wei_type, 
    max(case when days = '01' then wei end) as day_01, 
    max(case when days = '02' then wei end) as day_02, 
    max(case when days = '03' then wei end) as day_03, 
    max(case when days = '04' then wei end) as day_04, 
    max(case when days = '05' then wei end) as day_05, 
    max(case when days = '06' then wei end) as day_06, 
    max(case when days = '07' then wei end) as day_07, 
    max(case when days = '08' then wei end) as day_08 
from 
(
    select 
    t.cor_id, t.cor_cod, t.days, w.wei_type, 
    case w.wei_type when 'WEI1' then t.wei1 when 'WEI2' then t.wei2 else t.wei3 end as wei 
    from thetable t 
    cross join 
    (
    select 'WEI1' as wei_type from dual 
    union all 
    select 'WEI2' as wei_type from dual 
    union all 
    select 'WEI3' as wei_type from dual 
) w 
) 
group by cor_id, cor_cod, wei_type 
order by cor_id, cor_cod, wei_type; 
+0

哦很好[Thorsten Kettner],非常好,非常感謝... –