我想对“钱”列求和,但我想要状态的组名和代码。
这是存储过程代码[Sql Server 2008]:
SELECT um.upmoney as 'money'
,um.pId as 'code',um.FName as 'name',up.status as 'statusmoney'
From tb_upmoney um inner join tb_pID up on up.uId=um.pId
结果:
money | code | name | statusmoney
200.00 | 00001 | fin | 1
100.00 | 00001 | fin | 1
50.00 | 00001 | fin | 1
100.00 | 00002 | welson | 1
200.00 | 00002 | welson | 2
100.00 | 00002 | welson | 2
50.00 | 00002 | welson | 2
0.00 | 00002 | welson | 2
,但我想要:
money | code | name | statusmoney
250.00 | 00001 | fin | 1
100.00 | 00002 | welson | 1
250.00 | 00002 | welson | 2
最佳答案
您需要SUM(UM.upmoney)
并将其余字段分组,如下所示:
SELECT SUM(um.upmoney) AS 'money',
um.pId AS 'code',
um.FName AS 'name',
up.status AS 'statusmoney'
FROM tb_upmoney um
INNER JOIN tb_pID up
ON up.uId=um.pId
GROUP BY um.pId, um.FName, up.status
请参阅GROUP BY clause with an aggregator 'SUM()'的示例
关于sql - 存储过程中如何用名称求和和分组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11980704/