简体   繁体   中英

What would be the best way to group my data that is based on monthly columns?

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

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' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM