Solved

Select records based on a passed parameter in the WHERE clause

Posted on 2014-09-06
2
118 Views
Last Modified: 2014-09-07
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

0
Comment
Question by:Dale Fye (Access MVP)
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40308195
If that works for you then go for it, otherwise...

WHERE (@product = 'Gas' AND p1.IgnoreGas = 0) OR
   (@product='Oil' AND P1.IgnoreOir = 0) OR
   (@Product='Water AND P1.IgnoreWater = 0) OR
   (@Product NOT IN ('Gas', 'Oil', 'Water')
0
 
LVL 47

Author Closing Comment

by:Dale Fye (Access MVP)
ID: 40308423
Thanks, Jim.

I like your version.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question