Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to find TopLevel Company in a table

$
0
0
CREATE TABLE #Organizations (ID INT, Number INT, Name varchar(30), SuperiorOrganizationID INT)
INSERT INTO #Organizations VALUES
(1, 1, 'Dell', NULL),
(2, 2, 'HP', NULL),
(3, 100, 'IBM World', NULL),
(4, 150, 'IBM Europe', 3),
(5, 159, 'IBM Germany', 4)

Select Number, Name, TopMostOrganizationID = 'TO SOLVE' From #Organizations

DROP TABLE #Organizations

I need to solve the column TopMostOrganizationID. The result should be:

1, 'Dell', 1

2, 'HP', 2

100, 'IBM World', 3

150, 'IBM Europe', 3

159, 'IBM Germany', 3

Any idea?


... Podlesnick ...


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>