我已经看到许多“ 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');
我需要一个按时间,资源,交货日期,小时结束时间有效费率的点,这取决于Submit_Time和Eff_Stop。当Subimit_Time对于相同的Resource,Hour_Ending和Delivery_Date相同时,我遇到了问题。
我希望返回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')时,我想选择一个与max(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
我尝试了左右连接,两个内部连接以及一堆其他无效的垃圾。
任何帮助将不胜感激!
答案 0 :(得分:0)
我认为这可行:
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');