Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Select records based on a passed parameter in the WHERE clause

Posted on 2014-09-06
2
Medium Priority
?
123 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 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 48

Author Closing Comment

by:Dale Fye
ID: 40308423
Thanks, Jim.

I like your version.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

730 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