Solved

Filtering on a column name created with CASE statement

Posted on 2008-06-23
6
332 Views
Last Modified: 2010-04-21
I am using a case statement.  I assign the values to column defined in the case statement.  I want to filter on the values in that column.    I cannot seem to get the correct syntax to do this with the WHERE statement.  Or maybe I am just way off on how I approaching this problem.
-- Find Employees who's PTO max accural needs to adjusted.

DECLARE @MAX_PTO nvarchar (5)

SET @MAX_PTO = 'False'

SELECT  

	AccrualRules.RuleId, 

	Description, 

	EmployeeAccrualRules.EmployeeId, 

	FirstName, 

	LastName, 

	AccrualCapHours,

	CONVERT(varchar, DateHire, 101) AS 'Hire Date', 

	CONVERT(varchar, GetDate(), 101) As 'Current Date',

	DATEDIFF(m,DateHire, GetDate()) AS 'Number of Months',

	Class,

	Suspend,

	DateLeft,
 

 'Descrepancy' =

CASE 

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 10 AND 12 AND AccrualCapHours < 120 AND AccrualRules.RuleId = 1 THEN 0	-- Hourly & Admin

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 13 AND 48 AND AccrualCapHours < 176 AND AccrualRules.RuleId = 2 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 49 AND 108 AND AccrualCapHours < 216 AND AccrualRules.RuleId = 3 THEN 0
 

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 7 AND 12 AND AccrualCapHours < 136 AND AccrualRules.RuleId = 5 THEN 0		-- Management

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 13 AND 48 AND AccrualCapHours < 192 AND AccrualRules.RuleId = 6 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 49 AND 108 AND AccrualCapHours < 224 AND AccrualRules.RuleId = 7 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 109 AND 168 AND AccrualCapHours < 248 AND AccrualRules.RuleId = 8 THEN 0
 

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 7 AND 12 AND AccrualCapHours < 136 AND AccrualRules.RuleId = 10 THEN 0		-- Executive

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 13 AND 48 AND AccrualCapHours < 192 AND AccrualRules.RuleId = 11 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 49 AND 84 AND AccrualCapHours < 224 AND AccrualRules.RuleId = 12 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 85 AND 120 AND AccrualCapHours < 248 AND AccrualRules.RuleId = 13 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 121 AND 156 AND AccrualCapHours < 256 AND AccrualRules.RuleId = 14 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 127 AND 192 AND AccrualCapHours < 264 AND AccrualRules.RuleId = 15 THEN 0
 
 

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 10 AND 12 AND AccrualCapHours < 120 AND AccrualRules.RuleId = 17 THEN 0	-- Hourly & Admin (Non Clocking)

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 13 AND 48 AND AccrualCapHours < 176 AND AccrualRules.RuleId = 18 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 49 AND 108 AND AccrualCapHours < 216 AND AccrualRules.RuleId = 19 THEN 0
 

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 7 AND 12 AND AccrualCapHours < 136 AND AccrualRules.RuleId = 21 THEN 0		-- Management (Non Clocking)

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 13 AND 48 AND AccrualCapHours < 192 AND AccrualRules.RuleId = 22 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 49 AND 108 AND AccrualCapHours < 224 AND AccrualRules.RuleId = 23 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 109 AND 168 AND AccrualCapHours < 248 AND AccrualRules.RuleId = 24 THEN 0
 

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 7 AND 12 AND AccrualCapHours < 136 AND AccrualRules.RuleId = 26 THEN 0		-- Executive (Non Clocking)

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 13 AND 48 AND AccrualCapHours < 192 AND AccrualRules.RuleId = 27 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 49 AND 84 AND AccrualCapHours < 224 AND AccrualRules.RuleId = 28 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 85 AND 120 AND AccrualCapHours < 248 AND AccrualRules.RuleId = 29 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 121 AND 156 AND AccrualCapHours < 256 AND AccrualRules.RuleId = 30 THEN 0

	WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 127 AND 192 AND AccrualCapHours < 264 AND AccrualRules.RuleId = 31 THEN 0
 

END 
 
 

FROM dbo.AccrualRules 
 

INNER JOIN  dbo.EmployeeAccrualRules ON dbo.AccrualRules.RuleId = dbo.EmployeeAccrualRules.RuleId

INNER JOIN  dbo.EmployeeList ON dbo.EmployeeAccrualRules.EmployeeId = dbo.EmployeeList.EmployeeId

INNER JOIN  dbo.EmployeeJobCodes ON dbo.EmployeeList.EmployeeId = dbo.EmployeeJobCodes.EmployeeId
 

--WHERE  EmployeeJobCodes.JobCode = 5 AND Suspend = 0  AND DateLeft IS NULL AND(Class = 1 OR Class = 2 OR Class = 6)

WHERE [Descrepancy] = 0

ORDER BY [Descrepancy] ASC

Open in new window

0
Comment
Question by:BobRosas
  • 3
  • 3
6 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21851522
You just have to wrap another SELECT around it:

