我有这个查询
SELECT t.pname,MAX(t.name) ,MAX(t.total)
FROM
(
SELECT p.`id`,e.`name`,p.`pname`,(m.`hour`) AS total
FROM employee e INNER JOIN epmap m ON m.`employeeID`=e.`id` INNER JOIN project p ON p.`id`=m.`projectID`
)t
GROUP BY t.id
它给出了正确的答案,但这不是好的方法,因为Max(t.name)
不合适
答案 0 :(得分:0)
这将为您提供所需的结果:
SELECT t.pname, t.name, t.hour as total
FROM (
SELECT p.id, e.name, p.pname, m.total,
ROW_NUMBER() OVER(partition by p.id order by m.hour desc) rn
FROM employee e
INNER JOIN epmap m ON m.employeeID=e.id
INNER JOIN project p ON p.id=m.projectID
) t
where t.rn = 1
答案 1 :(得分:0)
SELECT DISTINCT可能会让你摆脱名字上的MAX。