如果:parameter不为null,则P.PRODUCT_NAME =:parameter否则:parameter如'seat%'

时间:2019-12-26 12:43:44

标签: sql oracle

SELECT PR.PLAN_NAME  ,
  SUM(CASE WHEN EXTRACT(MONTH FROM I.FROM_DATE) = 1 THEN I.QUANTITY ELSE NULL END)  JAN,
  SUM(CASE WHEN EXTRACT(MONTH FROM I.FROM_DATE) = 2 THEN I.QUANTITY ELSE NULL END)  FEB,
  SUM(CASE WHEN EXTRACT(MONTH FROM I.FROM_DATE) = 3 THEN I.QUANTITY ELSE NULL END)  MAR,
  SUM(CASE WHEN EXTRACT(MONTH FROM I.FROM_DATE) = 4 THEN I.QUANTITY ELSE NULL END)  APR,
  SUM(CASE WHEN EXTRACT(MONTH FROM I.FROM_DATE) = 5 THEN I.QUANTITY ELSE NULL END)  MAY,
  SUM(CASE WHEN EXTRACT(MONTH FROM I.FROM_DATE) = 6 THEN I.QUANTITY ELSE NULL END)  JUN,
  SUM(CASE WHEN EXTRACT(MONTH FROM I.FROM_DATE) = 7 THEN I.QUANTITY ELSE NULL END)  JUL,
  SUM(CASE WHEN EXTRACT(MONTH FROM I.FROM_DATE) = 8 THEN I.QUANTITY ELSE NULL END)  AUG,
  SUM(CASE WHEN EXTRACT(MONTH FROM I.FROM_DATE) = 9 THEN I.QUANTITY ELSE NULL END)  SEP,
  SUM(CASE WHEN EXTRACT(MONTH FROM I.FROM_DATE) = 10 THEN I.QUANTITY ELSE NULL END) OCT,
  SUM(CASE WHEN EXTRACT(MONTH FROM I.FROM_DATE) = 11 THEN I.QUANTITY ELSE NULL END) NOV,
  SUM(CASE WHEN EXTRACT(MONTH FROM I.FROM_DATE) = 12 THEN I.QUANTITY ELSE NULL END) DEC,
  F.PLAN_CODE , PR.PARTNER_CODE
FROM BILLZ.PRICE_PLANS PR
  INNER JOIN BILLZ.FIXED_CHARGES F ON (PR.PLAN_CODE = F.PLAN_CODE)
  INNER JOIN  BILLZ.PRODUCTS P     ON (F.PRODUCT_CODE = P.PRODUCT_CODE)
  INNER JOIN BILLZ.INVOICE_DETAILS I   ON (P.PRODUCT_CODE = I.PRODUCT_CODE)
  INNER JOIN BILLZ.INVOICES INV     ON (I.INVOICE_NUMBER = INV.INVOICE_NUMBER AND INV.INVOICE_STATE = 'CONFIRMED')
  INNER JOIN BILLZ.ACCOUNT_SERVICES  AC   ON (INV.ACCOUNT_NUMBER = AC.ACCOUNT_NUMBER AND AC.PLAN_CODE = F.PLAN_CODE)
    WHERE  
           CASE P.PRODUCT_NAME WHEN  :parameter  THEN 1 

            ELSE 
                (CASE  WHEN P.PRODUCT_NAME LIKE 'SEAT%' THEN 1 ELSE 0 END)
            END = 1
    AND  PR.PLAN_NAME IN ( 'CUSTOMER-CAN','CUSTOMER-USA','Reseller-CAN','Reseller-USA','Salesdrive WL')
    AND EXTRACT(YEAR FROM I.FROM_DATE) = '2020'
    AND  F.EFFECT_DATE = (select /*+ INDEX(FXCi, FIXED_CHARGES_UK1) */ MAX(FXCi.EFFECT_DATE)
                             FROM FIXED_CHARGES FXCi
                             WHERE     FXCi.PLAN_CODE = F.PLAN_CODE
                             AND FXCi.PRODUCT_CODE = F.PRODUCT_CODE
                             AND FXCi.EFFECT_DATE <= SYSDATE)`enter code here`
    GROUP BY (PR.PLAN_NAME,F.PLAN_CODE,PR.PARTNER_CODE);

如果不为空,我该如何检查:参数P.PRODUCT_NAME的位置=:参数P.PRODUCT_NAME的其他选择,如'SEAT:'

检查参数

1 个答案:

答案 0 :(得分:2)

您可以按照以下步骤进行操作:

WHERE (:PARAMETER IS NOT NULL AND P.PRODUCT_NAME = :PARAMETER) OR
      (:PARAMETER IS NULL AND P.PRODUCT_NAME LIKE 'SEAT:')

这等效于:

WHERE P.PRODUCT_NAME = COALESCE(:PARAMETER, 'SEAT:')