如何引用 1:N 关系链中的多个列

发布于 2024-11-02 00:15:13 字数 2015 浏览 0 评论 0原文

我仍然处于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洛阳烟雨空心柳 2024-11-09 00:15:13

在 SQL Server 2005+ 中

SELECT
  CASE GROUPING(d.name)
    WHEN 1 THEN 'All:'
    ELSE d.name
  END AS dept,
  COUNT(*) AS total_manager,
  SUM(p.salary) AS wages
FROM dept d
  INNER JOIN office o ON d.id = o.dept_id
  INNER JOIN personnel p ON o.id = p.office_id
GROUP BY d.name WITH ROLLUP

In SQL Server 2005+

SELECT
  CASE GROUPING(d.name)
    WHEN 1 THEN 'All:'
    ELSE d.name
  END AS dept,
  COUNT(*) AS total_manager,
  SUM(p.salary) AS wages
FROM dept d
  INNER JOIN office o ON d.id = o.dept_id
  INNER JOIN personnel p ON o.id = p.office_id
GROUP BY d.name WITH ROLLUP
怀中猫帐中妖 2024-11-09 00:15:13

我认为你很接近。使用 SUM 和 COUNT 时,您有时需要在 SQL 中使用 GROUP BY 子句,所以也许这就是您所缺少的。像这样的东西:

SELECT  dept,
        COUNT(managers),
        sum(salary) wages
FROM personal
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'))
GROUP BY managers

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:

SELECT  dept,
        COUNT(managers),
        sum(salary) wages
FROM personal
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'))
GROUP BY managers
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文