MySQL:选择一个组的Last Max()值,但由另一个组汇总

时间:2018-07-12 17:13:58

标签: mysql sql

我正在尝试通过此数据集按雇员和QA_Score的Date_Time的最后值列出Company_Name:

+--------------+-----------+------------------+-----------+----------+
| Company_Name | Wing_Name |    Date_Time     | Employees | QA_Score |
+--------------+-----------+------------------+-----------+----------+
| Company A    | Wing A    | 06/08/2018 13:00 |        82 | 3.5      |
| Company A    | Wing A    | 06/08/2018 13:01 |        83 | 3.6      |
| Company A    | Wing A    | 06/08/2018 13:02 |        84 | 3.7      |
| Company B    | Wing A    | 06/08/2018 14:00 |        82 | 3.6      |
| Company B    | Wing A    | 06/08/2018 14:01 |        85 | 3.7      |
| Company B    | Wing A    | 06/08/2018 14:02 |        88 | 3.8      |
+--------------+-----------+------------------+-----------+----------+

所需的输出应为:

+--------------+-----------+----------+
| Company_Name | Employees | QA_Score |
+--------------+-----------+----------+
| Company A    |        84 | 3.7      |
| Company B    |        88 | 3.8      |
+--------------+-----------+----------+

我已经成功地使用以下代码显示了Wing_Name的Date_Time的最后结果,但是我一生都无法为Company_Name找出它,而将Wing_Name排除在输出之外。

SELECT * FROM table
WHERE (Wing_Name, Date_Time) IN
(SELECT Wing_Name, MAX(Date_Time) Date_Time FROM table GROUP BY Wing_Name)
AND `Company_Name` = "Company A";

这一定是我所缺少的简单东西吗? 预先谢谢你。

编辑:

我相信我可能没有正确地问过我的问题,并且已经改进了数据集列表,以根据Wing_Name和我的输出意图提供更多颜色:

+--------------+-----------+------------------+-----------+----------+
| Company_Name | Wing_Name |    Date_Time     | Employees | QA_Score |
+--------------+-----------+------------------+-----------+----------+
| Company A    | Wing A    | 06/08/2018 13:00 |        82 | 3.5      |
| Company A    | Wing A    | 06/08/2018 13:01 |        83 | 3.6      |
| Company A    | Wing A    | 06/08/2018 13:02 |        84 | 3.7      |
| Company A    | Wing B    | 06/08/2018 13:00 |        50 | 4.1      |
| Company A    | Wing B    | 06/08/2018 13:01 |        51 | 4.2      |
| Company A    | Wing B    | 06/08/2018 13:02 |        52 | 4.3      |
| Company B    | Wing A    | 06/08/2018 14:00 |        82 | 3.6      |
| Company B    | Wing A    | 06/08/2018 14:01 |        85 | 3.7      |
| Company B    | Wing A    | 06/08/2018 14:02 |        88 | 3.8      |
+--------------+-----------+------------------+-----------+----------+

通过此编辑,所需的输出现在应该是通过每个Wing_name的结果得出的Company_Name员工总数,以及相应Wing_Name的QA_Score平均值,但汇总在Company_Name下

使用上面的新修订的数据集,所需的输出应为:

  • A公司和A部门雇员的上次Date_Time值为84。
  • A公司和B部门员工的上次Date_Time值为52。
  • 截至上次Date_Time检索,公司A有84 + 52名员工(136)。
  • A公司和Wing A的QA_Score的最新Date_Time值为3.7。
  • A公司和机翼B的QA_Score的上次Date_time值为4.3。
  • 公司A公司A的平均质量检查得分为4.0
+--------------+-----------+----------+
| Company_Name | Employees | QA_Score |
+--------------+-----------+----------+
| Company A    |       136 | 4        |
| Company B    |        88 | 3.8      |
+--------------+-----------+----------+

4 个答案:

答案 0 :(得分:0)

我认为您只需要添加公司名称:

SELECT t.*
FROM table t
WHERE (Company_name, Wing_Name, Date_Time) IN
        (SELECT CompanyName, Wing_Name, MAX(Date_Time) Date_Time
         FROM table
         GROUP BY Company_Name, Wing_Name
        );

答案 1 :(得分:0)

使用与现有sql类似的逻辑:

SELECT * FROM table
WHERE (Company_Name, Date_Time) IN
(SELECT Company_Name, MAX(Date_Time) Date_Time FROM table GROUP BY Company_Name);

或使用相关子查询:

SELECT * 
FROM table t1
WHERE Date_Time = (SELECT Max(Date_Time) FROM Table WHERE t1.Company_Name = Company_Name);

答案 2 :(得分:0)

或者是未解决的解决方案...

SELECT x.* 
  FROM my_table x 
  JOIN
     ( SELECT company_name
            , wing_name
            , MAX(date_time) dt 
         FROM my_table 
        GROUP 
           BY company_name
            , wing_name
     ) y
    ON y.company_name = x.company_name 
   AND y.wing_name = x.wing_name 
   AND y.dt = x.date_time;

答案 3 :(得分:0)

您可以将子查询与GROUP BYSUMAVG一起使用,请看下面的代码,并且sqlfiddle示例已更新:

 select Company_Name,sum(Employees),avg(QA_Score) from Table1 where (Company_Name,Date_Time) 
    in (
    select Company_Name,Max(Date_Time)  from Table1 group by Company_Name  
    )
    group by Company_Name

下面是一个sqlfiddle示例:https://www.db-fiddle.com/f/27SuaGjL3SKfxhvqRRWUdd/3