简体   繁体   English

复杂的 sql 查询与 jpa jpql

[英]complex sql query with jpa jpql

I use JPA2 for the dao layer.我将 JPA2 用于 dao 层。

Suppose i have a table as follows (just a sample):假设我有一个如下表(只是一个示例):

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

according to the give tables, (actually more complex than this), how can i get the result like this根据给出的表格,(实际上比这更复杂),我怎样才能得到这样的结果

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

NB: the status also must be distinguished.注意:状态也必须区分。 also the hours column is not existed in the db, the column is start_time and end_time数据库中也不存在小时列,该列是 start_time 和 end_time

so anyone can have some good idea for that?所以任何人都可以对此有一些好主意吗? I can' group by any column, because it will mix result.我不能按任何列分组,因为它会混合结果。

thanks for that.感谢那。

Try this:试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM