【问题标题】:Using LIMIT within GROUP BY to get N results per group twice在 GROUP BY 中使用 LIMIT 两次获得每组 N 个结果
【发布时间】:2018-11-05 19:22:51
【问题描述】:

我在 GROUP BY 中看到了很多“N 个值”,但我需要它两次。我有:

    CREATE TABLE foo(PK INT(11) NOT NULL, Delivery_Date DATE, Hour_Ending TIME(0), 
                     Resource VARCHAR(26), Rate FLOAT, Submit_Time DATETIME(0), Eff_stop DATETIME(0),  PRIMARY KEY (PK));

insert into foo values(1,'2017-01-04','07:00:00','Plant_BAR','10','2017-01-03 05:00:00','2017-01-03 06:22:00'),
(2,'2017-01-04','07:00:00','Plant_BAR','9','2017-01-03 06:00:00','2017-01-03 06:55:00'),
(3,'2017-01-04','07:00:00','Plant_BAR','10','2017-01-03 06:00:00','2017-01-03 08:22:00'),
(4,'2017-01-04','07:00:00','Plant_BAR','10','2017-01-03 07:55:00','2017-01-03 08:53:00'),
(5,'2017-01-04','07:00:00','Plant_BAzz','50','2017-01-03 13:04:00','2017-01-07 06:22:00'),
(6,'2017-01-04','08:00:00','Plant_BAR','10','2017-01-03 05:00:00','2017-01-03 06:22:00'),
(7,'2017-01-04','07:00:00','Plant_BAzz','55','2017-01-03 05:00:00','2017-01-03 06:22:00'),
(8,'2017-01-04','07:00:00','Plant_BAR','0','2017-01-03 10:00:00','2017-01-03 06:22:00');

我需要一个按资源、Delivery_Date、Hour_Ending 划分的有效时间点,这取决于 Submit_Time 和 Eff_Stop。当相同 Resource、Hour_Ending 和 Delivery_Date 的 Subimit_Time 相同时,我遇到了问题。

我想要返回 max(Submit_Time) 行,如果有两个相同的提交时间,我想要 max(Eff_Stop) 行

我有:

SELECT a.PK, a.Delivery_Date, a.Hour_Ending, a.Resource, a.Rate, a.Submit_Time, a.Eff_Stop  
FROM foo as a
INNER JOIN (SELECT Resource, Delivery_Date, Hour_Ending, max(Submit_Time) as max_submit
        FROM foo
        WHERE (Submit_Time < cast(Delivery_Date as datetime)-interval 1 day + interval 7 hour)
                AND (Delivery_Date ='2017-01-04') and (Hour_Ending ='07:00:00')
        GROUP BY Resource, Delivery_Date, Hour_Ending
) as b
ON a.Resource = b.Resource and a.Delivery_Date = b.Delivery_Date and a.Hour_Ending = b.Hour_Ending and a.Submit_Time = b.max_submit
WHERE (a.Delivery_Date ='2017-01-04') and (a.Hour_Ending ='07:00:00')
GROUP BY a.Hour_Ending, a.Delivery_Date, a.Resource;

这让我可以为每个资源返回 1 行,这是最新的 Submit_Time,但是当资源具有相同的 Submit_Time(在本例中为“2017-01-03 06:00:00”)时,我想选择那个与最大值(Eff_Stop)。

结果是:

PK  Delivery_Date   Hour_Ending Resource    Rate    Submit_Time Eff_stop
2   2017-01-04  07:00:00    Plant_BAR   9   2017-01-03T06:00:00Z    2017-01-03T06:55:00Z
7   2017-01-04  07:00:00    Plant_BAzz  55  2017-01-03T05:00:00Z    2017-01-03T06:22:00Z

我想要:

PK  Delivery_Date   Hour_Ending Resource    Rate    Submit_Time Eff_stop
3   2017-01-04  07:00:00    Plant_BAR   10  2017-01-03T06:00:00Z    2017-01-03T08:22:00Z
7   2017-01-04  07:00:00    Plant_BAzz  55  2017-01-03T05:00:00Z    2017-01-03T06:22:00Z

http://sqlfiddle.com/#!9/5cb999/1/0

我尝试了左右连接、两个内连接和一堆其他不起作用的垃圾。

任何帮助将不胜感激!

【问题讨论】:

  • 你的 MySQL 服务器版本是多少?您可以访问 8.0.2 及更高版本吗?
  • 我们正在使用 MySQL 5.6.39 的 AWS RDS 实例上运行。当我转到“修改”时,它最多只能让我选择 5.7.23
  • 如果你使用正确的GROUP BY,你就不会有这个问题。另外,GROUP BY 没有聚合有什么意义?
  • 这里有一个如何正确使用GROUP BY的教程。 techonthenet.com/sql/group_by.php
  • 感谢您的建议。我仍然不在那里。我尝试添加 max(a.Eff_Stop) 而不是 a.Eff_Stop 并且它更改了列,但没有在获得正确的 Submit_Time 后与最大 Eff_Stop 关联的速率

标签: mysql group-by inner-join


【解决方案1】:

我认为这可行:

SELECT a.PK, a.Delivery_Date, a.Hour_Ending, a.Resource, a.Rate, a.Submit_Time, a.Eff_stop  
FROM foo as a
INNER JOIN (SELECT PK, Resource, Delivery_Date, Hour_Ending, Rate, Eff_stop, max(Submit_Time) as max_submit
        FROM foo
        WHERE (Submit_Time < cast(Delivery_Date as datetime)-interval 1 day + interval 7 hour)
                AND (Delivery_Date ='2017-01-04') and (Hour_Ending ='07:00:00')
        GROUP BY Resource, Delivery_Date, Hour_Ending
) as b
ON a.Resource = b.Resource and a.Delivery_Date = b.Delivery_Date and a.Hour_Ending = b.Hour_Ending and a.Submit_Time = b.max_submit
INNER JOIN(SELECT PK, Resource, Delivery_Date, Hour_Ending, Rate, Eff_stop, Submit_Time, max(Eff_Stop) as max_stop
        FROM foo
        WHERE (Submit_Time < cast(Delivery_Date as datetime)-interval 1 day + interval 7 hour)
                AND (Delivery_Date ='2017-01-04') and (Hour_Ending ='07:00:00')
        GROUP BY Resource, Delivery_Date, Hour_Ending, Submit_Time
) as c
ON a.Resource = c.Resource and c.Delivery_Date = a.Delivery_Date and a.Hour_Ending = c.Hour_Ending and a.Eff_Stop = c.max_stop
WHERE (a.Delivery_Date ='2017-01-04') and (a.Hour_Ending ='07:00:00');

【讨论】:

    猜你喜欢
    • 2011-01-08
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多