gpt4 book ai didi

sql - 如何在存储过程中使用名称求和和分组?

转载 作者:行者123 更新时间:2023-12-02 03:57:26 25 4
gpt4 key购买 nike

我想对金钱列求和,但我想要状态中的组名和代码。

这是存储过程代码[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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com