3 回答
TA贡献1828条经验 获得超6个赞
你可以这样做GROUP BY LEFT(employeeidno, 4):
SELECT
LEFT(employeeidno, 4) deptcode,
MAX(RIGHT(employeeidno, 7)) empno
FROM employeemasterfile
GROUP BY LEFT(employeeidno, 4)
ORDER BY LEFT(employeeidno, 4)
TA贡献1780条经验 获得超1个赞
你可以使用 row_number()
select deptcode,empno from
( select LEFT (employeeidno, 4) deptcode,
RIGHT (employeeidno, 7) empno,
row_number()over(partition by LEFT (employeeidno, 4) ordere by RIGHT (employeeidno, 7) desc)
from employeemasterfile
) a where a.rn=1
TA贡献1841条经验 获得超3个赞
据我了解,聚合应该这样做。
SELECT left(employeeidno, 4) deptcode,
max(right(employeeidno, 7)) empno
FROM employeemasterfile
GROUP BY left(employeeidno, 4)
ORDER BY left(employeeidno, 4);
编辑:
我会试着解释一下:
您可以想象GROUP BY left(employeeidno, 4)将记录集划分为子集。在每个子集中left(employeeidno, 4),即deptno是相同的,并且没有两个子集具有相同的deptno。现在在每个子集中max(right(employeeidno, 7))取最大值right(employeeidno, 7)即最大值empno。(ORDER BY empno DESC将结果限制为一行TOP 1也会使您获得最大值。)然后通过获取deptno每个子集(即每个子集deptno)的 和最大值 来产生最终结果empno。
- 3 回答
- 0 关注
- 168 浏览
添加回答
举报