April 10, 2023 at 7:47 pm
Data looks like below
Status 1 is Active and 2 is Clean
For one Student ID we may have one active, one clean status;
I want to find the records only with Clean status for a student ID.
Student Id Student_Status
12345 1
12345 2
12345 1
49568 1
99889 2
57758 2
88898 1
88898 1
09598 2
09598 2
From the above query i need the records with student id (99889, 57758 and 09598) as these records has status clean.
April 10, 2023 at 8:20 pm
SELECT Student_ID
FROM dbo.table_name
GROUP BY Student_ID
HAVING MAX(CASE WHEN Student_Status = '2' THEN 1 ELSE 0 END) = 1 AND
MAX(CASE WHEN Student_Status <> '2' THEN 1 ELSE 0 END) = 0
--ORDER BY Student_ID
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
April 10, 2023 at 8:46 pm
ty so much , it worked
April 11, 2023 at 2:30 pm
SELECT Student_ID
FROM dbo.table_name
GROUP BY Student_ID
HAVING MAX(CASE WHEN Student_Status = '2' THEN 1 ELSE 0 END) = 1 AND
MAX(CASE WHEN Student_Status <> '2' THEN 1 ELSE 0 END) = 0
--ORDER BY Student_ID
I would have done the following.
SELECT Student_ID
FROM dbo.table_name
GROUP BY Student_ID
HAVING MIN(Student_Status) = '2'
/* If there are statuses greater than 2 uncomment the following line */
-- AND MAX(Student_Status) = '2'
--ORDER BY Student_ID
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
April 11, 2023 at 3:24 pm
ScottPletcher wrote:SELECT Student_ID
FROM dbo.table_name
GROUP BY Student_ID
HAVING MAX(CASE WHEN Student_Status = '2' THEN 1 ELSE 0 END) = 1 AND
MAX(CASE WHEN Student_Status <> '2' THEN 1 ELSE 0 END) = 0
--ORDER BY Student_IDI would have done the following.
SELECT Student_ID
FROM dbo.table_name
GROUP BY Student_ID
HAVING MIN(Student_Status) = '2'
/* If there are statuses greater than 2 uncomment the following line */
-- AND MAX(Student_Status) = '2'
--ORDER BY Student_IDDrew
I tend to write these things to allow any combination of values later, so I stick with individual checks. For example, suppose later they want to check for status 2 and status 1? Or (status 2 or status 1) and not status 3?
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
April 11, 2023 at 7:27 pm
I tend to write these things to allow any combination of values later, so I stick with individual checks. For example, suppose later they want to check for status 2 and status 1? Or (status 2 or status 1) and not status 3?
Heh... you beat me to it, Scott. I was just coming back to this to say "Really nicely done, Scott... especially since it's future-bullet-proof". It will also allow a conversion to IN and NOT IN if the requirements were to change to something like "can only have 2's and 3's", etc.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply