Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am looking to get all the comp codes associated with prod code '500'
The table will have 1 product and comp codes for that product. The comp codes themselves will have a prod code in the table, and that prod code may well have comp codes associated with it in the table. That is the hierrachy. I am looking at a CTE that can go through the table and work out the levels/sublevels and products and components.

What I have tried:

How can I create the treeview to reflect the node levels, and how to do this with a sql CTE?

Any idea would be great thanks
Posted
Updated 31-Aug-16 22:02pm
v3
Comments
Maciej Los 30-Aug-16 12:00pm    
What have you tried? Where are you stuck?
What framework: WinForms, WebControls, WPF, ... ?

Please, read my comment to the question first.

Well, MSDN documentation is full of examples:
How to: Bind a TreeView to Data That Has an Indeterminable Depth[^]
Walkthrough: Displaying Hierarchical Data in a TreeView Control[^]
How to: Use a TreeView to Display Hierarchical Data[^]

Please, follow the links to find out how to insert nodes to TreeView from hierarchical data using C# code.

As to the CTE... You can use CTE (recursive query) to determine the level of node depth, but your data have to be corrected. Please, study below example:
SQL
DECLARE @tmp TABLE([prod code] NVARCHAR(255), [comp code] NVARCHAR(255))

INSERT INTO @tmp ([prod code], [comp code])
VALUES('Root', NULL),
('Root', '21223'),
('21223', 'part1'),
('21223', 'part2'),
('part2', 'subpart'),
('part2', 'subpart2'),
('subpart', 'lowerlevel')


;WITH CTE AS
(
	SELECT 1 AS NodeLevel, [prod code] AS NodeName
	FROM @tmp
	WHERE [comp code] IS NULL 
	UNION ALL
	SELECT t1.NodeLevel + 1 AS NodeLevel, t2.[comp code] AS NodeName
	FROM CTE AS t1 INNER JOIN @tmp AS t2 ON t1.NodeName = t2.[prod code] 
	WHERE t2.[comp code] IS NOT NULL

)
SELECT *
FROM CTE 

Result:
NodeLevel	NodeName
1			Root
2			21223
3			part1
3			part2
4			subpart
4			subpart2
5			lowerlevel


For further information about CTE, please see:
Recursive Queries Using Common Table Expressions[^]
SQL SERVER - Introduction to Hierarchical Query using a Recursive CTE - A Primer - Journey to SQL Authority with Pinal Dave[^]
CTE Recursive query for data hierarchy(Parent Child hierarchy)[^]
Depth First Strategy with Hierarchical data using CTE (Common Table Expression) - SQL Server | The SQL Ideas[^]

Try!
 
Share this answer
 
v2
Comments
phil.o 31-Aug-16 4:43am    
Nice one Maciej :) Have my 5, your answer is much more detailed than mine.
Maciej Los 31-Aug-16 9:27am    
Thank you, Phil.
BillWoodruff 31-Aug-16 19:53pm    
+5
Maciej Los 1-Sep-16 2:01am    
Thank you, Bill.
The technical adjective for what you are searching for is "hierarchical".
Here's a basic search providing enough links to have clues about it:
C# hierarchical treeview[^]

Good luck, and keep up the good work :)
 
Share this answer
 
Comments
Maciej Los 30-Aug-16 12:33pm    
5ed!
PaulMayer 30-Aug-16 12:37pm    
Thanks Maciej

It looks like what I wanted but I will try in the morning. Many thanks for this
PaulMayer 31-Aug-16 5:49am    
Hi Maciej

Your solution was close, but the CTE needs somehow to recursivley query itself to find components that are themselves products in the table. The one table has products with components and those components may well be listed in the table as a product with components - and that is the hierachy. Could be many levels/sub levels for a product. Any idea?
In the end, I done a recursive function in C# to create the treeview. I could not find a way to achieve this in SQL. The process I came up with was to create a holding list, and a class that had a List<string>, Level, and TreeNode object. By knowing that a component existed in the table as a product, I would create another node and remove the product from the list.

So in effect, I ended up with a list which contains an object with its own list. For a 900+ product tree, the processing took 10 seconds. Maybe there is a neater way but that will do for me.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900