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


Annoying join problem & frozen brain.


Annoying join problem & frozen brain.

Author
Message
snomadj
snomadj
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2000 Visits: 718
create table A
(a int, b int)

create table D
(a int)

insert into A values (1,1)
insert into A values (2,1)
insert into A values (1,2)
insert into A values (4,2)

-- this table has other values.
insert into D values (1)
insert into D values (2)
insert into D values (3)
insert into D values (4)

-- I want to join A & D based on a values where A.b = 1
SELECT * FROM D
LEFT OUTER JOIN a
ON A.a = D.a
WHERE A.b = 1
-- but have the D values 3 & 4 show up with NULL where the A values should be :
-- like this:
1 1 1
2 2 1
3 null null
4 null null

-- In reality, it's more complex based on a big IN clause that I'm trying to avoid using twice.
SELECT * FROM D
LEFT OUTER JOIN a
ON A.a = D.a
WHERE A.b = 1
AND D.a IN
(1,2,3,4)

-- Appreciate any help!
snomadj
snomadj
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2000 Visits: 718
SELECT a2.a, D.a
FROM D
LEFT JOIN
(
SELECT * FROM A
WHERE A.b = 1
) AS a2
ON a2.a = D.a
WHERE D.a IN (1,2,3)

oh dear. i think the act of clarifying in a post sorted my brain out & the above code does what I need. sorry to have bothered!!

one of those days.

Smile
Andy Hyslop
Andy Hyslop
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5131 Visits: 3060
Like this?


SELECT * FROM D
LEFT JOIN ( SELECT *
FROM
A
WHERE
A.b = 1
) AS A
ON D.a = A.B



==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
Andy Hyslop
Andy Hyslop
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5131 Visits: 3060
Didn't see you had already sorted it!

==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
laurie-789651
laurie-789651
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3624 Visits: 1272
You need to put the condition in the join clause:



SELECT D.a, A.a, A.b
FROM D
LEFT OUTER JOIN A
ON A.a = D.a And A.b=1
WHERE A.b = 1 or A.b is null



laurie-789651
laurie-789651
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3624 Visits: 1272
In fact you don't need the WHERE clause:



SELECT D.a, A.a, A.b
FROM D
LEFT OUTER JOIN A
ON A.a = D.a And A.b=1




...but you've fixed it. Anyway - there are a few ideas there...
snomadj
snomadj
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2000 Visits: 718
Thank you both!! Appreciate you taking the time.

Smile
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97386 Visits: 20698
SELECT * 
FROM D
LEFT OUTER JOIN a
ON A.a = D.a
WHERE
A.b = 1 -- << this turns the LEFT OUTER JOIN into an INNER JOIN
AND D.a IN (1,2,3,4)

-- use this instead:
SELECT *
FROM D
LEFT OUTER JOIN a
ON A.a = D.a AND A.b = 1
WHERE D.a IN (1,2,3,4)



Ah, Laurie beat me to it.

“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
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