Link to home
Start Free TrialLog in
Avatar of Dale Fye
Dale FyeFlag for United States of America

asked on

Select records based on a passed parameter in the WHERE clause

I'm looking for the proper syntax to use in a WHERE clause.  I'm pulling data from a non-normalized table that is used for reporting purposes, and I only want to pull those records where the IgnoreXXX field for the particular product = 0.

The syntax below seems to be working properly, but I'm just looking for either confirmation that this would be the preferred syntax, or a better syntax if there is one.
SELECT P1.Prod_ID
, P1.Entity_ID
, P1.docDate 
, Vol = Case WHEN @Product = 'Gas' THEN P1.Gas
             WHEN @Product = 'Oil' THEN P1.Oil
             WHEN @Product = 'Water' THEN P1.Water
             Else NULL End
, Ignore = Case When @Product = 'Gas' THEN P1.IgnoreGas
             WHEN @Product = 'Oil' THEN P1.IgnoreOil
             WHEN @Product = 'Water' THEN P1.IgnoreWater
             ELSE NULL End
FROM tbl_sysProduction as P1
WHERE (CASE WHEN @Product = 'Gas' Then P1.IgnoreGas
            WHEN @Product = 'Oil' THEN P1.IgnoreOil
            WHEN @Product = 'Water' THEN P1.IgnoreWater
            ELSE 0 END) = 0

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dale Fye

ASKER

Thanks, Jim.

I like your version.