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

Top-pest parent column should be as new column

$
0
0

Hi,

i have a table like below 

CREATE TABLE #MyTable (ID Integer,Description varchar(200),ParentID Integer)
insert INTO #MyTable (ID,Description,ParentID) VALUES (1,'1AAA',0)
insert INTO #MyTable (ID,Description,ParentID) VALUES (2,'1BBB',0)
insert INTO #MyTable (ID,Description,ParentID) VALUES (3,'2AAA',1)
insert INTO #MyTable (ID,Description,ParentID) VALUES (4,'2BBB',2)
insert INTO #MyTable (ID,Description,ParentID) VALUES (5,'3AAA',3)
insert INTO #MyTable (ID,Description,ParentID) VALUES (6,'3AAB',3)
insert INTO #MyTable (ID,Description,ParentID) VALUES (7,'3BBB',4)
insert INTO #MyTable (ID,Description,ParentID) VALUES (8,'3BBC',4)
SELECT * from #MyTable

I need a new column which has to display the top-pest parent. like below

Thanks


Viewing all articles
Browse latest Browse all 12890

Trending Articles



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