DECODE or CASE STATEMENT for PARAMETER in WHERE CLAUSE [closed]

Posted on

Question :

I am having an issue where I need to make 1 parameter have multiple options, almost like a case statement or decode function.
Here is a snippit of the query below, We were using 2 parameters so he can have either or, or use a wildcard but we want to narrow it down to 1 parameter.

What I am looking for is.. if he where to choose '%' for the &industry parameter, it will show spc.characteristic_code in ('ARLFT','MINE')
other wise he would put either 'MINE' or 'ARLFT' in the &Industry parameter.

If I need to clarify, please advise. Thanks in advance!

  SELECT INV.COUNTRY
    , INV.NAME
    , INV.STATE
    , INV.CUSTOMER_ID
    , INV.CUST_GRP AS DIVISION
    , INV.DISTRICT_CODE 
    , INV.SLSPRT
    , SP.CATALOG_DESC
    , SPC.CHARACTERISTIC_CODE  
    , SUM(INV.QUANTITY) QTY
    , SUM(INV.SALES) SALES
    , SUM(INV.COST) COSTS
    , SUM(INV.SALES - INV.COST) AS MARGIN
    FROM IFSINFO.HB_INVOICING_ALL INV 
        JOIN IFSAPP.SALES_PART_CHARACTERISTIC SPC 
            ON (INV.SITE = SPC.CONTRACT) 
            AND (INV.SLSPRT = SPC.CATALOG_NO)
        JOIN IFSAPP.SALES_PART SP 
            ON (INV.SITE = SP.CONTRACT) 
            AND (INV.SLSPRT = SP.CATALOG_NO)
    WHERE (TO_DATE(INV.INVDATE) 
        BETWEEN TO_DATE('&Start_Date','mm/dd/yyyy') 
        AND TO_DATE('&End_Date','mm/dd/yyyy')
        OR  TO_DATE(INV.INVDATE)
        BETWEEN TO_DATE('&Start_Date2','mm/dd/yyyy') 
        AND TO_DATE('&End_Date2','mm/dd/yyyy'))
    AND INV.SITE = '&Site'
    AND INV.CUST_GRP like '&Cust_Grp'
    AND SPC.CHARACTERISTIC_CODE like '&Industry'
    GROUP BY INV.COUNTRY
    , INV.NAME
    , INV.STATE
    , INV.CUSTOMER_ID
    , INV.CUST_GRP
    , INV.DISTRICT_CODE 
    , INV.SLSPRT
    , SP.CATALOG_DESC
    , SPC.CHARACTERISTIC_CODE

Answer :

I don’t have a database on hand to test this but can’t you do something like this:

and SPC.CHARACTERISTIC in ('ARLFT','MINE')
and SPC.CHARACTERISTIC in ('&industry')

With both of those predicates present you will default to both values if you enter '%' but be restricted to just one if you enter either 'MINE' or 'ARLFT'

Leave a Reply

Your email address will not be published. Required fields are marked *