Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Getting Child-Top Level Parent Relationship without looping Expand / Collapse
Author
Message
Posted Wednesday, January 30, 2013 1:20 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 1:53 PM
Points: 785, Visits: 1,534
Hey all,

Given the following:

CREATE TABLE #Transfer
(
ID INT IDENTITY PRIMARY KEY,
OldKey INT,
NewKey INT
)

INSERT INTO #Transfer (OldKey, NewKey)
VALUES (1, 2)
INSERT INTO #Transfer (OldKey, NewKey)
VALUES (2, 3)
INSERT INTO #Transfer (OldKey, NewKey)
VALUES (3, 4)
INSERT INTO #Transfer (OldKey, NewKey)
VALUES (5, 6)
INSERT INTO #Transfer (OldKey, NewKey)
VALUES (6, 7)

ID	OldKey	NewKey
1 1 2
2 2 3
3 3 4
4 5 6
5 6 7

Is there any way to get the following output, *without* resorting to using loops?

SELECT 1 AS OldKey, 4 AS NewKey
UNION
SELECT 2 AS OldKey, 4 AS NewKey
UNION
SELECT 3 AS OldKey, 4 AS NewKey
UNION
SELECT 4 AS OldKey, 4 AS NewKey
UNION
SELECT 5 AS OldKey, 7 AS NewKey
UNION
SELECT 6 AS OldKey, 7 AS NewKey
UNION
SELECT 7 AS OldKey, 7 AS NewKey

OldKey	NewKey
1 4
2 4
3 4
4 4
5 7
6 7
7 7

Post #1413752
Posted Wednesday, January 30, 2013 6:18 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 12:42 AM
Points: 2,338, Visits: 3,158
Yup. Although technically most experts would consider a recursive CTE a loop in disguise.

CREATE TABLE #Transfer
(
ID INT IDENTITY PRIMARY KEY,
OldKey INT,
NewKey INT
)

INSERT INTO #Transfer (OldKey, NewKey)
VALUES (1, 2)
INSERT INTO #Transfer (OldKey, NewKey)
VALUES (2, 3)
INSERT INTO #Transfer (OldKey, NewKey)
VALUES (3, 4)
INSERT INTO #Transfer (OldKey, NewKey)
VALUES (5, 6)
INSERT INTO #Transfer (OldKey, NewKey)
VALUES (6, 7)

;WITH Transfers AS (
SELECT Oldkey=Newkey, Newkey
FROM #Transfer
UNION ALL
SELECT a.Oldkey, a.Newkey
FROM #Transfer a
UNION ALL
SELECT b.Oldkey, a.Newkey
FROM Transfers a
JOIN #Transfer b ON b.NewKey = a.Oldkey
)
SELECT OldKey, NewKey=MAX(NewKey)
FROM Transfers
GROUP BY OldKey
ORDER BY OldKey, NewKey

DROP TABLE #Transfer





No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1413812
Posted Thursday, January 31, 2013 9:10 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 1:53 PM
Points: 785, Visits: 1,534
excellent - thanks!
Post #1414194
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse