如何在sql中找到行的总和? [英] how to find the sum of rows in sql?
本文介绍了如何在sql中找到行的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到下表
deptId 9 10 11 13
----- - - - -
8 2 2 0 0
10 2 0 0 0
11 4 1 0 0 >
使用如下的sql,
i get the below table
deptId 9 10 11 13
----- -- -- -- --
8 2 2 0 0
10 2 0 0 0
11 4 1 0 0
using the sql as below,
SELECT SempMstrSdeptId as deptId,
[9] ,[10] ,[11] ,[13]
FROM
(SELECT SempMstrSdeptId,SempMstrGrpId FROM( select SempMstrSdeptId,SempMstrSshftId,SempMstrGrpId
from SgEmployeeMaster
where (SempMstrSshftId=15 or SempMstrSshftId=12 or SempMstrSshftId=13 or SempMstrSshftId=14 or SempMstrSshftId=16)
and (SempMstrGrpId=9 or SempMstrGrpId=10 or SempMstrGrpId=11 or SempMstrGrpId=13)
and SempStatus=1 )as b ) AS sourcetable
PIVOT
(
COUNT(SempMstrGrpId) FOR SempMstrGrpId IN ([9],[10],[11],[13])
)AS somename
how to get the sum of rows as below result,
deptId 9 10 11 13 total
----- -- -- -- -- ----
8 2 2 0 0 4
10 2 0 0 0 2
11 4 1 0 0 5
谢谢...
Thanks...
推荐答案
SELECT SempMstrSdeptId as deptId,
[9] as a ,[10] as b ,[11] as c ,[13] as d,(a+b+c+d) as Total
FROM
(SELECT SempMstrSdeptId,SempMstrGrpId FROM( select SempMstrSdeptId,SempMstrSshftId,SempMstrGrpId
from SgEmployeeMaster
where (SempMstrSshftId=15 or SempMstrSshftId=12 or SempMstrSshftId=13 or SempMstrSshftId=14 or SempMstrSshftId=16)
and (SempMstrGrpId=9 or SempMstrGrpId=10 or SempMstrGrpId=11 or SempMstrGrpId=13)
and SempStatus=1 )as b ) AS sourcetable
PIVOT
(
COUNT(SempMstrGrpId) FOR SempMstrGrpId IN ([9],[10],[11],[13])
)AS somename
这篇关于如何在sql中找到行的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文