Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Computers
- Processors
- Intel
- Pentium
- Core 2 Duo
- AMD
- Athlon
- SELECT * FROM products
- INNER JOIN categories ON categories.id = products.category_id
- WHERE categories.lft > 2 and categories.rgt < 11
- with catCTE (catid, parentid)
- as
- (
- select cat.catid, cat.catparentid from cat where cat.name = 'Processors'
- UNION ALL
- select cat.catid, cat.catparentid from cat inner join catCTE on cat.catparentid=catcte.catid
- )
- select distinct * from catCTE
- categoriesSet = empty set
- while new.size > 0:
- new = select * from categories where parent in categoriesSet
- categoriesSet = categoriesSet+new
- select *
- from products
- where products.category_id IN
- (select c2.category_id
- from categories c1 inner join categories c2 on c1.category_id = c2.parent_id
- where c1.category = 'Processors'
- group by c2.category_id)
- CREATE TABLE #categories (id INT NOT NULL, parentId INT, [name] NVARCHAR(100))
- INSERT INTO #categories
- SELECT 1, NULL, 'Computers'
- UNION
- SELECT 2, 1, 'Processors'
- UNION
- SELECT 3, 2, 'Intel'
- UNION
- SELECT 4, 2, 'AMD'
- UNION
- SELECT 5, 3, 'Pentium'
- UNION
- SELECT 6, 3, 'Core 2 Duo'
- UNION
- SELECT 7, 4, 'Athlon'
- SELECT *
- FROM #categories
- DECLARE @id INT
- SET @id = 2
- ; WITH r(id, parentid, [name]) AS (
- SELECT id, parentid, [name]
- FROM #categories c
- WHERE id = @id
- UNION ALL
- SELECT c.id, c.parentid, c.[name]
- FROM #categories c JOIN r ON c.parentid=r.id
- )
- SELECT *
- FROM products
- WHERE p.productd IN
- (SELECT id
- FROM r)
- DROP TABLE #categories
- DECLARE @startingCatagoryId int
- DECLARE @current int
- SET @startingCatagoryId = 13813 -- or whatever the CatagoryId is for 'Processors'
- CREATE TABLE #CatagoriesToFindChildrenFor
- (CatagoryId int)
- CREATE TABLE #CatagoryTree
- (CatagoryId int)
- INSERT INTO #CatagoriesToFindChildrenFor VALUES (@startingCatagoryId)
- WHILE (SELECT count(*) FROM #CatagoriesToFindChildrenFor) > 0
- BEGIN
- SET @current = (SELECT TOP 1 * FROM #CatagoriesToFindChildrenFor)
- INSERT INTO #CatagoriesToFindChildrenFor
- SELECT ID FROM Catagory WHERE ParentCatagoryId = @current AND Deleted = 0
- INSERT INTO #CatagoryTree VALUES (@current)
- DELETE #CatagoriesToFindChildrenFor WHERE CatagoryId = @current
- END
- SELECT * FROM #CatagoryTree ORDER BY CatagoryId
- DROP TABLE #CatagoriesToFindChildrenFor
- DROP TABLE #CatagoryTree
- -- create a categories table and fill it with 10 rows (with random parentIds)
- CREATE TABLE Categories ( Id uniqueidentifier, ParentId uniqueidentifier )
- GO
- INSERT
- INTO Categories
- SELECT NEWID(),
- NULL
- GO
- INSERT
- INTO Categories
- SELECT TOP(1)NEWID(),
- Id
- FROM Categories
- ORDER BY Id
- GO 9
- DECLARE @lvl INT, -- holds onto the level as we move throught the hierarchy
- @Id Uniqueidentifier -- the id of the current item in the stack
- SET @lvl = 1
- CREATE TABLE #stack (item UNIQUEIDENTIFIER, [lvl] INT)
- -- we fill fill this table with the ids we want
- CREATE TABLE #tmpCategories (Id UNIQUEIDENTIFIER)
- -- for this example we’ll just select all the ids
- -- if we want all the children of a specific parent we would include it’s id in
- -- this where clause
- INSERT INTO #stack SELECT Id, @lvl FROM Categories WHERE ParentId IS NULL
- WHILE @lvl > 0
- BEGIN -- begin 1
- IF EXISTS ( SELECT * FROM #stack WHERE lvl = @lvl )
- BEGIN -- begin 2
- SELECT @Id = [item]
- FROM #stack
- WHERE lvl = @lvl
- INSERT INTO #tmpCategories
- SELECT @Id
- DELETE FROM #stack
- WHERE lvl = @lvl
- AND item = @Id
- INSERT INTO #stack
- SELECT Id, @lvl + 1
- FROM Categories
- WHERE ParentId = @Id
- IF @@ROWCOUNT > 0
- BEGIN -- begin 3
- SELECT @lvl = @lvl + 1
- END -- end 3
- END -- end 2
- ELSE
- SELECT @lvl = @lvl - 1
- END -- end 1
- DROP TABLE #stack
- SELECT * FROM #tmpCategories
- DROP TABLE #tmpCategories
- DROP TABLE Categories
Add Comment
Please, Sign In to add comment