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


SubQuery returns more than 1 value


SubQuery returns more than 1 value

Author
Message
scribesjeff
scribesjeff
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 103
Hi,

I'm looking for a little help with this query. I'm trying to write a query that returns true if a visit exists in a Fact table and not in an error table and has a message NOT LIKE 'w%' otherwise it returns false

I have this query which returns all the visits that would be false visits but I need to output to be false not the visit number.

SELECT E.VisitID
FROM Fact F LEFT OUTER JOIN Error E
ON F.VisitID = E.VisitID
WHERE E.VisitID IS NOT NULL AND E.Message NOT LIKE 'w%'

I was thinking I could do a CASE Like this

SELECT CASE
WHEN F.VisitID = (SELECT F.VisitID
FROM Fact F LEFT OUTER JOIN Error E
ON F.VisitID = E.VisitID
WHERE E.VisitID IS NOT NULL AND EEPE.MessageNumber NOT LIKE 'w%')THEN 'False'
ELSE 'True'
END
FROM Fact F JOIN Error E
ON F.VisitID = E.VisitID

But that give me the problem of SubQuery returns more than 1 value.
Can anyone please point me a better direction or give me some help with my query?

Thanks!
mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1610 Visits: 3317
You could use and EXISTS in you case statement rather than an =

Not sure if I've got you logic right, but something like the following
-- Some Test Data
;with Fact as (
SELECT *
FROM (VALUES (1), (2), (3), (4), (5)) AS Fact(VisitID)
)
, Error as (
SELECT *
FROM (VALUES (1,'a'), (3,'w also has another'), (3,'a'), (4, 'w just this one'), (5,'a')) AS Error(VisitID, [Message])
)
-- The Query
SELECT f.VisitID, CASE
WHEN EXISTS(SELECT 1 FROM Error e WHERE e.visitID = f.visitID and [Message] not like 'w%') THEN 'false'
ELSE 'true'
END
FROM Fact f


scribesjeff
scribesjeff
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 103
Thanks That fixed it
mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1610 Visits: 3317
No Problem
kapil_kk
kapil_kk
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3139 Visits: 2766
good one micky....

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
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