Reputation: 485
So basically, I'm making a report that shows the max price for all products for any given month, from Jan to Jul. Ideally, I would like 1 row per PartNo and for it to show the max price from month to month. Here's my code and photo of result below:
SELECT
pd.PartNo,
CASE
WHEN MONTH(p.DateEnt) = 1 AND YEAR(p.DateEnt) = 2018 THEN MAX(pd.UnitCost)
END AS [Jan 2018],
CASE
WHEN MONTH(p.DateEnt) = 2 AND YEAR(p.DateEnt) = 2018 THEN MAX(pd.UnitCost)
END AS [Feb 2018],
CASE
WHEN MONTH(p.DateEnt) = 3 AND YEAR(p.DateEnt) = 2018 THEN MAX(pd.UnitCost)
END AS [Mar 2018],
CASE
WHEN MONTH(p.DateEnt) = 4 AND YEAR(p.DateEnt) = 2018 THEN MAX(pd.UnitCost)
END AS [Apr 2018],
CASE
WHEN MONTH(p.DateEnt) = 5 AND YEAR(p.DateEnt) = 2018 THEN MAX(pd.UnitCost)
END AS [May 2018],
CASE
WHEN MONTH(p.DateEnt) = 6 AND YEAR(p.DateEnt) = 2018 THEN MAX(pd.UnitCost)
END AS [Jun 2018],
CASE
WHEN MONTH(p.DateEnt) = 7 AND YEAR(p.DateEnt) = 2018 THEN MAX(pd.UnitCost)
END AS [Jul 2018]
FROM PODet pd
JOIN PO p ON pd.PONum = p.PONum
JOIN Estim e ON pd.PartNo = e.PartNo
WHERE p.DateEnt > '20180101'
GROUP BY pd.PartNo, p.DateEnt
ORDER BY pd.PartNo, p.DateEnt DESC
So if you look at the results and look at let's say PartNo CAB-01, you can see that not only is it not grouping across columns, but within the same month itself, it's also not grouping. What am I doing wrong? Thx in advance
Upvotes: 0
Views: 66
Reputation: 1740
You'd like to have one row per part number, but in your GROUP BY
clause you told it to produce one row per unique date that each part number entry occurs on: GROUP BY Pd.PartNo, Pd.DateEnt
. The CAB-01 item apparently has two different dates in July associated with it, so it has two rows with values under 'Jul 2018'; you just can't tell that they're for two different dates because the exact date is not part of the output.
To get what you want, you need to remove Pd.DateEnt
from your GROUP BY
and ORDER BY
clauses, and move your MAX()
from the inside to the outside of your CASE
statements so that the GROUP BY
is still valid:
SELECT
pd.PartNo,
MAX(CASE
WHEN MONTH(p.DateEnt) = 1 AND YEAR(p.DateEnt) = 2018 THEN pd.UnitCost
END) AS [Jan 2018],
MAX(CASE
WHEN MONTH(p.DateEnt) = 2 AND YEAR(p.DateEnt) = 2018 THEN pd.UnitCost
END) AS [Feb 2018],
MAX(CASE
WHEN MONTH(p.DateEnt) = 3 AND YEAR(p.DateEnt) = 2018 THEN pd.UnitCost
END) AS [Mar 2018],
MAX(CASE
WHEN MONTH(p.DateEnt) = 4 AND YEAR(p.DateEnt) = 2018 THEN pd.UnitCost
END) AS [Apr 2018],
MAX(CASE
WHEN MONTH(p.DateEnt) = 5 AND YEAR(p.DateEnt) = 2018 THEN pd.UnitCost
END) AS [May 2018],
MAX(CASE
WHEN MONTH(p.DateEnt) = 6 AND YEAR(p.DateEnt) = 2018 THEN pd.UnitCost
END) AS [Jun 2018],
MAX(CASE
WHEN MONTH(p.DateEnt) = 7 AND YEAR(p.DateEnt) = 2018 THEN pd.UnitCost
END) AS [Jul 2018]
FROM PODet pd
JOIN PO p ON pd.PONum = p.PONum
JOIN Estim e ON pd.PartNo = e.PartNo
WHERE p.DateEnt > '20180101'
GROUP BY pd.PartNo
ORDER BY pd.PartNo
Also, you're not using the ESTIM table at all in the query as shown, so you may want to take that JOIN out; and if you want to include ALL of 2018, you should change your WHERE
condition to WHERE p.DateEnt >= '20180101'
.
Upvotes: 2