如何引用 1:N 关系链中的多个列
我仍然处于 grails 的学习曲线中,并且遇到了以下问题:
class Departement {
String name // "hr", "it"...
static hasMany = [bureaus:Office]
}
class Office {
String bureaunumber // 102, 104, 106...
static hasMany = [ppl:Personnel]
}
class Personnel {
String name // "Smith", "Walker"...
String title // "project manager", "ceo", "financial manager"...
Integer salary
}
目标是找到特定部门中所有经理的工资总和。结果必须提供工资总额、经理总数和部门名称,如下所示:
|dept |total manager|wages |
|--------|-------------|--------|
| hr | 4 | 340000 |
| it | 7 | 610400 |
| ... | ... | ... |
| all | 11 | 950400 |
等。
我想使用 hibernate criteria api 创建一个结果集,但我坚持使用将提供的 SQL 查询从上面的结果。目前,它给了我适当的薪水:
select sum(salary) wages
from personnel
where title LIKE '%manager%'
and office_id in
(
select office.id
from office
where office.dept_id in
(
select dept.id
from dept
where name = 'hr'
)
)
嗯 - 不幸的是到目前为止就这些了。有谁知道如何计算每个部门的经理总数以及如何将部门名称添加到结果集中?
任何意见将不胜感激!
=========================
大家好, 感谢您花时间回答我的问题!
我对 Ben 建议的解决方案中的 count(managers) 部分感到有点困惑,我在人事领域类中没有经理字段......我是否错过了那里的东西?
尝试使用 count(*) 和 count(personnel.name) ,但它给了我所有员工每个部门的工资总额,而且,它还累加了人员总数 - 而不仅仅是那些“经理” " title :-/
所以运行查询后的结果如下所示:
|dept |total manager|wages |
|--------|-------------|---------|
| hr | 139 | 3988800 |
| it | 139 | 3988800 |
| ... | 139 | 3988800 |
| all | 139 | 3988800 |
======== 更新 2: sql server 2005+ 到 oracle 的语法 ===========
SELECT
CASE GROUPING(d.name)
WHEN 1 THEN 'All:'
ELSE d.name
END dept,
COUNT(*) total_manager,
SUM(p.salary) wages
FROM departement d
INNER JOIN office o ON d.id = o.dept_id
INNER JOIN personnel p ON o.id = p.office_id
WHERE p.name LIKE '%manager%'
GROUP BY ROLLUP(d.name)
I'm still in my learning curve with grails and I've bumped into the following problem:
class Departement {
String name // "hr", "it"...
static hasMany = [bureaus:Office]
}
class Office {
String bureaunumber // 102, 104, 106...
static hasMany = [ppl:Personnel]
}
class Personnel {
String name // "Smith", "Walker"...
String title // "project manager", "ceo", "financial manager"...
Integer salary
}
The goal is to find sum of salary for all managers in a specific departement. The result must deliver the total of salary, the total number of managers and the name of department something like this:
|dept |total manager|wages |
|--------|-------------|--------|
| hr | 4 | 340000 |
| it | 7 | 610400 |
| ... | ... | ... |
| all | 11 | 950400 |
etc.
I'd like to create a resultset with the hibernate criteria api but I'm stuck with the SQL query that would deliver the results from above. Currently, it gives me the proper sum of salary:
select sum(salary) wages
from personnel
where title LIKE '%manager%'
and office_id in
(
select office.id
from office
where office.dept_id in
(
select dept.id
from dept
where name = 'hr'
)
)
and well - unfortunately that's all sofar. Does anybody have an idea how to count the total number of managers for each department and how to add the department name to the resultset?
Any input would be much appreciated!
=========================
Hi guys,
thank you for taking the time answering my question!
I'm a bit confused by the count(managers) part of the suggested solution of Ben, I have no managers field in the personnel domain class... do I miss something there?
Tried with count(*) and also with count(personnel.name) but it gives me a total of salary in each and every department of all employees, also, it adds up the total count of personnel - and not just those with "manager" title :-/
so the result after running a query looks like this:
|dept |total manager|wages |
|--------|-------------|---------|
| hr | 139 | 3988800 |
| it | 139 | 3988800 |
| ... | 139 | 3988800 |
| all | 139 | 3988800 |
======== update 2: sql server 2005+ syntax to oracle ===========
SELECT
CASE GROUPING(d.name)
WHEN 1 THEN 'All:'
ELSE d.name
END dept,
COUNT(*) total_manager,
SUM(p.salary) wages
FROM departement d
INNER JOIN office o ON d.id = o.dept_id
INNER JOIN personnel p ON o.id = p.office_id
WHERE p.name LIKE '%manager%'
GROUP BY ROLLUP(d.name)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 SQL Server 2005+ 中
In SQL Server 2005+
我认为你很接近。使用 SUM 和 COUNT 时,您有时需要在 SQL 中使用 GROUP BY 子句,所以也许这就是您所缺少的。像这样的东西:
I think you are close. When using SUM and COUNT you sometimes need a GROUP BY clause in your SQL, so maybe that's what you are lacking. Something like: