使用UNION的SUM查询

时间:2014-01-16 13:18:46

标签: sql sum union add union-all

+------+-----------+---------------+---------+
|Period|EmOrg      |Total Hours O S|FTE_T    |
+------+-----------+---------------+---------+
|201304|BOT/GBE/T00|1067           |6.35119  |
+------+-----------+---------------+---------+
|201304|RSA/BDV/T00|352            |2.095238 |
+------+-----------+---------------+---------+
|201304|RSA/BDV/T00|374            |1.888888 |
+------+-----------+---------------+---------+
|201304|RSA/BFN/T00|198            |1        |
+------+-----------+---------------+---------+
|201304|RSA/BFN/T00|2581           |15.363095|
+------+-----------+---------------+---------+
|201304|RSA/CPT/T00|783            |3.954545 |
+------+-----------+---------------+---------+
|201304|RSA/CPT/T00|4176           |24.857142|
+------+-----------+---------------+---------+
|201304|RSA/DUR/T00|609            |3.075757 |
+------+-----------+---------------+---------+
|201304|RSA/DUR/T00|3978.5         |23.681547|
+------+-----------+---------------+---------+
|201304|RSA/JNB/T00|1545           |7.80303  |
+------+-----------+---------------+---------+
|201304|RSA/JNB/T00|6948.5         |41.360119|
+------+-----------+---------------+---------+
|201304|RSA/KNY/T00|2832           |16.857142|
+------+-----------+---------------+---------+
|201304|RSA/PLZ/T00|557            |2.813131 |
+------+-----------+---------------+---------+
|201304|RSA/PLZ/T00|3817.5         |22.723214|
+------+-----------+---------------+---------+
|201304|RSA/PTA/T00|1173           |5.924242 |
+------+-----------+---------------+---------+
|201304|RSA/PTA/T00|5813           |34.60119 |
+------+-----------+---------------+---------+
|201304|RSA/PZB/T00|5626.5         |28.416666|
+------+-----------+---------------+---------+
|201304|RSA/PZB/T00|24896.75       |148.19494|
+------+-----------+---------------+---------+
|201304|RSA/SMD/T00|1238           |7.369047 |
+------+-----------+---------------+---------+
SELECT LD.Period, LD.EmOrg,Sum(LD.RegHrs) AS 'Total Hours O S' , Sum(LD.RegHrs)/198 AS 'FTE_T' 
FROM SSI.dbo.LD LD
GROUP BY LD.Period, LD.EmOrg, LD.EmType
HAVING (LD.EmOrg Like '%T00') AND (LD.EmType='S')  
UNION
SELECT LD.Period, LD.EmOrg, Sum(LD.RegHrs) AS 'Total Hours O S' , Sum(LD.RegHrs)/168 AS 'FTE_T'
FROM SSI.dbo.LD LD
WHERE (LD.EmType In ('C','T','U')) AND (LD.EmOrg Like '%T00')
GROUP BY LD.Period, LD.EmOrg

我真的想加上我的ToTal Hours O S& FTE_T在一个月内返回jst一个值(例如)添加201304 RSA / BDV / T00 726和3.984126而不是具有2个单独的值我如何在我的查询中使用Sum方法和UNION时如何处理? 您的帮助将真正得到应用

2 个答案:

答案 0 :(得分:0)

我认为此查询不需要union。您可以使用条件聚合。假设第一个查询中的198是拼写错误:

SELECT LD.Period, LD.EmOrg,
       Sum(case when LD.EmType in (LD.EmType In ('C','T','U', 'S') then LD.RegHrs
                else 0
           end) AS "Total Hours O S" ,
       Sum(case when LD.EmType in (LD.EmType In ('C','T','U', 'S') then LD.RegHrs)/198
                else 0
           end) AS "FTE_T"
FROM SSI.dbo.LD LD
WHERE LD.EmOrg Like '%T00'
GROUP BY LD.Period, LD.EmOrg, LD.EmType;

如果不是:

SELECT LD.Period, LD.EmOrg,
       Sum(case when LD.EmType in (LD.EmType In ('C','T','U', 'S') then LD.RegHrs
                else 0
           end) AS "Total Hours O S" ,
       Sum(case when LD.EmType in (LD.EmType In ('S') then LD.RegHrs)/198
                when LD.EmType in (LD.EmType In ('C','T','U', 'S') then LD.RegHrs)/168
                else 0
           end) AS "FTE_T"
FROM SSI.dbo.LD LD
WHERE LD.EmOrg Like '%T00'
GROUP BY LD.Period, LD.EmOrg, LD.EmType;

我还将列别名的单个quoates更改为双引号。尽管某些数据库为此目的支持单引号,但它很容易出错。单引号只能用于字符串常量。

答案 1 :(得分:0)

Period  EmOrg            GrandTotal     Division
201304  BOT/GBE/T00          1067        6.1
201304  RSA/BDV/T00          726         3.9
201304  RSA/BFN/T00          2779        15.7
201304  RSA/CPT/T00          4959        27.7
201304  RSA/DUR/T00          4587.5      25.7
201304  RSA/JNB/T00          8493.5      47.3
201304  RSA/KNY/T00          2832        16.1
201304  RSA/PLZ/T00          4374.5      24.5
201304  RSA/PTA/T00          6986        39
201304  RSA/PZB/T00          30523.25    169.9
201304  RSA/SMD/T00          1238        7

此代码的工作原理与MAGIC

相同
SELECT 
     Period, 
     EmOrg, 
     ROUND(Sum(Total_H),2) As GrandTotal, 
     ROUND(Sum(FTE_O),1) As Division
FROM(
SELECT 
     LD.Period, 
     LD.EmOrg, 
     Sum(LD.RegHrs) AS 'Total_H', 
     Sum(LD.RegHrs)/176 AS 'FTE_O'
FROM SSI.dbo.LD LD
WHERE (LD.EmType In ('C','T','U')) AND (LD.EmOrg Like '%T00')
GROUP BY LD.Period, LD.EmOrg
UNION
SELECT 
      LD.Period, 
      LD.EmOrg, 
      Sum(LD.RegHrs) AS 'Total_H', 
      Sum(LD.RegHrs)/198 AS 'FTE_O'
FROM SSI.dbo.LD LD
WHERE (LD.EmType='S') AND (LD.EmOrg Like '%T00')
GROUP BY LD.Period, LD.EmOrg)AS counts
GROUP BY Period, EmOrg