必須找到總總銷售額作出並獲得總折扣,在2002年及2002年銷售集中......樞軸還是轉置?
表1還當月:的SalesOrderDetail
- UnitPrice
- UnitPriceDiscount
- OrderQty
表2:SalesOrderHeader的
- OrderDate
附上的是這兩個表的模式的捕捉
https://i.stack.imgur.com/f2b5A.png
我能設法得到的結果seperately但不理解如何 結合/聚集他們....是否使用轉或轉動......
#Yearly
select DATE_FORMAT(a.OrderDate,'%Y') AS Yearly,sum(b.UnitPriceDiscount) as DiscTotal,
SUM((`UnitPrice` * OrderQty)-UnitPriceDiscount) AS total
FROM SalesOrderHeader a join SalesOrderDetail b
on b.SalesOrderID = a.SalesOrderID
WHERE YEAR(OrderDate) = 2002
group by Yearly
order by MONTH(OrderDate)
#CurMonthOfYear
select DATE_FORMAT(a.OrderDate,'%M') AS CurrentMonth,sum(b.UnitPriceDiscount) as DiscountAmt,
SUM((`UnitPrice` * OrderQty)-UnitPriceDiscount) AS total
FROM SalesOrderHeader a join SalesOrderDetail b
on b.SalesOrderID = a.SalesOrderID
WHERE month(OrderDate) = MONTH(NOW())
**的結果應該是以下格式**
Metrics Yearly CurMonthOfYear
DiscountAmt 540.82 28.48
TotalSales 303.17 237.2
@ Pierre.Vriens它沒有透視或轉置,但查詢有助於獲得所需格式的輸出。 –