如何聚合SQL查询的结果?

时间:2013-02-20 23:27:37

标签: sql sum

我目前有以下查询,该查询返回特定员工和月份的时间表条目:

SELECT supervisors.employee, 
       hourscharged.project, 
       hourscharged.chargedescription, 
       hourscharged.account, 
       hourscharged.org, 
       hourscharged.entered_hrs 
FROM   accounting 
       INNER JOIN hourscharged 
               ON hourscharged.period_no_cd = accounting.period_no_cd 
       INNER JOIN supervisors 
               ON hourscharged.employeeid = supervisors.employeenumber 
WHERE  supervisors.employee = 'JOHN' 
       AND accounting.accountingmonth = 'FEB-13' 
GROUP  BY supervisors.employee, 
          hourscharged.project, 
          hourscharged.chargedescription, 
          hourscharged.account, 
          hourscharged.org, 
          hourscharged.entered_hrs 

查询返回7行,其中包含

的以下值
Employee | Project | ChargeDescription | Account | Org | ENTERED_HRS:
JOHN | 15113 | MBT Upgrade | 0510 | 1.1.10.5 | 2.00
JOHN | 15113 | MBT Upgrade | 0510 | 1.1.10.5 | 3.50
JOHN | 15113 | MBT Upgrade | 0510 | 1.1.30.4 | 2.00
JOHN | 15113 | MBT Upgrade | 0510 | 1.1.30.4 | 3.50
JOHN | 34188 | KY13 S & T  | 0320 | 1.1.30.4 | 1.00
JOHN | 34188 | KY13 S & T  | 0320 | 1.1.30.4 | 4.00
JOHN | 34188 | KY13 S & T  | 0320 | 1.1.30.4 | 8.00

这些是时间表条目。我想显示这些行的总计的唯一行,即:

JOHN | 15113 | MBT Upgrade | 0510 | 1.1.10.5 | 5.50
JOHN | 15113 | MBT Upgrade | 0510 | 1.1.30.4 | 5.50
JOHN | 34188 | KY13 S & T  | 0320 | 1.1.30.4 | 13.00

我试过了:

SELECT supervisors.employee, 
       hourscharged.project, 
       hourscharged.chargedescription, 
       hourscharged.account, 
       hourscharged.org, 
       Sum(hourscharged.entered_hrs) <-- * 
FROM   accounting 
       INNER JOIN hourscharged 
               ON hourscharged.period_no_cd = accounting.period_no_cd 
       INNER JOIN supervisors 
               ON hourscharged.employeeid = supervisors.employeenumber 
WHERE  supervisors.employee = 'JOHN' 
       AND accounting.accountingmonth = 'FEB-13' 
GROUP  BY supervisors.employee, 
          hourscharged.project, 
          hourscharged.chargedescription, 
          hourscharged.account, 
          hourscharged.org, 
          hourscharged.entered_hrs 

得到了:

JOHN | 15113 | MBT Upgrade | 0510 | 1.1.10.5 | 38.50
JOHN | 15113 | MBT Upgrade | 0510 | 1.1.30.4 | 38.50
JOHN | 34188 | KY13 S & T  | 0320 | 1.1.30.4 | 91.00 

这很奇怪,因为以下查询(例如)

SELECT chargedescription, 
       Sum(entered_hrs) 
FROM   hourscharged 
WHERE  employeeid = '123456' 
       AND chargedescription LIKE 'KY13 S & T' 
GROUP  BY chargedescription 

给出了正确的总和时间,即:

JOHN | 34188 | KY13 S & T  | 0320 | 1.1.30.4 | 70.00

我可以用SUM做我想做的事吗?如果是这样,请指导我。我已经广泛搜索了聚合SUM函数的应用,但我似乎无法将它应用于我自己的问题。我也觉得奇怪的是,在我的复杂查询中使用SUM而不是ENTERED_HRS给了我完全错误的总计小时数。如果SUM不起作用,会是什么?我无法理解我需要申请的原则。此查询所基于的模式可能并不理想,但它是遗留的,我需要使用它。我也在使用SQL Server 2005.谢谢。

我与INNER JOIN的查询相当于:

SELECT supervisors.employee, 
       hourscharged.project, 
       hourscharged.chargedescription, 
       hourscharged.account, 
       hourscharged.org, 
       hourscharged.entered_hrs 
FROM   supervisors, 
       accounting, 
       hourscharged 
WHERE  supervisors.employee = 'JOHN' 
       AND hourscharged.employeeid = supervisors.employeenumber 
       AND accounting.accountingmonth = 'FEB-13' 
       AND hourscharged.period_no_cd = accounting.period_no_cd 
GROUP  BY supervisors.employee, 
          hourscharged.project, 
          hourscharged.chargedescription, 
          hourscharged.account, 
          hourscharged.org, 
          hourscharged.entered_hrs 

1 个答案:

答案 0 :(得分:0)

sum()函数正常工作。这是您正在选择的其他字段导致问题。或者它可能是您的数据。

在这两行中:

JOHN | 15113 | MBT升级| 0510 | 1.1.10.5 | 38.50

JOHN | 15113 | MBT升级| 0510 | 1.1.30.4 | 38.50

HoursCharged.Org结果有不同的值。你应该看看造成这种情况的原因。