Alter a SQL Case statement in Having Clause for an additional parameter.

I have the following Case  
HAVING  ( SUM(CONVERT(INT, CASE @OTSOVR
                             WHEN ( 'OTS' ) THEN ( cs.OTSPlan )
                             ELSE ( cs.OTSOpen )
                           END)) >  @MinQty )

Open in new window


I need to add a parameter that that will change the Operator from > to <

This way I can choose records less than or greater than  @MinQty
mburk1968Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dean ChafeeIT/InfoSec ManagerCommented:
Do you mean like passing a parameter to a stored procedure to change the operator? If so I think you would have to duplicate the block of code and test the parameter.  Something like:

if @Less = 1 HAVING  ( SUM(CONVERT(INT, CASE @OTSOVR
                             WHEN ( 'OTS' ) THEN ( cs.OTSPlan )
                             ELSE ( cs.OTSOpen )
                           END)) <  @MinQty )
Else
HAVING  ( SUM(CONVERT(INT, CASE @OTSOVR
                             WHEN ( 'OTS' ) THEN ( cs.OTSPlan )
                             ELSE ( cs.OTSOpen )
                           END)) >  @MinQty )
0
ste5anSenior DeveloperCommented:
First of all: CASE is an expression, not a statement.

E.g.

(@Switch=1 AND SUM(CONVERT(INT, CASE @OTSOVR WHEN ( 'OTS' ) THEN ( cs.OTSPlan ) ELSE ( cs.OTSOpen ) END)) >  @MinQty ))
OR
(@Switch=0 AND SUM(CONVERT(INT, CASE @OTSOVR WHEN ( 'OTS' ) THEN ( cs.OTSPlan ) ELSE ( cs.OTSOpen ) END)) <  @MinQty ))

Open in new window

0
mburk1968Author Commented:
Correct I need to change the HAVING in the following QUERY. I'm not certain as to how I work this into the query

DECLARE @OTSOVR CHAR(3);
DECLARE @MinQty INT;
DECLARE @MaxQty INT;
DECLARE @LessGreat CHAR(3)
DECLARE @Location CHAR(6);
SET @OTSOVR = 'OTS';
SET @MinQty = '6';
SET @MaxQty = '0';
SET @LessGreat = 'NEG'
SET @Location = 'PORT';


SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

SELECT DISTINCT
        cs.DIVISION ,
        LTRIM(RTRIM(cs.group_code4)) + '-' + LTRIM(RTRIM(cs.STYLE)) AS Scale_Style ,
        ISNULL(zzxexfvr.stor_file, ' ') AS Img_File_Name ,
        cs.Scale_Name ,
        cs.group_code4 ,
        cs.SeaSyrScale ,
        cs.STYLE ,
        cs.EndLocation ,
        SUM(CONVERT(INT, CASE @OTSOVR
                           WHEN ( 'OTS' ) THEN ( cs.OTSPlan )
                           ELSE ( cs.OTSOpen )
                         END)) AS OTS ,
        cs.COLOR_CODE ,
        cs.LBL_CODE ,
        CONVERT(DECIMAL(10, 2), cs.std_cost) AS std_cost ,
        cs.Size_Ind ,
        CONVERT(DECIMAL(10, 2), cs.a_price) AS std_price ,
        cs.Main_Label ,
        SUM(cs.AvailQOH) AS AvailQOH ,
        SUM(cs.QOH) AS QOH ,
        CASE WHEN ( EXISTS ( SELECT *
                             FROM   zzxrangd AS Rg
                             WHERE  ( Rg.style = cs.STYLE )
                                    AND ( Rg.color_code = cs.COLOR_CODE )
                                    AND ( Rg.lbl_code = cs.LBL_CODE ) ) )
             THEN ( 'Y' )
             ELSE ( 'N' )
        END AS RangeComponent ,
        cs.season ,
        cs.Classification ,
        dbo.KLLfn_Get_Max_ETAWhs_Date_Style_Color_Lbl_Scale(cs.STYLE,
                                                            cs.COLOR_CODE,
                                                            cs.LBL_CODE,
                                                            cs.group_code4) AS ETADate ,
        cs.Roll_Lbl ,
        cs.NeckLabel ,
        cs.OTSOpen ,
        cs.OTSPlan
