I am using the following SQL Statement to summarize my sales history data by Qty sold for each month.
WITH StockSales AS
(
SELECT
CASE
WHEN Month(PostST.TxDate) = 1 THEN 'Jan'
WHEN Month(PostST.TxDate) = 2 THEN 'Feb'
WHEN Month(PostST.TxDate) = 3 THEN 'Mar'
WHEN Month(PostST.TxDate) = 4 THEN 'Apr'
WHEN Month(PostST.TxDate) = 5 THEN 'May'
WHEN Month(PostST.TxDate) = 6 THEN 'Jun'
WHEN Month(PostST.TxDate) = 7 THEN 'Jul'
WHEN Month(PostST.TxDate) = 8 THEN 'Aug'
WHEN Month(PostST.TxDate) = 9 THEN 'Sept'
WHEN Month(PostST.TxDate) = 10 THEN 'Oct'
WHEN Month(PostST.TxDate) = 11 THEN 'Nov'
WHEN Month(PostST.TxDate) = 12 THEN 'Dec'
END AS MonthSold,
YEAR(PostST.TxDate) AS YearSold,
CONCAT(StkItem.Description_1, ' - ', StkItem.Code) AS Item,
CASE
WHEN PostST.TrCodeID = 30 THEN PostST.Quantity * -1
WHEN PostST.TrCodeID = 34 THEN PostST.Quantity * 1
ELSE 0
END AS QtySold
FROM
StkItem
INNER JOIN
PostST ON PostST.AccountLink = StkItem.StockLink
WHERE
PostST.TrCodeID IN (34, 30)
)
SELECT
StockSales.MonthSold,
StockSales.YearSold,
StockSales.Item,
SUM (StockSales.QtySold) AS QtySold
FROM
StockSales
GROUP BY
StockSales.QtySold, StockSales.MonthSold, StockSales.YearSold, StockSales.Item
I get the following format:
However, what I want is the Item to only appear once (grouped) and then the each month and year to be a separate field with the SUM of QtySold under each month.
As an example, I have just set it up in Excel how I want it to look:
Is there a way of setting up the field differently?
Thank you :)