仅 return 名在最近一次支付期间超过 25 岁的员工
Only return employees who are over 25 from the latest pay period
我有这个查询,我只想要最近的 RunID 上的员工以及他们超过 25 岁的位置。正如您从我的结果中看到的那样,它返回超过 25 岁的员工和他们最近的 RunID。我只想要来自最后一个 RunID(2131) 的员工。下周 RunID 将为 (2132),并且每周都会增加。有没有办法只让员工使用最后一个 RunID?真的卡在了这一点上。这是我查询的图片。
SELECT DISTINCT
ed.EeID,
CONCAT(ed.Forename, '', ed.Surname) AS 'Name',
MAX(pr.RunID)
FROM EeDetails ed
INNER JOIN EeRunBals erb on erb.EeID = ed.EeID
INNER JOIN PayrollRuns pr on pr.RunID = erb.RunID
WHERE ed.BirthDate < '1994-01-01'
--WHERE pr.RunID = MAX(pr.RunID)
GROUP BY ed.EeID, ed.Forename, ed.Surname, ed.BirthDate
ORDER BY ed.EeId
只使用聚合怎么样?
SELECT ed.EeID, ed.Forename, ed.Surname, ed.BirthDate, MAX(pr.RunId)
FROM EeDetails ed INNER JOIN
EeRunBals erb
ON erb.EeID = ed.EeID INNER JOIN
PayrollRuns pr
ON pr.RunID = erb.RunID
WHERE ed.BirthDate < '1994-01-01'
GROUP BY ed.EeID, ed.Forename, ed.Surname, ed.BirthDate
ORDER BY ed.EeId;
哦,我明白了,你不想要最后一个 运行 id 每个人 。你想要最后一个整体。在那种情况下:
SELECT TOP (1) WITH TIES . . .
FROM EeDetails ed INNER JOIN
EeRunBals erb
ON erb.EeID = ed.EeID INNER JOIN
PayrollRuns pr
ON pr.RunID = erb.RunID
WHERE ed.BirthDate < '1994-01-01'
ORDER BY pr.RunId DESC;
这实际上是获取出生日期小于 '1994-01-01'
的人的最后一个 运行 id。
您可以使用:
DECLARE @MaxRunID = (select MAX(RunID) from PayrollRuns)
SELECT DISTINCT
ed.EeID,
CONCAT(ed.Forename, '', ed.Surname) AS 'Name',
MAX(pr.RunID)
FROM EeDetails ed
INNER JOIN EeRunBals erb on erb.EeID = ed.EeID
INNER JOIN PayrollRuns pr on pr.RunID = erb.RunID
WHERE ed.BirthDate < '1994-01-01'
AND pr.RunID = @MaxRunID
GROUP BY ed.EeID, ed.Forename, ed.Surname, ed.BirthDate
ORDER BY ed.EeId
或者,您可以绕过变量而只放置子查询,尽管变量更好。
您可以使用 having 子句来过滤聚合值,即 max runid。您还需要使用子查询来获取最大 runid,因为在父查询中 MAX(pr.RunID)
实际上是员工(分组依据)而不是所有员工的最大 runid。在您的代码中,您可以像这样使用查询:
SELECT DISTINCT
ed.EeID,
CONCAT(ed.Forename, '', ed.Surname) AS 'Name',
MAX(pr.RunID)
FROM EeDetails ed
INNER JOIN EeRunBals erb on erb.EeID = ed.EeID
INNER JOIN PayrollRuns pr on pr.RunID = erb.RunID
WHERE ed.BirthDate < '1994-01-01'
GROUP BY ed.EeID, ed.Forename, ed.Surname, ed.BirthDate
HAVING MAX(pr.RunID)=(select MAX(RunID) from PayrollRuns)
ORDER BY ed.EeId
希望对您有所帮助。
我有这个查询,我只想要最近的 RunID 上的员工以及他们超过 25 岁的位置。正如您从我的结果中看到的那样,它返回超过 25 岁的员工和他们最近的 RunID。我只想要来自最后一个 RunID(2131) 的员工。下周 RunID 将为 (2132),并且每周都会增加。有没有办法只让员工使用最后一个 RunID?真的卡在了这一点上。这是我查询的图片。
SELECT DISTINCT
ed.EeID,
CONCAT(ed.Forename, '', ed.Surname) AS 'Name',
MAX(pr.RunID)
FROM EeDetails ed
INNER JOIN EeRunBals erb on erb.EeID = ed.EeID
INNER JOIN PayrollRuns pr on pr.RunID = erb.RunID
WHERE ed.BirthDate < '1994-01-01'
--WHERE pr.RunID = MAX(pr.RunID)
GROUP BY ed.EeID, ed.Forename, ed.Surname, ed.BirthDate
ORDER BY ed.EeId
只使用聚合怎么样?
SELECT ed.EeID, ed.Forename, ed.Surname, ed.BirthDate, MAX(pr.RunId)
FROM EeDetails ed INNER JOIN
EeRunBals erb
ON erb.EeID = ed.EeID INNER JOIN
PayrollRuns pr
ON pr.RunID = erb.RunID
WHERE ed.BirthDate < '1994-01-01'
GROUP BY ed.EeID, ed.Forename, ed.Surname, ed.BirthDate
ORDER BY ed.EeId;
哦,我明白了,你不想要最后一个 运行 id 每个人 。你想要最后一个整体。在那种情况下:
SELECT TOP (1) WITH TIES . . .
FROM EeDetails ed INNER JOIN
EeRunBals erb
ON erb.EeID = ed.EeID INNER JOIN
PayrollRuns pr
ON pr.RunID = erb.RunID
WHERE ed.BirthDate < '1994-01-01'
ORDER BY pr.RunId DESC;
这实际上是获取出生日期小于 '1994-01-01'
的人的最后一个 运行 id。
您可以使用:
DECLARE @MaxRunID = (select MAX(RunID) from PayrollRuns)
SELECT DISTINCT
ed.EeID,
CONCAT(ed.Forename, '', ed.Surname) AS 'Name',
MAX(pr.RunID)
FROM EeDetails ed
INNER JOIN EeRunBals erb on erb.EeID = ed.EeID
INNER JOIN PayrollRuns pr on pr.RunID = erb.RunID
WHERE ed.BirthDate < '1994-01-01'
AND pr.RunID = @MaxRunID
GROUP BY ed.EeID, ed.Forename, ed.Surname, ed.BirthDate
ORDER BY ed.EeId
或者,您可以绕过变量而只放置子查询,尽管变量更好。
您可以使用 having 子句来过滤聚合值,即 max runid。您还需要使用子查询来获取最大 runid,因为在父查询中 MAX(pr.RunID)
实际上是员工(分组依据)而不是所有员工的最大 runid。在您的代码中,您可以像这样使用查询:
SELECT DISTINCT
ed.EeID,
CONCAT(ed.Forename, '', ed.Surname) AS 'Name',
MAX(pr.RunID)
FROM EeDetails ed
INNER JOIN EeRunBals erb on erb.EeID = ed.EeID
INNER JOIN PayrollRuns pr on pr.RunID = erb.RunID
WHERE ed.BirthDate < '1994-01-01'
GROUP BY ed.EeID, ed.Forename, ed.Surname, ed.BirthDate
HAVING MAX(pr.RunID)=(select MAX(RunID) from PayrollRuns)
ORDER BY ed.EeId
希望对您有所帮助。