如何在WHERE子句中使用MySql MAX()

时间:2016-04-27 09:03:07

标签: mysql sql select

我有一个名为tbservicecallallocation的表,下面是该表的数据。

enter image description here

从上面的数据我想要技术人员的MAX AllocationTime。 下图显示了我想要的结果.. enter image description here

请帮我编写MySQL SELECT查询以检索上述数据。

5 个答案:

答案 0 :(得分:5)

有一个子查询来返回每个技术人员的最大分配时间。加入这个结果:

select t1.*
from tbservicecallallocation t1
join (select TechnicianIDF, max(AllocationTime) as MAxAllocationTime
      from tbservicecallallocation
      group by TechnicianIDF) t2
  on  t1.TechnicianIDF = t2.TechnicianIDF
  and t1.AllocationTime = t2.MAxAllocationTime

可选择在末尾添加ORDER BY子句:

ORDER BY AllocationStatus DESC

答案 1 :(得分:4)

试试这个:

SELECT *
FROM tbservicecallallocation t1
WHERE NOT EXISTS(
    SELECT 'NEXT'
    FROM tbservicecallallocation t2
    WHERE t1.TechnicianIDF = t2.TechnicianIDF
    AND t2.AllocationTime > t1.AllocationTime
)

答案 2 :(得分:3)

您可以使用子查询来计算最大AllocationTime

SELECT *
FROM tbservicecallallocation t1
WHERE (TechnicianIDF,AllocationTime) IN
      (SELECT TechnicianIDF, MAX(AllocationTime) AS AllocationTime
       FROM tbservicecallallocation
       GROUP BY TechnicianIDF)

答案 3 :(得分:3)

试试这个

select t1.*
from tbservicecallallocation as t1 inner join
(
 select technicianidf, max(allocationtime) as allocationtime
       from tbservicecallallocation
       group by technicianidf
) as t2 on t1.technicianidf=t2.technicianidf and t1.allocationtime=t2.allocationtime

答案 4 :(得分:0)

试试这个

SELECT * FROM tbservicecallallocation t1
WHERE (TechnicianIDF,AllocationTime) IN
   (SELECT TechnicianIDF, MAX(AllocationTime) AS AllocationTime
    FROM tbservicecallallocation
    GROUP BY TechnicianIDF)