December 7, 2022 at 8:30 pm
Hi, I have a table containing some columns and some simple data. I want to select from the table by specifying the JobDomain and if there is a TestType = 'B', then just return that record. If there isn't a TestType = 'B', the return all records for that JobDomain
So, if JobDomain = 1, I just want to return the row where JobDomain = 1, TestType = 'B' and JobIndex = 126
If the JobDomain = 2, I want to return the 4 rows where JobDomain = 2, testType = 'A' and JobIndex, 123, 124, 125, 127
How do I do this?
CREATE TABLE TestTable(JobDomain int, TestType varchar(5), JobIndex int)
INSERT INTO TestTable(JobDomain, TestType, JobIndex) VALUES (1, 'A', 123)
INSERT INTO TestTable(JobDomain, TestType, JobIndex) VALUES (1, 'A', 124)
INSERT INTO TestTable(JobDomain, TestType, JobIndex) VALUES (1, 'A', 125)
INSERT INTO TestTable(JobDomain, TestType, JobIndex) VALUES (1, 'B', 126)
INSERT INTO TestTable(JobDomain, TestType, JobIndex) VALUES (1, 'A', 127)
INSERT INTO TestTable(JobDomain, TestType, JobIndex) VALUES (2, 'A', 123)
INSERT INTO TestTable(JobDomain, TestType, JobIndex) VALUES (2, 'A', 124)
INSERT INTO TestTable(JobDomain, TestType, JobIndex) VALUES (2, 'A', 125)
INSERT INTO TestTable(JobDomain, TestType, JobIndex) VALUES (2, 'A', 127)
December 8, 2022 at 1:09 am
Here's a quick way:
DECLARE @JobDomain int
SET @JobDomain = 1
;WITH cte_Type_B AS (
SELECT *
FROM dbo.TestTable
WHERE JobDomain = @JobDomain AND TestType = 'B'
),
cte_Type_Other AS (
SELECT *
FROM dbo.TestTable
WHERE JobDomain = @JobDomain AND NOT EXISTS(SELECT 1 FROM cte_Type_B)
)
SELECT *
FROM cte_Type_B
UNION ALL
SELECT *
FROM cte_Type_Other
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".
December 8, 2022 at 1:38 am
Scratched the answer that was here... I didn't read the post correctly.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 8, 2022 at 1:40 am
Or slightly more concisely
DECLARE @JobDomain int
SET @JobDomain = 1
SELECT *
FROM dbo.TestTable
WHERE JobDomain = @JobDomain
AND (TestType = 'B'
OR NOT EXISTS(SELECT *
FROM dbo.TestTable
WHERE JobDomain = @JobDomain
AND TestType = 'B'))
December 8, 2022 at 1:50 am
Guys... the OP doesn't know which domains contain a "B" ahead of time. He was just explaining the desired results for the test data he provided. Or, at least, that's how I read it.
See Jonathan's post below. I believe I'm going to go to be early tonight {headdesk}.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 8, 2022 at 1:53 am
Guys... the OP doesn't know which domains contain a "B" ahead of time. He was just explaining the desired results for the test data he provided. Or, at least, that's how I read it.
I want to select from the table by specifying the JobDomain and if there is a TestType = 'B', then just return that record. If there isn't a TestType = 'B', the return all records for that JobDomain
December 8, 2022 at 2:10 am
Heh... I've been having issues with caffeine absorption lately. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
December 8, 2022 at 9:37 am
Hi,
Thanks for the replies. Both approaches work (Scott's & Jonathan's). I've never used EXISTS before so must have a play with it. I also didn't know you could use a CTE like in Scott's answer as in define 2 in the same query.
As they say in my part of the world: every day's a school day!!
December 8, 2022 at 5:16 pm
Jeff Moden wrote:Heh... I've been having issues with caffeine absorption lately. 😀
Too much ? Or not enough ?
Both, actually. Too much in the morning and too little in afternoon/evening, and night. I'm considering just hooking up an IV. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply