0

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

代码

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

谢谢你的时间。:)

4

2 回答 2

2

您只需要从查询中删除dateand :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
于 2013-02-28T03:46:28.487 回答
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

于 2013-02-28T03:46:39.890 回答