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


JOINS with COALESCE


JOINS with COALESCE

Author
Message
deepakkn
deepakkn
SSC Veteran
SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)

Group: General Forum Members
Points: 266 Visits: 238
Hi All,



Please fine the following is the scenario:

CREATE TABLE #ABC
(
id INT IDENTITY(1,1),
anotherid INT
)


CREATE TABLE #DEF
(
id INT IDENTITY(1,1),
anotherid INT
)

INSERT INTO #ABC
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10

INSERT INTO #DEF
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 10


SELECT '#ABC',A.*,'#DEF',B.* FROM #ABC A
LEFT JOIN #DEF B ON A.anotherid = COALESCE(B.anotherid,B.anotherid+1,B.anotherid+2)

Please check the following OUTPUT:

(No column name) id anotherid (No column name) id anotherid
#ABC 1 1 #DEF 1 1
#ABC 2 2 #DEF 2 2
#ABC 3 3 #DEF NULL NULL
#ABC 4 4 #DEF NULL NULL
#ABC 5 5 #DEF NULL NULL
#ABC 6 6 #DEF 3 6
#ABC 7 7 #DEF 4 7
#ABC 8 8 #DEF 5 8
#ABC 9 9 #DEF NULL NULL
#ABC 10 10 #DEF 6 10


I need to output like this:
Instead of NULL i need the next number in the another id column.(6)


(No column name) id anotherid (No column name) id anotherid
#ABC 1 1 #DEF 1 1
#ABC 2 2 #DEF 2 2
#ABC 3 3 #DEF 3 6
#ABC 4 4 #DEF 3 6
#ABC 5 5 #DEF 3 6
#ABC 6 6 #DEF 3 6
#ABC 7 7 #DEF 4 7
#ABC 8 8 #DEF 5 8
#ABC 9 9 #DEF 7 10
#ABC 10 10 #DEF 7 10



Please let me know if this is not understandable!!

Thanks

Deepak
Simon Liddle
Simon Liddle
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1260 Visits: 3178
Based on your sample data, this would work, but it relies on #DEF.anotherid being unique and inserted into the table in numerical order - it's not a particularly nice piece of T-SQL!

SELECT   
'#ABC',
A.*,
'#DEF',
MIN(b.id) id,
MIN(b.anotherid) anotherid FROM #ABC A
LEFT OUTER JOIN #DEF B ON A.anotherid <= B.[anotherid]
GROUP BY a.id, a.[anotherid]



If you didn't need to see #DEF.id in your results then you could drop the column from your select list and this code should work regardless of the order #DEF.anotherid is inserted in.

Just out of curiosity, what is it you are trying to do?

Cheers,
Simon :-)
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19268 Visits: 14900
Your issue is that, in the ON clause B.anotherid is never null so you are never getting to anotherid + 1 or anotherid + 2 as the evaluation is only returning a true or false.

Simon's solution is good.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
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