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 query

Author  Topic 

Furrukh
Starting Member

6 Posts

Posted - 2012-04-19 : 12:27:35
Hi,

I need help in order to sort out following result set.



CREATE TABLE MYPARTS (PART_NO VARCHAR(10) NOT NULL, REPLACEMENT_PART_NO VARCHAR(10) NULL)

GO

INSERT INTO MYPARTS (PART_NO, REPLACEMENT_PART_NO) VALUES ('999' , '789');
INSERT INTO MYPARTS (PART_NO, REPLACEMENT_PART_NO) VALUES ('123' , '456');
INSERT INTO MYPARTS (PART_NO, REPLACEMENT_PART_NO) VALUES ('456' , '777');
INSERT INTO MYPARTS (PART_NO, REPLACEMENT_PART_NO) VALUES ('789' , '777');
INSERT INTO MYPARTS (PART_NO, REPLACEMENT_PART_NO) VALUES ('777' , '888');
INSERT INTO MYPARTS (PART_NO, REPLACEMENT_PART_NO) VALUES ('888' , NULL);


/* Required result set

PART_NO REPLACEMENT_LIST
999
123
456 123
789 999
777 789,456,123,999
888 777,789,456,123,999

*/

Any help will be appreciated.

Thanks,
FurrukH Baig

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-19 : 14:33:33
you need to use a CTE for this

have a look at

http://msdn.microsoft.com/en-us/library/ms186243.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Furrukh
Starting Member

6 Posts

Posted - 2012-04-19 : 18:45:09
quote:
Originally posted by visakh16

you need to use a CTE for this

have a look at

http://msdn.microsoft.com/en-us/library/ms186243.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Hi Visak16,

I already knew that I need CTE hence the title says recursive query.

I want to see result as follows

PART_NO REPLACEMENT_LIST
999
123
456 123
789 999
777 789,456,123,999
888 777,789,456,123,999

this is complete hirerchy of replaced parts. like part 777 has replaced 456 and 789 and 456 did replaced 123 simillarly 789 replaced 999 hence result should contain all possible replacement as

777 789,456,123,999

please note 777 directly replaced 789 and 456 but indirectly it also is the replacement for 123 and 999.
Go to Top of Page

namman
Constraint Violating Yak Guru

285 Posts

Posted - 2012-04-20 : 13:49:48
Furrukh

Do you have solution yet ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-20 : 15:14:43
quote:
Originally posted by Furrukh

quote:
Originally posted by visakh16

you need to use a CTE for this

have a look at

http://msdn.microsoft.com/en-us/library/ms186243.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Hi Visak16,

I already knew that I need CTE hence the title says recursive query.

I want to see result as follows

PART_NO REPLACEMENT_LIST
999
123
456 123
789 999
777 789,456,123,999
888 777,789,456,123,999

this is complete hirerchy of replaced parts. like part 777 has replaced 456 and 789 and 456 did replaced 123 simillarly 789 replaced 999 hence result should contain all possible replacement as

777 789,456,123,999

please note 777 directly replaced 789 and 456 but indirectly it also is the replacement for 123 and 999.



What happened when you tried using CTE? can we see tried query?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Furrukh
Starting Member

6 Posts

Posted - 2012-04-21 : 06:02:12
quote:
Originally posted by namman

Furrukh

Do you have solution yet ?



no not yet. I am surprise that no one on this forum can answer simple question. but thanks anyways for asking.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-21 : 11:38:43
quote:
Originally posted by Furrukh

quote:
Originally posted by namman

Furrukh

Do you have solution yet ?



no not yet. I am surprise that no one on this forum can answer simple question. but thanks anyways for asking.

Everyone who responds to questions on this forum is a volunteer, and answers questions which interests them and have expertise in, in their spare time. So sometimes, questions do fall through the cracks. If you post the query that you have now, even if it does not work correctly, Visakh, or someone else would have given an answer.

In any case, here is one way in which you can use CTE to get the results you are looking for. You will notice that the replacement parts are not ordered in the same order as you showed in your sample results. Not sure if that is sufficient, or if you must have a specific ordering. If you do, it can be done, if you can describe what the rule for ordering is
;WITH cte1 AS
(
SELECT a.part_no,b.part_no AS REPLACEMENT_PART_NO
FROM MyParts a LEFT JOIN MyParts b ON a.part_no=b.REPLACEMENT_PART_NO
UNION ALL
SELECT
c1.part_no,
b.part_no
FROM
cte1 c1
INNER JOIN MyParts b ON c1.REPLACEMENT_PART_NO = b.REPLACEMENT_PART_NO
)
SELECT DISTINCT
a.part_no,
b.Replacements
FROM
cte1 a
OUTER APPLY
(
SELECT STUFF((
SELECT ','+b.REPLACEMENT_PART_NO AS [text()]
FROM cte1 b
WHERE b.part_no = a.part_no
ORDER BY b.REPLACEMENT_PART_NO
FOR XML PATH('')),1,1,'')
) b(Replacements);
BTW, in case you think I am being all negative, I am not!! For example, I thought your first post was great!! - it had the script to create the table and sample data, along with expected results - what more could one ask for!?!
Go to Top of Page

