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
 General SQL Server Forums
 New to SQL Server Programming
 Nested SQL select statement

Author  Topic 

sparrow37
Posting Yak Master

148 Posts

Posted - 2010-09-15 : 06:05:42
Hi all,

I have a table with following columns

[CategoryID] [int] IDENTITY(1,1) NOT NULL,
[ParentCatID] [int] NOT NULL,
[CategoryName] [varchar](50) NOT NULL,


and data is in this form

2 5 Sub Category
3 2 Tertiary Category 1
4 2 Tertiary Category 2
5 0 Category 1
6 0 Category 2

I want a select statement which shows data in this form: ( if there is parent category then it should be appendeded no matter how many parents)

Category
Category / Sub Category
Category / Sub Category / Tertiary Category
Category / Sub Category

Regards,
Asif Hameed

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2010-09-15 : 06:19:00
Have a look at this:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=88675&SearchTerms=heirachy

Or there are loads of other articles on parent/child relationships..
Go to Top of Page

sparrow37
Posting Yak Master

148 Posts

Posted - 2010-09-15 : 06:27:47
Hi RickD:

Thanks for the quick response. My table structure is a bit different. Can you please suggest solution in case of my table structure ?

Regards,
Asif Hameeed
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-09-15 : 07:00:16
It's almost exactly the same as some of the examples!

Play about with this until you understand what's happening.

/*
[CategoryID] [int] IDENTITY(1,1) NOT NULL,
[ParentCatID] [int] NOT NULL,
[CategoryName] [varchar](50) NOT NULL,


and data is in this form

2 5 Sub Category
3 2 Tertiary Category 1
4 2 Tertiary Category 2
5 0 Category 1
6 0 Category 2

I want a select statement which shows data in this form: ( if there is parent category then it should be appendeded no matter how many parents)

Category
Category / Sub Category
Category / Sub Category / Tertiary Category
Category / Sub Category
*/
DECLARE @foo TABLE (
[categoryID] INT PRIMARY KEY
, [parentCatID] INT
, [categoryName] VARCHAR(50)
)

INSERT @foo ([categoryID], [parentCatID], [categoryName])
SELECT 2, 5, 'Sub Cetegory'
UNION SELECT 3, 2, 'Tertiary Category 1'
UNION SELECT 4, 2, 'Tertiary Category 2'
UNION SELECT 5, 0, 'Category 1'
UNION SELECT 6, 0, 'Category 2'

; WITH

tree ([categoryID], [categoryName], [path])
AS (

-- Anchor Definition
SELECT
[categoryID], [categoryName], CAST([categoryName] AS VARCHAR(MAX))
FROM
@foo
WHERE
[parentCatId] = 0

-- Recursive Definition
UNION ALL SELECT
f.[categoryID], f.[categoryName], t.[path] + ' / ' + CAST(f.[categoryName] AS VARCHAR(MAX))
FROM
@foo AS f
JOIN tree AS t ON t.[categoryID] = f.[parentCatID]
)

SELECT * FROM tree ORDER BY [path]




Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

sparrow37
Posting Yak Master

148 Posts

Posted - 2010-09-15 : 07:12:20
Hi TC :

Thanks for your help. I Created this:



ALTER PROCEDURE GetProdCategories
@CategoryName VARCHAR(100) = ''
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

DECLARE @SampleTable Table(
[CategoryID] [int] ,
[ParentCatID] [int] NOT NULL,
[CategoryName] [varchar](50) NOT NULL
)

INSERT INTO @SampleTable ([CategoryID],[ParentCatID],[CategoryName])
SELECT CategoryID,ParentCatID,CategoryName FROM ProductCategories

DECLARE @FullName VARCHAR(500)
SET @FullName = ''

;WITH Yak(CategoryID,ParentCatID,CategoryName,Lvl)
AS(

SELECT CategoryID,ParentCatID,CategoryName,0
FROM @SampleTable
WHERE CategoryName = @CategoryName

UNION ALL

SELECT s.CategoryID,s.ParentCatID,s.CategoryName,y.Lvl
FROM @SampleTable s
INNER JOIN Yak AS y ON y.ParentCatID = s.CategoryID

)

SELECT @FullName = @FullName + CategoryName + '/'
FROM Yak
ORDER BY CategoryID DESC


SELECT LEFT(@FullName,CASE WHEN RIGHT(@FullName,1) = '/' THEN LEN(@FullName)-1 ELSE LEN(@FullName) END)

END
GO
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2010-09-15 : 14:32:48
This is an adjacency list model and you might want to Google it for code.

What you are trying to do is called Violating First Normal Form (1NF) and good SQL programmers do not write this kind of code. Do your display work in the front end, not the database.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -