0

我将 JPA2 用于 dao 层。

假设我有一个如下表(只是一个示例):

emp_name     week     approver_name  status   hours
emp1       2010-01-02    app1          a        2
emp1       2010-01-02    app1          a        2
emp1       2010-01-02    app2          b        3
emp1       2010-01-09    app1          b        2
emp1       2010-01-09    app2          a        7
emp2       2010-01-02    app1          b        5
emp2       2010-01-02    app2          a        9
emp2       2010-01-02    app1          a        3
emp2       2010-01-09    app2          a        4
emp2       2010-01-09    app2          b        7
emp2       2010-01-09    app1          a        3
emp2       2010-01-09    app1          a        2

根据给定的表格,(实际上比这更复杂),我怎样才能得到这样的结果

emp_name     week     approver_name    status  hours (add hours together)
emp1      2010-01-02      app1          a        4
emp1      2010-01-02      app2          b        3
emp1      2010-01-09      app1          b        2
emp1      2010-01-09      app2          a        7
emp2      2010-01-02      app1          b        5
emp2      2010-01-02      app1          a        3
emp2      2010-01-02      app2          a        9
emp2      2010-01-09      app1          a        5
emp2      2010-01-09      app2          a        4
emp2      2010-01-09      app2          b        7

注意:状态也必须区分。数据库中也不存在小时列,该列是 start_time 和 end_time

所以任何人都可以对此有一些好主意吗?我可以按任何列分组,因为它会混合结果。

感谢那。

4

1 回答 1

0

试试这个:

SELECT emp_name, week, approver_name, status, SUM(hours)
FROM MyTable
GROUP BY emp_name, week, approver_name, status
ORDER BY emp_name, week, approver_name, status
于 2012-04-05T02:19:32.730 回答