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 2005 Forums
 Transact-SQL (2005)
 Query help please?

Author  Topic 

JBelthoff
Posting Yak Master

173 Posts

Posted - 2007-01-19 : 14:19:40
Hi,

I have a table of products. I have a table of Categories. I need a query that will return a list of categories in order depending on where it is in the categories.

So Product 1 could be under:
Top - Cat1 - Cat2 - Cat 3 - Prod 1

But product 5 could be closer to the top:
Top - Cat 1 - Cat 2 - Prod 5

Below is some sample data and two tables.

Any help is appreciated - Thanks,

CREATE TABLE [dbo].[JB_Test_Category_Main](
[CategoryId] [bigint] IDENTITY(1,1) NOT NULL,
[CategoryName] [varchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[CategoryParentId] [bigint] NULL,
CONSTRAINT [PK_JB_Test_Category_Main] PRIMARY KEY CLUSTERED
(
[CategoryId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Insert [dbo].[JB_Test_Category_Main] (CategoryName, CategoryParentId)
Values ('Top', 0)

Insert [dbo].[JB_Test_Category_Main] (CategoryName, CategoryParentId)
Values ('Cat1', 1)

Insert [dbo].[JB_Test_Category_Main] (CategoryName, CategoryParentId)
Values ('Cat2', 2)

Insert [dbo].[JB_Test_Category_Main] (CategoryName, CategoryParentId)
Values ('Cat3', 3)

Insert [dbo].[JB_Test_Category_Main] (CategoryName, CategoryParentId)
Values ('Cat4', 4)

Insert [dbo].[JB_Test_Category_Main] (CategoryName, CategoryParentId)
Values ('Cat5', 5)

Insert [dbo].[JB_Test_Category_Main] (CategoryName, CategoryParentId)
Values ('Cat6', 6)

Insert [dbo].[JB_Test_Category_Main] (CategoryName, CategoryParentId)
Values ('Cat7', 7)

Insert [dbo].[JB_Test_Category_Main] (CategoryName, CategoryParentId)
Values ('Top', 0)


CREATE TABLE [dbo].[JB_Test_Product](
[PID] [int] IDENTITY(1,1) NOT NULL,
[CatID] [bigint] NOT NULL,
[PName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

GO


Insert dbo.JB_Test_Product (CatID, PName)
Values (5, 'Prod 4')

Insert dbo.JB_Test_Product (CatID, PName)
Values (2, 'Prod 5')

Insert dbo.JB_Test_Product (CatID, PName)
Values (1, 'Prod 6')

Insert dbo.JB_Test_Product (CatID, PName)
Values (4, 'Prod 7')



/*
--Clean Up Mess
Drop Table dbo.JB_Test_Category_Main
GO

Drop Table dbo.JB_Test_Product
GO
*/


JBelthoff
• Hosts Station is a Professional Asp Hosting Provider
• Position SEO can provide your company with SEO Services at an affordable price
› As far as myself... I do this for fun!

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-01-19 : 15:11:11
So what would you want the results of the query you're asking about to look like?
Go to Top of Page

JBelthoff
Posting Yak Master

173 Posts

Posted - 2007-01-19 : 15:21:00
I would like a result set to look like this:

Top
Cat1
Cat2
Cat 3

Does that make sense?

JBelthoff
• Hosts Station is a Professional Asp Hosting Provider
• Position SEO can provide your company with SEO Services at an affordable price
› As far as myself... I do this for fun!
Go to Top of Page

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-01-19 : 17:02:16
I guess that's what I figured but then why did you even include products in the question? I just want to make sure I understand the question.
Go to Top of Page
   

- Advertisement -