SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 recursive query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

magmo
Aged Yak Warrior

524 Posts

Posted - 02/28/2013 :  08:07:05  Show Profile  Reply with Quote
Hi

I have the following table and data....




USE [Test]
GO
/****** Object:  Table [dbo].[Products]    Script Date: 2013-02-28 13:58:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Products](
	[NodeId] [int] IDENTITY(1,1) NOT NULL,
	[ParentNodeId] [int] NULL,
	[Text] [nvarchar](255) NULL,
	[Active] [bit] NULL,
	[CustID] [int] NOT NULL,
 CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED 
(
	[NodeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET IDENTITY_INSERT [dbo].[Products] ON 

GO
INSERT [dbo].[Products] ([NodeId], [ParentNodeId], [Text], [Active], [CustID]) VALUES (1, NULL, N'Blues', 1, 20)
GO
INSERT [dbo].[Products] ([NodeId], [ParentNodeId], [Text], [Active], [CustID]) VALUES (3, NULL, N'Pop', NULL, 20)
GO
INSERT [dbo].[Products] ([NodeId], [ParentNodeId], [Text], [Active], [CustID]) VALUES (4, NULL, N'Rock', NULL, 20)
GO
INSERT [dbo].[Products] ([NodeId], [ParentNodeId], [Text], [Active], [CustID]) VALUES (5, 4, N'Elvis Presley', NULL, 20)
GO
INSERT [dbo].[Products] ([NodeId], [ParentNodeId], [Text], [Active], [CustID]) VALUES (6, 3, N'Duran Duran', NULL, 20)
GO
INSERT [dbo].[Products] ([NodeId], [ParentNodeId], [Text], [Active], [CustID]) VALUES (7, 4, N'Moody Blue', NULL, 20)
GO
INSERT [dbo].[Products] ([NodeId], [ParentNodeId], [Text], [Active], [CustID]) VALUES (8, 4, N'Aloha from Hawaii Via Satellite', NULL, 20)
GO
INSERT [dbo].[Products] ([NodeId], [ParentNodeId], [Text], [Active], [CustID]) VALUES (9, 3, N'Notorious ', NULL, 20)
GO
INSERT [dbo].[Products] ([NodeId], [ParentNodeId], [Text], [Active], [CustID]) VALUES (10, 3, N'Decade: Greatest Hits ', NULL, 20)
GO
SET IDENTITY_INSERT [dbo].[Products] OFF
GO




I would like to create a query that produce a result like this....

CombinedTopNode ArticleName

Blues
Pop\Duran Duran Notorious
Pop\Duran Duran Decade: Greatest Hits
Rock\Elvis Presley Moody Blue
Rock\Elvis Presley Aloha from Hawaii Via Satellite



Can anyone show this could be done?

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 02/28/2013 :  08:50:37  Show Profile  Reply with Quote
The basic recursive query would be like shown below. You can make it more compact and efficient by removing columns you don't need, add some logic to display only the rows/columns you want to see etc.

;WITH cte AS
(
SELECT
	p.NodeId,
	p.ParentNodeId,
	p.Text,
	p.Active,
	p.CustID,
	CAST([Text] AS VARCHAR(256)) AS CombinedName,
	CAST([Text] AS VARCHAR(256)) AS ArticleName
FROM
	Products p
WHERE
	ParentNodeId IS NULL
UNION ALL

SELECT
	p.NodeId,
	p.ParentNodeId,
	p.Text,
	p.Active,
	p.CustID,
	CAST(c.CombinedName + '\' + p.Text AS VARCHAR(256)),
	CAST(p.Text AS VARCHAR(256))
FROM
	Products p
	INNER JOIN cte c ON c.NodeId = p.ParentNodeId
)
SELECT * FROM cte;
The tutorial on this page is very well-written http://msdn.microsoft.com/en-us/library/ms186243(v=sql.105).aspx
Go to Top of Page

magmo
Aged Yak Warrior

524 Posts

Posted - 02/28/2013 :  09:00:00  Show Profile  Reply with Quote
Thanks James, it worked perfect
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 02/28/2013 :  09:46:38  Show Profile  Reply with Quote
You are very welcome - glad to help.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000