Solved

Select records based on a passed parameter in the WHERE clause

Posted on 2014-09-06
2
116 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
Comment Utility
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)
Comment Utility
Thanks, Jim.

I like your version.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now