Solved

Select records based on boolean parameter

Posted on 2013-02-04
8
256 Views
Last Modified: 2013-02-04
I have a query and I'm trying to add code so that if the parameter @Client is true then only select records with a Class of either 2 or 6.  Else If @Client is false then select All classes.  Would you show me what I'm doing wrong?


select	workdate, empId, L.LastName + ', ' + L.FirstName AS Name, ManagerUID, 
convert(varchar(5), dateadd(second, Total_WorkDate_Units, '0:00:00'),108) as HrsMin,
		CASE WHEN @Clients = -1 then L.Class IN (2, 6) END  
from	Employee_Work_Units_Summary s inner join dbo.EmployeeList L on s.empId = L.EmployeeId
where	Total_WorkDate_Units >  32400 AND (L.Suspend = 0)  and   (ManagerUID = ISNULL(NULLIF (@Supervisor, 0), ManagerUID))  AND (@Employee =0 or L.EmployeeId =@Employee) 
Order by workdate, Name]

Open in new window

0
Comment
Question by:BobRosas
[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
  • 4
  • 3
8 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 38852591
CASE WHEN @Clients = -1 then L.Class IN (2, 6) END  

should be:

Case WHEN @Clients = 1 then L.Class in (2,6) else L.Class in (select distinct class from <table>) END
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 38852593
SELECT blah, blah, blah
FROM YourTable
WHERE ( (l.class IN (2, 6) AND @client = True) OR @client = False) )
0
 

Author Comment

by:BobRosas
ID: 38852826
Thank you both for your quick response.  I'm still trying to get the code to work.  

ged325
I tried the following but I can't even get it to compile.  I don't know what I'm missing...

select	workdate, empId, L.LastName + ', ' + L.FirstName AS Name, ManagerUID, 
convert(varchar(5), dateadd(second, Total_WorkDate_Units, '0:00:00'),108) as HrsMin,
Case WHEN @Clients = 1 then L.Class in (2,6) else L.Class in (select distinct class from dbo.EmployeeList) END 
from	Employee_Work_Units_Summary s inner join dbo.EmployeeList L on s.empId = L.EmployeeId
where	Total_WorkDate_Units >  32400 AND (L.Suspend = 0)  and   (ManagerUID = ISNULL(NULLIF (@Supervisor, 0), 
ManagerUID))  AND (@Employee =0 or L.EmployeeId =@Employee) 
Order by workdate, Name

Open in new window



jim horn...
I tried this ...
AND( (L.class IN (2, 6) AND @Clients = -1) OR @Clients = 0)   

Open in new window

because I have @Clients as type bit and I got an invalid error using true.  But even with that change the result is not what I expect.  
EX:
Client     Class
101              5
102              3
103             4
104              1
105              2
106               6
107               4
108                3
109                 2
110                9

For the above data, when @Clients is true(-1) I want only 2's and 6's so I should have 3 records.
When @Clients = False (0) I want all records so I'm expecting 10 records.  That's why I'm trying to not filter by Class if @Clients is False.  Maybe that's not possilbe?
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 38852893
clients  is true = 1 . . . not -1

AND( (L.class IN (2, 6) AND @Clients = 1) OR @Clients = 0)
0
 

Author Comment

by:BobRosas
ID: 38853011
ged325
Thank you for your input.  I made that change but my data results did not change.  Should they?
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 250 total points
ID: 38853034
try the following:

select      workdate, empId, L.LastName + ', ' + L.FirstName AS Name, ManagerUID,
convert(varchar(5), dateadd(second, Total_WorkDate_Units, '0:00:00'),108) as HrsMin
            from      Employee_Work_Units_Summary s inner join dbo.EmployeeList L on s.empId = L.EmployeeId
where      Total_WorkDate_Units >  32400 AND (L.Suspend = 0)  and   (ManagerUID = ISNULL(NULLIF (@Supervisor, 0), ManagerUID))  AND (@Employee =0 or L.EmployeeId =@Employee)
AND( (L.class IN (2, 6) AND @Clients = 1) OR @Clients = 0)

Order by workdate, L.LastName + ', ' + L.FirstName
0
 

Author Comment

by:BobRosas
ID: 38853093
Thanks again!  I copied and pasted your code right in.  It compiles and runs but if I enter 1 (true) I don't get any results (I'm expecting 1 record).  If I enter 0 (false)  it excludes class 2 and 6 and I would like to include all classes for false.  I can't figure out what I'm missing but I do appreciate your help.
0
 

Author Comment

by:BobRosas
ID: 38853186
I found it!  I was comparing a report that's been working for a long time to a newly created Report Services report (which I thought was working).  But a circumstance that I didn't code for in report services just happen to be in my test data set and I didn't see it.  Once I ran and compared other data it looked good.  I apologize and thank you both so much. I will increase points and split them.  EE is awesome!
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

695 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