Question
Convert this static pivot query to a dynamic pivot query. I am trying to list each product category and the number of units sold by
Convert this static pivot query to a dynamic pivot query. I am trying to list each product category and the number of units sold by month.You should have months as headers across the top and product categories down the side
SELECT * FROM (SELECT pc.Name AS CategoryName, sod.OrderQty, DATENAME(mm,soh.OrderDate) AS ORDERMONTH FROM Production.ProductCategory pc INNER JOIN Production.ProductSubcategory sc ON pc.ProductCategoryID = sc.ProductCategoryID INNER JOIN Production.Product pr ON sc.ProductSubcategoryID = pr.ProductSubcategoryID INNER JOIN sales.SalesOrderDetail sod ON pr.ProductID = sod.ProductID INNER JOIN sales.SalesOrderHeader soh ON sod.SalesOrderID = soh.SalesOrderID) dataTable PIVOT (SUM(OrderQty) FOR ORDERMONTH IN ([January], [February], [March], [April], [May], [June], [July], [August], [September], [October], [November],[December]) )AS PivotTable ORDER BY CategoryName
link to schema: https://i.stack.imgur.com/LMu4W.gif
AdventureWorks 2008 OLTP Schema Sales HumanResourccs Person Production Schemas Sales Purchasing Person Production AdventureWorks 2008 OLTP Schema Sales HumanResourccs Person Production Schemas Sales Purchasing Person Production
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started