Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL Challenge #1


T-SQL Challenge #1

Author
Message
guruvn-335273
guruvn-335273
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 6
select
a.code
, aname
, bname
, cname
from (
select
code
, bName
, ROW_NUMBER() over (partition by code order by code) as br
from @b
) b
right join (
select
code
, cname
, ROW_NUMBER() over (partition by code order by code) as cr
from @c
) c on b.code = c.code and b.br = c.cr
right join @a a on c.code = a.code
below86
below86
SSChasing Mays
SSChasing Mays (642 reputation)SSChasing Mays (642 reputation)SSChasing Mays (642 reputation)SSChasing Mays (642 reputation)SSChasing Mays (642 reputation)SSChasing Mays (642 reputation)SSChasing Mays (642 reputation)SSChasing Mays (642 reputation)

Group: General Forum Members
Points: 642 Visits: 2121
Here is what I came up with:

SELECT ISNULL(ac.code, b1.code) AS code, a2.aname, b1.bname, ac.cname
FROM (SELECT a.code, a.aname, c.cname, ROW_NUMBER() OVER (Partition BY a.aname ORDER BY a.code, a.aname) AS RNO
FROM @a AS a
LEFT JOIN @c AS c
ON a.code = c.code) AS ac
FULL JOIN (SELECT b.code, b.bname, ROW_NUMBER() OVER (Partition BY b.code ORDER BY b.code, b.bname) AS RNO
FROM @b b) AS b1
ON ac.code = b1.code
AND ac.RNO = b1.RNO
LEFT JOIN @a AS a2
ON ISNULL(ac.code, b1.code) = a2.code
ORDER BY ISNULL(ac.code, b1.code), a2.aname, b1.bname, ac.cname



-------------------------------------------------------------
we travel not to escape life but for life not to escape us
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