SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL How to use CASE with a NOT EXISTS statement


SQL How to use CASE with a NOT EXISTS statement

Author
Message
raiyanb
raiyanb
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 15
I have quite a complex query in SQL where I am pulling a load of information from various tables and doing various joins. I want to pull a column which checks whether the particular tradeId is contained in 2 different tables. I'm getting stuck with how to do this properly though.

The below code gives me all the TradeIds from the Trade table that are NOT in the TCM (which is just a combination of 2 tables). However I would like ALL the trades from the Trade table and then a column to indicate whether it is found in TCM or not.

I know this would be done with a CASE WHEN query but I'm confused how to structure it so that it fits in the CASE WHEN.

With subCA As
(Select distinct OTPTradeId, ConfoAuditSenderRef from ConfirmationAudit where ConfoAuditSenderRef like 'HBEUM%'),
TCM As
(Select distinct OTPTradeID from subCA union ALL select TradeId from subCA inner join ConfirmationSent on (OTPTradeId = ConfoId
AND ConfoAuditSenderRef like 'HBEUMN%'))
select TradeId from Trade where NOT EXISTS (Select OtpTradeId from TCM where OtpTradeId = TradeId)
and TradeDate = '17 jun 2013'




Heres my attempt to fit it in a CASE WHEN statement but I get an error because the NOT EXISTS is not allowed without a WHERE I believe. But what I am after is something like this. If I use NOT IN it becomes painstakingly slow like 5 minutes plus and this is part of an larger query and I dont want it to take so long - if possible!

With subCA As
(Select distinct OTPTradeId, ConfoAuditSenderRef from ConfirmationAudit where ConfoAuditSenderRef like 'HBEUM%'),
TCM As
(Select distinct OTPTradeID from subCA union ALL select TradeId from subCA inner join ConfirmationSent on (OTPTradeId = ConfoId
AND ConfoAuditSenderRef like 'HBEUMN%'))
select TradeId,
CASE WHEN
(TradeId NOT EXISTS (Select OtpTradeId from TCM where OtpTradeId = TradeId) Then 'Y' Else 'N' End As 'TCM'
from Trade
WHERE TradeDate = '17 jun 2013'


ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40076 Visits: 20000
Like this?
With subCA As (
Select distinct
OTPTradeId,
ConfoAuditSenderRef
from ConfirmationAudit
where ConfoAuditSenderRef like 'HBEUM%'
),
TCM As (
Select distinct
OTPTradeID
from subCA
union ALL
select TradeId
from subCA
inner join ConfirmationSent
on (OTPTradeId = ConfoId
AND ConfoAuditSenderRef like 'HBEUMN%')
)
SELECT
t.TradeId,
CASE WHEN x.OtpTradeId IS NULL THEN 'Y' Else 'N' End As 'TCM'
FROM Trade t
OUTER APPLY (SELECT TOP 1 OtpTradeId FROM TCM m WHERE m.OtpTradeId = t.TradeId) x
WHERE t.TradeDate = '17 jun 2013'



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
raiyanb
raiyanb
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 15
Thanks for that - it pulls out what I need but the query time is 8mins (and counting!) I know its difficult to say as you cannot see the structure of the DB but where could it be going wrong? (I only have readonly access to the DB so cant view the execution plan sadly)
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40076 Visits: 20000
There's scope for improvement but without ddl/execution plan it's a long shot. Try this:

SELECT 
t.TradeId,
CASE WHEN x.OtpTradeId IS NULL THEN 'Y' Else 'N' End As 'TCM'
FROM Trade t
OUTER APPLY (
SELECT TOP 1 ca.OTPTradeID, cs.TradeId
FROM ConfirmationAudit ca
LEFT JOIN ConfirmationSent cs
ON ca.OTPTradeId = cs.ConfoId
AND cs.ConfoAuditSenderRef LIKE 'HBEUMN%'
WHERE ca.ConfoAuditSenderRef LIKE 'HBEUM%'
AND (ca.OTPTradeID = t.TradeId OR cs.TradeId = t.TradeId)
) x
WHERE t.TradeDate = '17 jun 2013'




“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
raiyanb
raiyanb
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 15
I gave up on the first query - was 30 mins and counting! Massively appreciate your time Chris.

Ive tried another way albeit a copout from the IF EXISTS method - it seems to look like it works but could you please check it as well. It only took 2 minutes.


With TCM AS(
select distinct OTPTradeId from confirmationaudit where confoauditsenderref like 'HBEUM0%'
union
select distinct tradeid from confirmationsent cs, confirmationaudit ca where cs.confoid = ca.otptradeid
)
select TradeId, OTPTradeId As 'TCM ID',
case when OTPTradeId is NULL Then 'No' Else 'Yes' End as 'TCM'
from Trade left join TCM on TradeID = OTPTradeId
where tradedate = '17 Jun 2013'


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search