多个日期时间的总和值如何?

时间:2013-02-28 03:42:48

标签: sql

我想要多个日期时间的和值。

代码

SELECT cs.EmployeeRun, cs.Date, cs.Total, SUM(cs.Total) as Sum
FROM CusSalaryDay cs
WHERE cs.Date Between '2012-12-03' and '2012-12-05'
GROUP BY cs.EmployeeRun, cs.Date, cs.Total
Order By EmployeeRun,Date

输出

EmployeeRun | Date | Total | Sum
2330005 | 2012-12-03 00:00:00.000 | 600 |   600
2330005 | 2012-12-04 00:00:00.000 | 600 |   600
2330005 | 2012-12-05 00:00:00.000 | 0 | 0
3467951 | 2012-12-03 00:00:00.000 | 600 |   600
3467951 | 2012-12-04 00:00:00.000 | 600 |   600
3467951 | 2012-12-05 00:00:00.000 | 480 |   480

但我需要输出

EmployeeRun | Date | Sum
2330005 | 2012-12-03 - 2012-12-05 | 1200
3467951 | 2012-12-03 - 2012-12-05 | 1680

感谢您的时间。 :)

2 个答案:

答案 0 :(得分:2)

您只需要从查询中的date中删除cs.totalgroup by

SELECT cs.EmployeeRun, min(cs.Date) as MinDate, max(cs.Date) as MaxDate,
       SUM(cs.Total) as SumTotal
FROM CusSalaryDay cs
WHERE cs.Date Between '2012-12-03' and '2012-12-05'
GROUP BY cs.EmployeeRun
Order By EmployeeRun

答案 1 :(得分:1)

这取决于您使用的服务器的风格,但在MySQL中您只是:

SELECT
     EmployeeRun,
     Date,
     Total,
     SUM(Total) AS sum
FROM CusSalaryDay
WHERE Date Between
    '2012-12-03'
    AND
    '2012-12-05'
GROUP BY EmployeeRun
Order By
    EmployeeRun,
    Date

格式化,只需将GROUP BY限制为EmployeeRun