SQL查询IF语句

时间:2014-06-10 09:26:38

标签: sql

我试图在下面的sql查询中添加if else类型函数

SELECT        OEEL.invoicedt, UCASE(OEEL.whse) AS Whse, OEEL.orderno, OEEL.ordersuf, OEEL.custno, UCASE(OEEL.shipto) AS Shipto, UCASE(OEEL.slsrepin) AS Slsrepin, 
                     UCASE(OEEL.slsrepout) AS Slsrepout, OEEL.returnfl, OEEL.netamt, OEEL.wodiscamt, OEEL.discamtoth, OEEL.qtyship, OEEL.commcost, ICSS.csunperstk, UCASE(ICSD.name) AS Name, 
                     UCASE(ICSD.region) AS Region, UCASE(OEEL.prodcat) AS Prodcat, UCASE(SASTA.descrip) AS Descrip, UCASE(OEEL.transtype) AS Transtype, UCASE(ARSS.user2) AS User2, 
                     OEEL.transdt, ICSS.transdt AS "ICSS.Transdt", ICSD.transdt AS "ICSD.Transdt", SASTA.transdt AS "SASTA.Transdt", ARSS.transdt AS "ARSS.Transdt", { fn CURDATE() } AS CURDATE1, { fn CURTIME() } AS CURTIME2, 
                     OEEL.lineno

FROM            { oj { oj { oj { oj PUB.oeel OEEL LEFT OUTER JOIN
                         PUB.icss ICSS ON OEEL.cono = ICSS.cono AND OEEL.shipprod = ICSS.prod AND OEEL.icspecrecno = ICSS.icspecrecno } LEFT OUTER JOIN
                         PUB.icsd ICSD ON OEEL.cono = ICSD.cono AND OEEL.whse = ICSD.whse } LEFT OUTER JOIN
                         PUB.sasta SASTA ON OEEL.cono = SASTA.cono AND OEEL.prodcat = SASTA.codeval } LEFT OUTER JOIN
                         PUB.arss ARSS ON OEEL.cono = ARSS.cono AND OEEL.custno = ARSS.custno AND OEEL.shipto = ARSS.shipto }


WHERE        (OEEL.cono = 1) AND (OEEL.invoicedt BETWEEN { d '2014-06-02' } AND { d '2014-06-03' }) AND (SASTA.codeiden IN ('C', 'c')) 


ORDER By  OEEL.custno, OEEL.shipto, OEEL.prodcat

我想if是

if OEEL.returnfl = False THEN (OEEL.netamt-OEEL.wodiscamt-OEEL.discamtoth) AS "SALES"
Else (-1*(OEEL.netamt-OEEL.wodiscamt-OEEL.discamtoth)) AS "SALES"

1 个答案:

答案 0 :(得分:0)

您可以在查询中添加CASE语句

CASE WHEN OEEL.returnfl = 'False' THEN (OEEL.netamt-OEEL.wodiscamt-OEEL.discamtoth) ELSE (-1*(OEEL.netamt-OEEL.wodiscamt-OEEL.discamtoth)) END AS "SALES"