我有一张桌子,我正在存储员工的休假信息,我想计算员工休假后到目前为止所用的总叶数。 我尝试过查询,但不是为每个员工分组,而是继续添加所有以前的行值。
CREATE TABLE SampleLeave
(
ID Numeric,
EmpCode Numeric,
NoOfLeaves Numeric
);
INSERT INTO SampleLeave VALUES (1, 12, 2);
INSERT INTO SampleLeave VALUES (2, 12, 1);
INSERT INTO SampleLeave VALUES (3, 12, 3);
INSERT INTO SampleLeave VALUES (4, 1, 1);
INSERT INTO SampleLeave VALUES (5, 1, 5);
SELECT *, SUM([NoOfLeaves]) OVER (ORDER BY [ID]) Leaves FROM SampleLeave
这是我得到的输出,而我希望Sum函数计算员工ID不同时的叶数。
ID EmpCode NoOfLeaves Leaves
--------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
1 12 2 2
2 12 1 3
3 12 3 6
4 1 1 7
5 1 5 12
我期待的输出:
ID EmpCode NoOfLeaves Leaves
--------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
1 12 2 2
2 12 1 3
3 12 3 6
4 1 1 1
5 1 5 6
答案 0 :(得分:2)
您需要添加seq_no
子句才能根据partition
EmpCode