SELECT * FROM
(
SELECT  
      AccrualRules.RuleId,
      Description,
      EmployeeAccrualRules.EmployeeId,
      FirstName,
      LastName,
      AccrualCapHours,
      CONVERT(varchar, DateHire, 101) AS 'Hire Date',
      CONVERT(varchar, GetDate(), 101) As 'Current Date',
      DATEDIFF(m,DateHire, GetDate()) AS 'Number of Months',
      Class,
      Suspend,
      DateLeft,
 
 'Descrepancy' =
CASE
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 10 AND 12 AND AccrualCapHours < 120 AND AccrualRules.RuleId = 1 THEN 0      -- Hourly & Admin
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 13 AND 48 AND AccrualCapHours < 176 AND AccrualRules.RuleId = 2 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 49 AND 108 AND AccrualCapHours < 216 AND AccrualRules.RuleId = 3 THEN 0
 
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 7 AND 12 AND AccrualCapHours < 136 AND AccrualRules.RuleId = 5 THEN 0            -- Management
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 13 AND 48 AND AccrualCapHours < 192 AND AccrualRules.RuleId = 6 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 49 AND 108 AND AccrualCapHours < 224 AND AccrualRules.RuleId = 7 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 109 AND 168 AND AccrualCapHours < 248 AND AccrualRules.RuleId = 8 THEN 0
 
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 7 AND 12 AND AccrualCapHours < 136 AND AccrualRules.RuleId = 10 THEN 0            -- Executive
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 13 AND 48 AND AccrualCapHours < 192 AND AccrualRules.RuleId = 11 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 49 AND 84 AND AccrualCapHours < 224 AND AccrualRules.RuleId = 12 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 85 AND 120 AND AccrualCapHours < 248 AND AccrualRules.RuleId = 13 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 121 AND 156 AND AccrualCapHours < 256 AND AccrualRules.RuleId = 14 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 127 AND 192 AND AccrualCapHours < 264 AND AccrualRules.RuleId = 15 THEN 0
 
 
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 10 AND 12 AND AccrualCapHours < 120 AND AccrualRules.RuleId = 17 THEN 0      -- Hourly & Admin (Non Clocking)
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 13 AND 48 AND AccrualCapHours < 176 AND AccrualRules.RuleId = 18 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 49 AND 108 AND AccrualCapHours < 216 AND AccrualRules.RuleId = 19 THEN 0
 
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 7 AND 12 AND AccrualCapHours < 136 AND AccrualRules.RuleId = 21 THEN 0            -- Management (Non Clocking)
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 13 AND 48 AND AccrualCapHours < 192 AND AccrualRules.RuleId = 22 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 49 AND 108 AND AccrualCapHours < 224 AND AccrualRules.RuleId = 23 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 109 AND 168 AND AccrualCapHours < 248 AND AccrualRules.RuleId = 24 THEN 0
 
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 7 AND 12 AND AccrualCapHours < 136 AND AccrualRules.RuleId = 26 THEN 0            -- Executive (Non Clocking)
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 13 AND 48 AND AccrualCapHours < 192 AND AccrualRules.RuleId = 27 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 49 AND 84 AND AccrualCapHours < 224 AND AccrualRules.RuleId = 28 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 85 AND 120 AND AccrualCapHours < 248 AND AccrualRules.RuleId = 29 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 121 AND 156 AND AccrualCapHours < 256 AND AccrualRules.RuleId = 30 THEN 0
      WHEN  DATEDIFF(m,DateHire, GetDate())BETWEEN 127 AND 192 AND AccrualCapHours < 264 AND AccrualRules.RuleId = 31 THEN 0
 
END
 
 
FROM dbo.AccrualRules
 
INNER JOIN  dbo.EmployeeAccrualRules ON dbo.AccrualRules.RuleId = dbo.EmployeeAccrualRules.RuleId
INNER JOIN  dbo.EmployeeList ON dbo.EmployeeAccrualRules.EmployeeId = dbo.EmployeeList.EmployeeId
INNER JOIN  dbo.EmployeeJobCodes ON dbo.EmployeeList.EmployeeId = dbo.EmployeeJobCodes.EmployeeId
) a

WHERE [Descrepancy] = 0
ORDER BY [Descrepancy] ASC
0
 

Author Comment

by:BobRosas
ID: 21858298
THANK YOU!!!  That worked perfectly - just so I understand - why did I need the extra SELECT statement?

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21858318
I used your first statement as a "virtual table" so to speak.  So, that query runs and returns results that are used by the outer query.  In the outer query, since the value from the case statement has already been returned, I can set criteria on it and filter it as needed.  I could have done it in the original SELECT statement, but I would have had to recreate the entire case statement again in the where clause to do so...so this is more eloquent..and should be faster too.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:BobRosas
ID: 21858359
Your awesome!!!!!  Thank you very much.  I have been frustrated by this for days!!  
Thanks!!
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21858370
You're very welcome.
0
 

Author Closing Comment

by:BobRosas
ID: 31470004
I thought I already awarded points.  Thanks again.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SRSS - IF statements in Report Builder 3.0 to sum of number of items in each order 8 31
tempdb latch contention 12 48
SQL Select JOIN table 2 19
SQL Login 17 38
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

910 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

24 Experts available now in Live!

Get 1:1 Help Now