日期:2014-05-18  浏览次数:20555 次

有高手嗎????疑難的SQL語句問題,誰能輕鬆解答???????????
這是我寫的SQL語句:
SELECT   de09,   de02,   de03,   de04,   de05,   de06,   de07,   de08,   ce05,   da02,   da03,   da24,   ce06=   (  
        CASE  
WHEN     da24   =   ce12   then   ce06  
WHEN     ce12   =   da14   then     ce06/da13
WHEN   ce12   =   da16   then   ce06/da15    
WHEN   ce12   =   da25   then   ce06   *   da26/da27    
WHEN   ce12   =   da28   then     ce06*   da29/da30    
ELSE     ce06*   da32/da33  
END)   ,   ca03   =   (SELECT   ca03   FROM   tcm01   WHERE   de05   =   ca01)  
                FROM     tdm05   LEFT   JOIN   tcm05   ON   de02   =   ce02   AND   de05   =   ce011  
                                                          LEFT   JOIN   tdm01   ON   de02   =   da01  
                                                          WHERE   de01   =     '12377777777777777777 '       ORDER   BY   de02
想在ce06/da13,ce06/da15,ce06   *   da26/da27等處做個判斷被除數da13,da15,da27等數不可為零
怎麼做呢?等待高手解答!

------解决方案--------------------
SELECT de09, de02, de03, de04, de05, de06, de07, de08, ce05, da02, da03, da24, ce06= (
CASE
WHEN da24 = ce12 then ce06
WHEN ce12 = da14 and da13 > 0 then ce06/da13
WHEN ce12 = da16 and da15 > 0 then ce06/da15
WHEN ce12 = da25 and da27 > 0then ce06 * da26/da27
WHEN ce12 = da28 and da30 > 0 then ce06* da29/da30
ELSE ce06* da32/da33
END) ,
ca03 = (SELECT ca03 FROM tcm01 WHERE de05 = ca01)
FROM tdm05 LEFT JOIN tcm05 ON de02 = ce02 AND de05 = ce011 LEFT JOIN tdm01 ON de02 = da01
WHERE de01 = '12377777777777777777 ' ORDER BY de02


--这样看看
------解决方案--------------------
加一个case when?