2017-05-31 58 views
0

我有一個相當大的查詢在DB2服務器上運行。在那個查詢中,我有一個包含20個選項的case語句。這個案例完美的工作,直到我試圖在每個when子句中放置一個子查詢。然後它給了我一個錯誤,我們不確定它發生在哪裏。如何在DB2選擇案例中包含來自不同表的選擇

Select SCDATA.SCCLNT.CCLNT, 
    (Select SCDATA.SCCLNT.CNAME From SCDATA.SCCLNT 
    Where SCDATA.SCCLNT.CLTGRP Like '231043' Fetch First 
    1 Rows Only) As ClientName, 
    CONCAT(TRIM(SCDATA.SCCLNT.CADD1), SCDATA.SCCLNT.CADD2) As Address1, 
    CONCAT(TRIM(SCDATA.SCCLNT.CCITY), CONCAT(', ', 
    CONCAT(TRIM(SCDATA.SCCLNT.CSTATE), CONCAT(' ',    
TRIM(SCDATA.SCCLNT.CZIP))))) 
    As Address2, 
    SCDATA.SCCLNT.CLTGRP As Group, 
    CONCAT((Select  
VARCHAR_FORMAT(Min(TIMESTAMP_FORMAT(Cast(SCDATA.SCACCT.GPLDAT 
    As Char(10)), 'YYYYMMDD')), 'MM/DD/YYYY') From SCDATA.SCACCT 
    Where SCDATA.SCACCT.GPLDAT Between 20170401 and 20170501), CONCAT(' -  ', 
    (Select VARCHAR_FORMAT(Max(TIMESTAMP_FORMAT(Cast(SCDATA.SCACCT.GPLDAT  As 
    Char(10)), 'YYYYMMDD')), 'MM/DD/YYYY') From SCDATA.SCACCT 
    Where SCDATA.SCACCT.GPLDAT Between 20170401 and 20170501))) As  dateRange, 
    SCDATA.SCACCT.GCLNT, 
    SCDATA.SCACCT.GPLACE, 
    SCDATA.SCACCT.GPLDAT, 
    SCDATA.SCACCT.GACCT# As GACCTnum, 
    CONCAT(TRIM(SCDATA.SCACCT.GLAST), CONCAT(', ',  TRIM(SCDATA.SCACCT.GFIRST))) As 
    Debtor, 
    (SCDATA.SCACCT.GPRI + SCDATA.SCACCT.GINT) as Balance, 
    SCDATA.SCACCT.Gdebt#, 
CASE 
    WHEN GDC20 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC20  FETCH TOP 1 ROWS) 
    WHEN GDC19 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC19 FETCH TOP 1 ROWS) 
    WHEN GDC18 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC18 FETCH TOP 1 ROWS) 
    WHEN GDC17 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC17  FETCH TOP 1 ROWS) 
    WHEN GDC16 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC16 FETCH TOP 1 ROWS) 
    WHEN GDC15 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC15 FETCH TOP 1 ROWS) 
    WHEN GDC14 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC14 FETCH TOP 1 ROWS) 
    WHEN GDC13 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC13 FETCH TOP 1 ROWS) 
    WHEN GDC12 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC12 FETCH TOP 1 ROWS) 
    WHEN GDC11 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC11 FETCH TOP 1 ROWS) 
    WHEN GDC10 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC10 FETCH TOP 1 ROWS) 
    WHEN GDC09 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC09 FETCH TOP 1 ROWS) 
    WHEN GDC08 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC08 FETCH TOP 1 ROWS) 
    WHEN GDC07 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC07 FETCH TOP 1 ROWS) 
    WHEN GDC06 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC06 FETCH TOP 1 ROWS) 
    WHEN GDC05 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC05 FETCH TOP 1 ROWS) 
    WHEN GDC04 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC04 FETCH TOP 1 ROWS) 
    WHEN GDC03 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC03 FETCH TOP 1 ROWS) 
    WHEN GDC02 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC02 FETCH TOP 1 ROWS) 
    WHEN GDC01 !='' THEN (Select SDDESC from SC0016 where SDCODE=GDC01 FETCH TOP 1 ROWS) 
    ELSE 'xx' 
END as Description 


From SCDATA.SCCLNT 
    inner Join SCDATA.SCACCT On SCDATA.SCACCT.GCLNT = SCDATA.SCCLNT.CCLNT  And 
    SCDATA.SCACCT.GPLDAT Between 20170401 and 20170501 
Where SCDATA.SCCLNT.CLTGRP Like '231043' 
Group By SCDATA.SCCLNT.CCLNT, 
    SCDATA.SCCLNT.CLTGRP, 
    SCDATA.SCCLNT.CNAME, 
    SCDATA.SCCLNT.CADD1, 
    SCDATA.SCCLNT.CADD2, 
    SCDATA.SCCLNT.CCITY, 
    SCDATA.SCCLNT.CSTATE, 
    SCDATA.SCCLNT.CZIP, 
    SCDATA.SCACCT.GFIRST, 
    SCDATA.SCACCT.GLAST, 
    SCDATA.SCACCT.GCLNT, 
    SCDATA.SCACCT.GPLACE, 
    SCDATA.SCACCT.GPLDAT, 
    SCDATA.SCACCT.GACCT#, 
SCDATA.SCACCT.gpri, 
SCDATA.SCACCT.GINT, 
SCDATA.SCACCT.GDEBT#, 
GDC20, 
GDC19, 
GDC18, 
GDC17, 
GDC16, 
GDC15, 
GDC14, 
GDC13, 
GDC12, 
GDC11, 
GDC10, 
GDC09, 
GDC08, 
GDC07, 
GDC06, 
GDC05, 
GDC04, 
GDC03, 
GDC02, 
GDC01 

此查詢背後的目的是爲了獲得在查詢結果中顯示排名最高的描述代碼,但該程序是內置,這樣的說明會陸續加入,所以我需要倒計時從20到1找到最近使用的代碼。這些代碼是一個簡單的2字符組合,對應於另一個需要提取信息的表。

問題是如何重新設計DB2中的case語句以將我需要的單個值從SC0016表中提取出來?

感謝

回答

0

的解決方案,事實證明,是把在連接語句中的選擇情況。所以在選擇部分我會列出我想要的領域,SDDESC,並在加入我 LEFT JOIN SC0016上SDCODE = 情況下,當GDC20!=「」然後GDC20 ......結束

這將使我基於第一個表中最高的填充列來連接每一行。不是很規範化,但我沒有寫入數據庫:)這工作。