I’m using SQL Server 2008. Here is what I’m trying to do. I have a table with marketgroupID and parentID. each marketgroup has a name. I’d like to create the hierarchy path of those names.
I tried the CTE solution with a WITH anchor_statement recursive_statement, select. it didn’t work.
I found another solution where you create a new table, and fill in the values iteratively. It’s not the most performant, it duplicates lots of data and columns, but i don’t really care because it’s more or less a one time process on a relatively small database. I’m just looking for a simple solution (like everyone I guess)
Basically, I successfully created the table and copied some values inside of it.
--Create Lineage Table
Create Table Eve.dbo.invLineage(
Node int NOT NULL IDENTITY(100,1),
typeID int,
parentNode int,
marketGroupName nvarchar(100),
marketGroupID int,
Depth tinyint,
Lineage nvarchar(max)
)
and copied values into it.
INSERT INTO EVE.dbo.invLineage ( typeID,marketGroupID)
SELECT [EVE].[dbo].invTypes.typeID,EVE.dbo.invTypes.marketGroupID
FROM EVE.dbo.invTypes
LEFT JOIN eve.dbo.invGroups ON EVE.dbo.invTypes.groupID= eve.dbo.invGroups.groupID
WHERE eve.dbo.invGroups.categoryID=7 AND Eve.dbo.invTypes.published=1
this is an exemple of the result I get at this stage:
Node typeID parentNode marketGroupName marketGroupID Depth Lineage
100 377 NULL NULL 605 NULL NULL
101 380 NULL NULL 605 NULL NULL
102 393 NULL NULL 126 NULL NULL
103 394 NULL NULL 126 NULL NULL
104 399 NULL NULL 609 NULL NULL
105 400 NULL NULL 609 NULL NULL
106 405 NULL NULL 604 NULL NULL
107 406 NULL NULL 604 NULL NULL
So I managed to get a nodeID, a typeID, and a marketGroupID successfully. sorry for the bad display in here, I’m still learning the forums.
Now, I’d like to update the parentNode for each row of the Lineage table.
This information is a column in the marketGroup table.
Here is the request I wrote but that returns 0 rows.
UPDATE T SET T.parentNode=P.Node
FROM Eve.dbo.invLineage as T
INNER JOIN eve.dbo.invMarketGroups E ON T.marketGroupID=E.marketGroupID
INNER JOIN eve.dbo.invMarketGroups B ON E.parentGroupID=B.marketGroupID
INNER JOIN Eve.dbo.invLineage P ON B.marketGroupID=P.marketGroupID
The first 2 inner joins seems to work fine, but the last one makes it return 0 rows.
Also, I’m following this handy guide : http://www.sqlteam.com/article/more-trees-hierarchies-in-sql
Here are some results i get if i run this select query for debug purpose:
SELECT * --SET T.parentNode=P.Node
FROM Eve.dbo.invLineage as T
INNER JOIN eve.dbo.invMarketGroups E ON T.marketGroupID=E.marketGroupID
INNER JOIN eve.dbo.invMarketGroups B ON E.parentGroupID=B.marketGroupID
--INNER JOIN eve.dbo.invLineage P ON B.marketGroupID=P.marketGroupID
Node typeID parentNode marketGroupName marketGroupID Depth Lineage marketGroupID parentGroupID marketGroupName description iconID hasTypes marketGroupID parentGroupID marketGroupName description iconID hasTypes
1134 10039 NULL NULL 760 NULL NULL 760 9 Civilian Modules Modules whose specifications are geared toward use on rookie ships. NULL 1 9 NULL Ship Equipment Everything the dedicated pilot needs to outfit their ship. 1432 0
2913 21853 NULL NULL 760 NULL NULL 760 9 Civilian Modules Modules whose specifications are geared toward use on rookie ships. NULL 1 9 NULL Ship Equipment Everything the dedicated pilot needs to outfit their ship. 1432 0
Both your records have
B.marketGroupID = 9.But your
eve.dbo.invLineagetable only hasmarketGroupID IN (605, 126, 209, 604)So, in short, you get no records because
ON B.marketGroupID=P.marketGroupIDnever finds any matches.