3

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

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

Result:

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

but I want:

money | code | name | statusmoney
250.00 | 00001 | fin | 1
100.00 | 00002 | welson | 1
250.00 | 00002 | welson | 2
4

4 回答 4

1

您需要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

请参阅带有聚合器“SUM()”的 GROUP BY 子句的示例

于 2012-08-16T04:32:58.713 回答
0
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
于 2012-08-16T04:29:07.973 回答
0

检查这个

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按 um.FName、um.pId 分组

于 2012-08-16T04:34:54.150 回答
0

怎么样

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

总和(事务处理 SQL)

分组依据 (Transact-SQL)

于 2012-08-16T04:27:12.837 回答