我有一张名为UserId
的表格。我希望对该值求和并按+--------+----------+-------+
| UserId | UserName | Value |
+--------+----------+-------+
| 1 | A | 100 |
| 1 | B | 200 |
| 1 | C | 300 |
| 2 | D | 100 |
| 3 | E | 200 |
| 3 | F | 300 |
+--------+----------+-------+
分组。
tblEmployee:
userid
我想根据 +--------+----------+-------+---------+
| UserId | UserName | Value | Result |
+--------+----------+-------+---------+
| 1 | A | 100 | |
| 1 | B | 200 | 600 |
| 1 | C | 300 | |
| 2 | D | 100 | 100 |
| 3 | E | 200 | |
| 3 | F | 300 | 500 |
+--------+----------+-------+---------+
对值求和,并显示如下输出。
输出:
var entity=db.(your databasetablename).Create();
怎么做?
答案 0 :(得分:2)
select
userid,username,
value,
sum(value) over (partition by userid) as totalval
from table
答案 1 :(得分:2)
如果您不想使用分区,还可以使用带有GROUP BY
的子查询来获得所需的输出:
SELECT t1.UserId, t1.UserName, t1.Value, t2.Result
FROM tblEmployee t1
INNER JOIN
(
SELECT UserId, SUM(Value) AS Result
FROM tblEmployee
GROUP BY UserId
) t2
ON t1.UserId = t2.UserId