Furrukh
Starting Member

6 Posts

Posted - 2012-04-21 : 12:22:15
quote:
Originally posted by sunitabeck

quote:
Originally posted by Furrukh

quote:
Originally posted by namman

Furrukh

Do you have solution yet ?



no not yet. I am surprise that no one on this forum can answer simple question. but thanks anyways for asking.

Everyone who responds to questions on this forum is a volunteer, and answers questions which interests them and have expertise in, in their spare time. So sometimes, questions do fall through the cracks. If you post the query that you have now, even if it does not work correctly, Visakh, or someone else would have given an answer.

In any case, here is one way in which you can use CTE to get the results you are looking for. You will notice that the replacement parts are not ordered in the same order as you showed in your sample results. Not sure if that is sufficient, or if you must have a specific ordering. If you do, it can be done, if you can describe what the rule for ordering is
;WITH cte1 AS
(
SELECT a.part_no,b.part_no AS REPLACEMENT_PART_NO
FROM MyParts a LEFT JOIN MyParts b ON a.part_no=b.REPLACEMENT_PART_NO
UNION ALL
SELECT
c1.part_no,
b.part_no
FROM
cte1 c1
INNER JOIN MyParts b ON c1.REPLACEMENT_PART_NO = b.REPLACEMENT_PART_NO
)
SELECT DISTINCT
a.part_no,
b.Replacements
FROM
cte1 a
OUTER APPLY
(
SELECT STUFF((
SELECT ','+b.REPLACEMENT_PART_NO AS [text()]
FROM cte1 b
WHERE b.part_no = a.part_no
ORDER BY b.REPLACEMENT_PART_NO
FOR XML PATH('')),1,1,'')
) b(Replacements);
BTW, in case you think I am being all negative, I am not!! For example, I thought your first post was great!! - it had the script to create the table and sample data, along with expected results - what more could one ask for!?!



Hi Sunitabeck,

Thanks for answering my query. this is exactly what I was looking for. Ordering item is not required.

Many thanks once more.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-21 : 12:27:01
quote:
Originally posted by Furrukh

quote:
Originally posted by namman

Furrukh

Do you have solution yet ?



no not yet. I am surprise that no one on this forum can answer simple question. but thanks anyways for asking.


We can answer as long as we see an attempt from your end. I had given guidelines to you but refrained myself from posting complete solution as I wanted you to try it fast. Giving spoonfed answers will help you only for the particular instance, but you wont learn and benefit anything from that. We want people to learn and understand concept for a solution not just mug up the solution itself.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Furrukh
Starting Member

6 Posts

Posted - 2012-04-21 : 12:36:31
quote:
Originally posted by visakh16

quote:
Originally posted by Furrukh

quote:
Originally posted by namman

Furrukh

Do you have solution yet ?



no not yet. I am surprise that no one on this forum can answer simple question. but thanks anyways for asking.


We can answer as long as we see an attempt from your end. I had given guidelines to you but refrained myself from posting complete solution as I wanted you to try it fast. Giving spoonfed answers will help you only for the particular instance, but you wont learn and benefit anything from that. We want people to learn and understand concept for a solution not just mug up the solution itself.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Thanks for response but we all are experienced developers here. But sometime we just cant get things right for specific need and missing something very small and you need a solution from someone else in community. Please accept my apology if anything I said hurts you but I would suggest not to underestimate ones skills.
Cheers.
Furrukh Baig
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-21 : 12:45:33
No problem
I'm not underestimating you (or anyone here).
We get lots of posts here from students who wants to get some solution to complete assignment without doing any try themselves. So unless we see a proper attempt we cant make out whether you're an experienced developer or a student trying to finish an assignment. In any case,posting what you tried would have certainly helped us to understand you better!
Anyways next time you've a problem, please remember to post what you tried so that we can guide you based on that (it need not be your actual query, just sample query with an indication of logic used for a sample data will be sufficient)



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Furrukh
Starting Member

6 Posts

Posted - 2012-04-21 : 12:59:24
quote:
Originally posted by visakh16

No problem
I'm not underestimating you (or anyone here).
We get lots of posts here from students who wants to get some solution to complete assignment without doing any try themselves. So unless we see a proper attempt we cant make out whether you're an experienced developer or a student trying to finish an assignment. In any case,posting what you tried would have certainly helped us to understand you better!
Anyways next time you've a problem, please remember to post what you tried so that we can guide you based on that (it need not be your actual query, just sample query with an indication of logic used for a sample data will be sufficient)



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Will do ;)

Thanks
Furrukh Baig
Blog : [url]http://furrukhbaig.wordpress.com/[/url]
Go to Top of Page
   

- Advertisement -