FROM    ( SELECT    a_price ,
                    SUM(AvailQOH) AS AvailQOH ,
                    Classification ,
                    COLOR_CODE ,
                    DIVISION ,
                    group_code4 ,
                    LBL_CODE ,
                    Main_Label ,
                    NeckLabel ,
                    SUM(OTSOpen) AS OTSOpen ,
                    SUM(OTSPlan) AS OTSPlan ,
                    SUM(QOH) AS QOH ,
                    Scale_Name ,
                    season ,
                    SeaSyrScale ,
                    Size_Ind ,
                    std_cost ,
                    STYLE ,
                    EndLocation ,
                    MAX(Roll_Lbl) AS Roll_Lbl
          FROM      [KLL Cut and Sold OTS Sum - New Rollup]
          WHERE     ( EndLocation IN ( @Location ) )
          GROUP BY  a_price ,
                    Classification ,
                    COLOR_CODE ,
                    DIVISION ,
                    group_code4 ,
                    LBL_CODE ,
                    Main_Label ,
                    NeckLabel ,
                    Scale_Name ,
                    season ,
                    SeaSyrScale ,
                    Size_Ind ,
                    std_cost ,
                    STYLE ,
                    EndLocation
        ) AS cs
        INNER JOIN zzxstylr ON cs.DIVISION = zzxstylr.division
                               AND cs.STYLE = zzxstylr.style
        LEFT OUTER JOIN zzxexfvr ON zzxstylr.pkey = zzxexfvr.FKEY
                                    AND zzxexfvr.CONTEXTID = 'ZZXSTYLR'
                                    AND zzxexfvr.prime_image = 'Y'
WHERE   ( cs.DIVISION = 'KLL' )
GROUP BY cs.DIVISION ,
        LTRIM(RTRIM(cs.group_code4)) + '-' + LTRIM(RTRIM(cs.STYLE)) ,
        ISNULL(zzxexfvr.stor_file, ' ') ,
        cs.Scale_Name ,
        cs.group_code4 ,
        cs.SeaSyrScale ,
        cs.STYLE ,
        cs.EndLocation ,
        cs.COLOR_CODE ,
        cs.LBL_CODE ,
        CONVERT(DECIMAL(10, 2), cs.std_cost) ,
        cs.Size_Ind ,
        CONVERT(DECIMAL(10, 2), cs.a_price) ,
        cs.Main_Label ,
        cs.season ,
        cs.Classification ,
        dbo.KLLfn_Get_Max_ETAWhs_Date_Style_Color_Lbl_Scale(cs.STYLE,
                                                            cs.COLOR_CODE,
                                                            cs.LBL_CODE,
                                                            cs.group_code4) ,
        cs.Roll_Lbl ,
        cs.NeckLabel ,
        cs.OTSOpen ,
        cs.OTSPlan
HAVING  
--CASE GOES HERE

ORDER BY cs.Scale_Name ,
        cs.group_code4 ,
        cs.SeaSyrScale ,
        cs.STYLE ,
        cs.COLOR_CODE ,
        cs.LBL_CODE ,
        cs.Size_Ind ,
        cs.EndLocation;

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ste5anSenior DeveloperCommented:
Try my proposal. It should work.
0
mburk1968Author Commented:
Am I to place this inside a CASE?

(@Switch=1 AND SUM(CONVERT(INT, CASE @OTSOVR WHEN ( 'OTS' ) THEN ( cs.OTSPlan ) ELSE ( cs.OTSOpen ) END)) >  @MinQty ))
OR
(@Switch=0 AND SUM(CONVERT(INT, CASE @OTSOVR WHEN ( 'OTS' ) THEN ( cs.OTSPlan ) ELSE ( cs.OTSOpen ) END)) <  @MinQty ))

I cannot get this to work.
0
ste5anSenior DeveloperCommented:
Just after the HAVING. And remove the ending parentheses. They are wrong:

(@Switch=1 AND SUM(CONVERT(INT, CASE @OTSOVR WHEN ( 'OTS' ) THEN ( cs.OTSPlan ) ELSE ( cs.OTSOpen ) END)) >  @MinQty )
OR
(@Switch=0 AND SUM(CONVERT(INT, CASE @OTSOVR WHEN ( 'OTS' ) THEN ( cs.OTSPlan ) ELSE ( cs.OTSOpen ) END)) <  @MinQty )

Open in new window


p.s. use the CODE button to embed code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mburk1968Author Commented:
Thank you, remove the ending parentheses worked perfectly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.