SQL减去别名并创建另一个

时间:2014-06-10 11:59:33

标签: sql

我想减去"别名"我创建了名为" SALES" &安培; " GP"并创建一个新的列调用GP

 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,
        CASE
            WHEN OEEL.returnfl = '0' THEN (OEEL.netamt-OEEL.wodiscamt-OEEL.discamtoth)
            ELSE (-1*(OEEL.netamt-OEEL.wodiscamt-OEEL.discamtoth))
        END AS "SALES",
        CASE
            WHEN OEEL.returnfl = '0' THEN (OEEL.qtyship*OEEL.commcost*NVL(ICSS.csunperstk,1))
            ELSE (-1*OEEL.qtyship*OEEL.commcost*NVL(ICSS.csunperstk,1))
        END AS "COST"
   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

1 个答案:

答案 0 :(得分:0)

在最后一栏添加以下内容

(CASE WHEN OEEL.returnfl = '0' THEN (OEEL.netamt-OEEL.wodiscamt-OEEL.discamtoth) ELSE (-1*(OEEL.netamt-OEEL.wodiscamt-OEEL.discamtoth)) END) - (CASE WHEN OEEL.returnfl = '0' THEN (OEEL.qtyship*OEEL.commcost*NVL(ICSS.csunperstk,1)) ELSE (-1*OEEL.qtyship*OEEL.commcost*NVL(ICSS.csunperstk,1)) END) AS GP