如何在存储过程中使用名称求和和分组? [英] How Sum and Group By with name in stored procedure?

查看:32
本文介绍了如何在存储过程中使用名称求和和分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对money 列求和,但我想要状态中的组名和代码.

I want to sum the money column, but I want group name and code in status.

这是存储过程代码[Sql Server 2008]:

this is the stored procedure code [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)和分组剩余的字段:

You need to SUM(UM.upmoney) and group remaining field like this:

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

参见带有聚合器'SUM()的GROUP BY子句的示例'

See the example of GROUP BY clause with an aggregator 'SUM()'

这篇关于如何在存储过程中使用名称求和和分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