在Oracle SQL中使用IF语句

时间:2014-11-03 20:30:31

标签: oracle

我正在尝试在SQL查询中使用IF ELSE语句,因为我想根据每个PLN#的包大小来计算每个区的单位。我可以这样做吗?这是我的尝试,但不起作用:

      SELECT district_nbr,PLN_NBR, SUM(A.LO_IOH_UNITS) AS TOTAL_UNITS, SUM (A.LO_IOH_EXT_COST_DLRS) AS TOTAL_DOLLARS
FROM FCT_LOIOH_DAY_STR_PLN A, DIM_PROD_PLN B, DIM_LOCATION C
WHERE B.PLN_NBR IN(40000683181, 40000418723, 40000335776)
AND A.PROD_ID = B.PROD_ID
AND A.LOC_ID = C.LOC_ID
if PLN_NBR = '40000683181' then SUM(A.LO_IOH_UNITS)/25
else SUM(A.LO_IOH_UNITS)/60

GROUP BY district_nbr,PLN_NBR

2 个答案:

答案 0 :(得分:0)

在Oracle SQL中,您可以使用CASE WHEN ... ELSE作为条件:

CASE WHEN PLN_NBR = '40000683181' THEN SUM(A.LO_IOH_UNITS) / 25
                                  ELSE SUM)A.LO_IOH_UNITS) / 60 
END

此类CASE WHEN ...语句转到其他所选列的部分:

SELECT
  foo,
  bar,
  CASE ....
FROM
   ...

答案 1 :(得分:0)

SELECT district_nbr, PLN_NBR, 
       SUM(A.LO_IOH_UNITS) / (case when PLN_NBR = 40000683181 then 25 else 60 end) AS TOTAL_UNITS, 
       SUM (A.LO_IOH_EXT_COST_DLRS) AS TOTAL_DOLLARS
FROM FCT_LOIOH_DAY_STR_PLN A, 
     DIM_PROD_PLN B, 
     DIM_LOCATION C
WHERE B.PLN_NBR IN (40000683181, 40000418723, 40000335776)
  AND A.PROD_ID = B.PROD_ID
  AND A.LOC_ID = C.LOC_ID  
GROUP BY district_nbr, PLN_NBR;