SELECT s1.ID FROM binventory_ostemp s1 JOIN
( SELECT Cust_FkId, ProcessID, MAX(Service_Duration) AS duration
FROM binventory_ostemp WHERE ProcessID='4d2d6068678bc' AND Overall_Rank IN
(
SELECT MIN(Overall_Rank) FROM binventory_ostemp WHERE ProcessID='4d2d6068678bc' GROUP BY Cust_FkId
)
GROUP BY Cust_FkId
) AS s2 ON s1.Cust_FkId = s2.Cust_FkId AND s1.ProcessID=s2.ProcessID
AND s1.Service_Duration=s2.duration AND s1.ProcessID='4d2d6068678bc'
GROUP BY s1.Cust_FkId
如果该表中的行数超过10K,它就会消失。它的作用是为每个拥有min的客户找到行。整体排名和最高排名给定processid的服务持续时间
表格数据
ID Cust_FkId Overall_Rank Service_Duration ProcessID
1 23 2 30 4d2d6068678bc
2 23 1 45 4d2d6068678bc
3 23 1 60 4d2d6068678bc
4 56 3 90 4d2d6068678bc
5 56 2 50 4d2d6068678bc
6 56 2 85 4d2d6068678bc
结果数据
结果ID值必须 3 且 6
答案 0 :(得分:0)
以下选择可能会更快。
(覆盖)索引
通过查看执行计划来删除未使用的索引。
SQL声明
SELECT b.*
FROM binventory_ostemp b
INNER JOIN (
SELECT b.Cust_FkID
, ovr.Overall_Rank
, MAX(Service_Duration) AS Service_Duration
FROM binventory_ostemp b
INNER JOIN (
SELECT Cust_FkID
, MIN(Overall_Rank) AS Overall_Rank
FROM binventory_ostemp
GROUP BY
Cust_FkID
) ovr ON ovr.Cust_FkID = b.Cust_FKID
AND ovr.Overall_Rank = b.Overall_Rank
GROUP BY
b.Cust_FkID
, ovr.Overall_Rank
) ovrs ON ovrs.Cust_FkID = b.Cust_FkID
AND ovrs.Overall_Rank = b.Overall_Rank
AND ovrs.Service_Duration = b.Service_Duration