code_junkie

Select products where the category belongs to any category in the hierarchy

Nov 11th, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. Computers
  2. Processors
  3. Intel
  4. Pentium
  5. Core 2 Duo
  6. AMD
  7. Athlon
  8.  
  9. SELECT * FROM products
  10. INNER JOIN categories ON categories.id = products.category_id
  11. WHERE categories.lft > 2 and categories.rgt < 11
  12.  
  13. with catCTE (catid, parentid)
  14. as
  15. (
  16. select cat.catid, cat.catparentid from cat where cat.name = 'Processors'
  17. UNION ALL
  18. select cat.catid, cat.catparentid from cat inner join catCTE on cat.catparentid=catcte.catid
  19. )
  20. select distinct * from catCTE
  21.  
  22. categoriesSet = empty set
  23. while new.size > 0:
  24. new = select * from categories where parent in categoriesSet
  25. categoriesSet = categoriesSet+new
  26.  
  27. select *
  28. from products
  29. where products.category_id IN
  30. (select c2.category_id
  31. from categories c1 inner join categories c2 on c1.category_id = c2.parent_id
  32. where c1.category = 'Processors'
  33. group by c2.category_id)
  34.  
  35. CREATE TABLE #categories (id INT NOT NULL, parentId INT, [name] NVARCHAR(100))
  36. INSERT INTO #categories
  37. SELECT 1, NULL, 'Computers'
  38. UNION
  39. SELECT 2, 1, 'Processors'
  40. UNION
  41. SELECT 3, 2, 'Intel'
  42. UNION
  43. SELECT 4, 2, 'AMD'
  44. UNION
  45. SELECT 5, 3, 'Pentium'
  46. UNION
  47. SELECT 6, 3, 'Core 2 Duo'
  48. UNION
  49. SELECT 7, 4, 'Athlon'
  50. SELECT *
  51. FROM #categories
  52. DECLARE @id INT
  53. SET @id = 2
  54. ; WITH r(id, parentid, [name]) AS (
  55. SELECT id, parentid, [name]
  56. FROM #categories c
  57. WHERE id = @id
  58. UNION ALL
  59. SELECT c.id, c.parentid, c.[name]
  60. FROM #categories c JOIN r ON c.parentid=r.id
  61. )
  62. SELECT *
  63. FROM products
  64. WHERE p.productd IN
  65. (SELECT id
  66. FROM r)
  67. DROP TABLE #categories
  68.  
  69. DECLARE @startingCatagoryId int
  70. DECLARE @current int
  71. SET @startingCatagoryId = 13813 -- or whatever the CatagoryId is for 'Processors'
  72.  
  73. CREATE TABLE #CatagoriesToFindChildrenFor
  74. (CatagoryId int)
  75.  
  76. CREATE TABLE #CatagoryTree
  77. (CatagoryId int)
  78.  
  79. INSERT INTO #CatagoriesToFindChildrenFor VALUES (@startingCatagoryId)
  80.  
  81. WHILE (SELECT count(*) FROM #CatagoriesToFindChildrenFor) > 0
  82. BEGIN
  83. SET @current = (SELECT TOP 1 * FROM #CatagoriesToFindChildrenFor)
  84.  
  85. INSERT INTO #CatagoriesToFindChildrenFor
  86. SELECT ID FROM Catagory WHERE ParentCatagoryId = @current AND Deleted = 0
  87.  
  88. INSERT INTO #CatagoryTree VALUES (@current)
  89. DELETE #CatagoriesToFindChildrenFor WHERE CatagoryId = @current
  90. END
  91.  
  92. SELECT * FROM #CatagoryTree ORDER BY CatagoryId
  93.  
  94. DROP TABLE #CatagoriesToFindChildrenFor
  95. DROP TABLE #CatagoryTree
  96.  
  97. -- create a categories table and fill it with 10 rows (with random parentIds)
  98. CREATE TABLE Categories ( Id uniqueidentifier, ParentId uniqueidentifier )
  99. GO
  100.  
  101. INSERT
  102. INTO Categories
  103. SELECT NEWID(),
  104. NULL
  105. GO
  106.  
  107. INSERT
  108. INTO Categories
  109. SELECT TOP(1)NEWID(),
  110. Id
  111. FROM Categories
  112. ORDER BY Id
  113. GO 9
  114.  
  115.  
  116. DECLARE @lvl INT, -- holds onto the level as we move throught the hierarchy
  117. @Id Uniqueidentifier -- the id of the current item in the stack
  118.  
  119. SET @lvl = 1
  120.  
  121. CREATE TABLE #stack (item UNIQUEIDENTIFIER, [lvl] INT)
  122. -- we fill fill this table with the ids we want
  123. CREATE TABLE #tmpCategories (Id UNIQUEIDENTIFIER)
  124.  
  125. -- for this example we’ll just select all the ids
  126. -- if we want all the children of a specific parent we would include it’s id in
  127. -- this where clause
  128. INSERT INTO #stack SELECT Id, @lvl FROM Categories WHERE ParentId IS NULL
  129.  
  130. WHILE @lvl > 0
  131. BEGIN -- begin 1
  132.  
  133. IF EXISTS ( SELECT * FROM #stack WHERE lvl = @lvl )
  134. BEGIN -- begin 2
  135.  
  136. SELECT @Id = [item]
  137. FROM #stack
  138. WHERE lvl = @lvl
  139.  
  140. INSERT INTO #tmpCategories
  141. SELECT @Id
  142.  
  143. DELETE FROM #stack
  144. WHERE lvl = @lvl
  145. AND item = @Id
  146.  
  147. INSERT INTO #stack
  148. SELECT Id, @lvl + 1
  149. FROM Categories
  150. WHERE ParentId = @Id
  151.  
  152. IF @@ROWCOUNT > 0
  153. BEGIN -- begin 3
  154. SELECT @lvl = @lvl + 1
  155. END -- end 3
  156. END -- end 2
  157. ELSE
  158. SELECT @lvl = @lvl - 1
  159.  
  160. END -- end 1
  161.  
  162. DROP TABLE #stack
  163.  
  164. SELECT * FROM #tmpCategories
  165. DROP TABLE #tmpCategories
  166. DROP TABLE Categories
Add Comment
Please, Sign In to add comment