Advertisement
selvalives

Untitled

Mar 13th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. -- Demonstration A
  2.  
  3. -- Step 1: Open a new query window to the TSQL database
  4. USE TSQL;
  5. GO
  6.  
  7. -- Step 2: Creating Windows with OVER
  8.  
  9. -- Setup views for demo
  10. IF OBJECT_ID('Production.CategorizedProducts','V') IS NOT NULL DROP VIEW Production.CategorizedProducts
  11. GO
  12. CREATE VIEW Production.CategorizedProducts
  13. AS
  14. SELECT Production.Categories.categoryid AS CatID,
  15. Production.Categories.categoryname AS CatName,
  16. Production.Products.productname AS ProdName,
  17. Production.Products.unitprice AS UnitPrice
  18. FROM Production.Categories
  19. INNER JOIN Production.Products ON Production.Categories.categoryid=Production.Products.categoryid;
  20. GO
  21. IF OBJECT_ID('Sales.CategoryQtyYear','V') IS NOT NULL DROP VIEW Sales.CategoryQtyYear
  22. GO
  23. CREATE VIEW Sales.CategoryQtyYear
  24. AS
  25. SELECT c.categoryname AS Category,
  26. SUM(od.qty) AS Qty,
  27. YEAR(o.orderdate) AS Orderyear
  28. FROM Production.Categories AS c
  29. INNER JOIN Production.Products AS p ON c.categoryid=p.categoryid
  30. INNER JOIN Sales.OrderDetails AS od ON p.productid=od.productid
  31. INNER JOIN Sales.Orders AS o ON od.orderid=o.orderid
  32. GROUP BY c.categoryname, YEAR(o.orderdate);
  33. GO
  34.  
  35. -- Step 3: Using OVER with ordering
  36. -- Rank products by price from high to low
  37. SELECT CatID, CatName, ProdName, UnitPrice,
  38. RANK() OVER(ORDER BY UnitPrice DESC) AS PriceRank
  39. FROM Production.CategorizedProducts
  40. ORDER BY PriceRank;
  41.  
  42. -- Rank products by price in descending order in each category.
  43. -- Note the ties.
  44. SELECT CatID, CatName, ProdName, UnitPrice,
  45. RANK() OVER(PARTITION BY CatID ORDER BY UnitPrice DESC) AS PriceRank
  46. FROM Production.CategorizedProducts
  47. ORDER BY CatID;
  48.  
  49. -- Step 4: Use framing to create running total
  50. -- Display a running total of quantity per product category.
  51. -- This uses framing to set boundaries at the start
  52. -- of the set and the current row, for each partition
  53. SELECT Category, Qty, Orderyear,
  54. SUM(Qty) OVER (
  55. PARTITION BY category
  56. ORDER BY orderyear
  57. ROWS BETWEEN UNBOUNDED PRECEDING
  58. AND CURRENT ROW) AS RunningQty
  59. FROM Sales.CategoryQtyYear;
  60.  
  61.  
  62. -- Display a running total of quantity per year.
  63. SELECT Category, Qty, Orderyear,
  64. SUM(Qty) OVER (
  65. PARTITION BY orderyear
  66. ORDER BY Category
  67. ROWS BETWEEN UNBOUNDED PRECEDING
  68. AND CURRENT ROW) AS RunningQty
  69. FROM Sales.CategoryQtyYear;
  70.  
  71. -- Show both side-by-side per category and per-year
  72.  
  73. SELECT Category, Qty, Orderyear,
  74. SUM(Qty) OVER (PARTITION BY orderyear ORDER BY Category ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningTotalByYear,
  75. SUM(Qty) OVER (PARTITION BY Category ORDER BY OrderYear ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningTotalByCategory
  76. FROM Sales.CategoryQtyYear
  77. ORDER BY Orderyear, Category;
  78.  
  79. -- Step 5: Clean up
  80. IF OBJECT_ID('Production.CategorizedProducts','V') IS NOT NULL DROP VIEW Production.CategorizedProducts
  81. IF OBJECT_ID('Sales.CategoryQtyYear','V') IS NOT NULL DROP VIEW Sales.CategoryQtyYear
  82. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement