简体   繁体   English

基于月列对数据进行分组的最佳方法是什么?

[英]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. 因此,基本上,我正在制作一个报告,显示从1月到7月任何给定月份内所有产品的最高价格。理想情况下,我希望每个PartNo 1行,并显示每个月的最高价格。 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. 因此,如果您查看结果并查看PartNo CAB-01,您不仅可以看到它不是跨列分组,而且在同一个月内它也没有分组。 What am I doing wrong? 我究竟做错了什么? Thx in advance 提前Thx

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 . 您希望每个零件号都有一行,但是在GROUP BY子句中,您告诉它在每个零件号条目发生在的唯一日期每个行产生一行: 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'; 很显然,CAB-01项目在7月有两个不同的日期,因此它有两行的值在'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: 要获得所需的内容,需要从GROUP BYORDER BY子句中删除Pd.DateEnt ,然后将MAX()CASE语句的内部移到外部,以使GROUP BY仍然有效:

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; 而且,您根本没有在查询中使用ESTIM表,如图所示,因此您可能希望将JOIN删除掉。 and if you want to include ALL of 2018, you should change your WHERE condition to WHERE p.DateEnt >= '20180101' . 如果要包括2018年的全部内容,则应将WHERE条件更改为WHERE p.DateEnt >= '20180101'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在SQL中保存大型每月数据备份的最佳方法是什么? - What's the best way to save large monthly data backups in SQL? 将所有日期时间列批量更新为 UTC 的最佳方法是什么 - What would be the best way to bulk update All Datetime columns to UTC 分组和汇总树数据的最佳方法是什么? - What is the best way to group and aggregate and sum tree data? Group by 月度数据查询 - Monthly Data Query With Group by 链接这两个表的最佳方法是什么 - What would be the best way to link these two tables 在两个位置之间传输我的(小)SQL Server数据库的最佳方法是什么? - What would be the best way to transfer my (small) SQL Server database between 2 locations? 给定以下数据,编写查询以生成表的最佳方法是什么? - What would be the best way to write a query to produce a table given the following data? 从我的SQL DB数据构建动态报告的最佳方法是什么? - What would be the best method for building Dynamic Reports from my SQL DB Data? 当经常使用两个表中的数据时,在表之间创建关系的最佳选择是什么? - What would be my best option for creating a relationship between tables when the data from both tables will be used often? 使用此SQL语句按用户名分组的最佳方法是什么? - What is the best way to group by username with this SQL statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM