Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Recursive Queries

Author  Topic 

elo
Starting Member

2 Posts

Posted - 2015-04-20 : 07:47:16
i have a problem with Recursive Queries: in the table partlist are two fields: article and part. The article can have more parts and a part can also have another parts.

For example:
Article 0 have parts article 1 and article 2
Article 1 have parts article 1a and article 1b
Article 2 have parts article 1a and article 2b
Article 2b have parts article 3a and article 3b

I put it in a SQL With list as (select article,part from partlist as e union all select article,part from partlist as e inner join liste as l on e.article=l.part)

select article, part from list

It looks well:
article part
article 1 article 1a
article 1 article 1b
article 2 article 1a
article 2 article 2b
article 2b article 3a
article 2b article 3b

but now i want to put an two kinds of row number on it. One for all rows and one for the parent row It should be like this
row parent row article part
0 null Article 0 article 1
1 null Article 0 article 2
2 0 Article 1 article 1a
3 0 Article 1 article 1b
4 1 Article 2 article 1a
5 1 Article 2 article 2b
6 5 article 2b article 3a
7 5 article 2b article 3b

I tried it with row_number but i cannot solve the problem

Please help me.

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-04-20 : 08:39:48
Please post the query you tried.
Go to Top of Page

elo
Starting Member

2 Posts

Posted - 2015-04-21 : 03:56:41
the sql statement:

With liste as (
select row_number() over (order by artikelnum) as row, article,part from partlist as e
union all
select l.row, article,part from partlist as e inner join liste as l on e.article=l.part)

select * from liste



The result should be

row row_parent row_parent_kind parent kind
101 - 101 article 0 article 1
102 - 102 article 0 article 2
103 101 101_103 article 1 article 1a
104 101 101_104 article 1 article 1b
105 102 102_105 article 2 article 1a
106 102 102_106 article 2 article 2b
107 106 102_106_107 article 2b article 3a
108 106 102_106_108 article 2b article 3b
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2015-04-21 : 06:47:41
Please post test data in a comsumable format:

CREATE TABLE #temp
(
article varchar(20) NOT NULL
,part varchar(20) NOT NULL
);
INSERT INTO #temp
VALUES ('article1', 'article1a')
,('article1', 'article1b')
,('article2', 'article1a')
,('article2', 'article2b')
,('article2b', 'article3a')
,('article2b', 'article3b');


I fail to see how you can get your results from the test data.
Try playing with something like:

WITH Parts
AS
(
SELECT DISTINCT
CAST(NULL AS varchar(20)) AS parent
,article
,1 AS hlevel
FROM #temp A
WHERE NOT EXISTS
(
SELECT 1
FROM #temp A1
WHERE A1.part = A.article
)

UNION ALL

SELECT A.article, A.part, P.hlevel + 1
FROM #temp A
JOIN Parts P
ON A.article = P.article
)
,PartNos
AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY hlevel, parent, article) AS RN
,parent, article
FROM Parts
WHERE parent IS NOT NULL
)
SELECT P.RN, P1.RN AS ParentRN
,P.parent as article
,P.article as part
FROM PartNos P
LEFT JOIN PartNos P1
ON P.parent = P1.article
ORDER BY RN;
Go to Top of Page
   

- Advertisement -