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

hierarchy data using recursive CTE

$
0
0

I'm working on a report that is in picbasic(yippee) and converting it to .NET. The tables are all pulled from unibasic tables to sql tables. The report is suppose to look like the example below or similar. The table(only showing a portion of data to keep it simple) has the ParentID(ID which can have many to start) and the ChildID(which ties to the next parentId if there is one)  and basically joins until there isn't a ChildID available(NULL). I wrote a procedure which I thought would run fine, but sits and doesn't return any type of results. SQL is not my strong point. As you can see the Starting number(part#) is SA33028 and there are 8 parts underneath it and then those parts may have component parts and so on, so forth.

0.........  SA330281........42S01311........5S01271........6S01751........  SA330232.......16S01422.......18S01032.......24S01082.......28SM01052.......  S30461.AI2.......  S304633......  S288073......  S288072.......  S306412.......  S311342.......  S93832.......  SA298013......16S01163......  S277983......  S311703......  SA300594.....38S01164.....7S01054.....  S19430.1455....7SM01456...7SM0145.0016...7SM0145.0025....  S194304.....  S194312.......  SA30910

Data:

SA3302316S0142
SA3302318S0103
SA3302324S0108
SA3302328SM0105
SA33023S30461.AI
SA33023S30463
S30463S28807
S30463S28807
SA33023S30641
SA33023S31134
SA33023S9383
SA33023SA29801
SA2980116S0116
SA29801S27798
SA29801S31170
SA29801SA30059
SA3005938S0116
SA300597S0105
SA30059S19430.145
S19430.1457SM0145
7SM01457SM0145.001
7SM01457SM0145.002
S19430.145S19430
SA30059S19431
SA33023SA30910

SQL:

WITH AllParts AS 
(
SELECT 1 AS Level,
	Child.Parent_Part,
	Child.Component_Part
	FROM ps as Child
WHERE Parent_Part = 'SA33028'

UNION ALL 

SELECT ap.Level + 1, 
		TempPS.Parent_Part,
		TempPS.Component_Part
 FROM ps AS TempPS
INNER JOIN AllParts AS ap
	ON TempPS.Parent_Part = ap.Component_Part 
)
SELECT * FROM Allparts

END


Viewing all articles
Browse latest Browse all 23857

Trending Articles



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