Advertisement
mauza

Untitled

Mar 20th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. /* Question #4 - 10 points
  2.  
  3. Write a query that brings back all the stock items (in warehouse.StockItems). Provide
  4. a column that totals sales for each for 2013, 2014, 2015, and 2016. Include the
  5. StockItemName and StockItemID. Sort the list from largest overall sales to
  6. the smallest.
  7.  
  8. You should get 227 records and 6 columns. The first 2 row looks like the following:
  9.  
  10. StockItemID StockItemName 2013Sales 2014Sales 2015Sales 2016Sales
  11. 215 Air cushion machine (Blue) 3612087.90 3688522.65 3815185.95 1657542.15
  12. 173 32 mm Anti static bubble wrap (Blue) 50m 1977885.00 2167462.50 2332890.00 863362.50
  13. */
  14. SELECT
  15. SI.StockItemID,
  16. SI.StockItemName,
  17. SUM(CASE WHEN YEAR(O.OrderDate) = 2013 THEN OL.Quantity * OL.UnitPrice END) [2013Sales],
  18. SUM(CASE WHEN YEAR(O.OrderDate) = 2014 THEN OL.Quantity * OL.UnitPrice END) [2014Sales],
  19. SUM(CASE WHEN YEAR(O.OrderDate) = 2015 THEN OL.Quantity * OL.UnitPrice END) [2015Sales],
  20. SUM(CASE WHEN YEAR(O.OrderDate) = 2016 THEN OL.Quantity * OL.UnitPrice END) [2016Sales]
  21.  
  22. FROM
  23. Sales.Orders O
  24. INNER JOIN Sales.OrderLines OL ON O.OrderID = OL.OrderID
  25. INNER JOIN Warehouse.StockItems SI ON SI.StockItemID = OL.StockItemID
  26.  
  27. GROUP BY
  28. SI.StockItemID,
  29. SI.StockItemName
  30.  
  31. ORDER BY
  32. SUM(OL.Quantity * OL.UnitPrice) DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement