SQL语句、子查询计数?

发布于 2024-11-29 12:22:44 字数 628 浏览 0 评论 0原文

我有以下 SQL 表

Department

|name|employees|

Employee

|name|gender|type|dead |
|John|male  |good|yes  |
|Mary|female|bad |no   |
|Joe |male  |ugly|maybe|

我想编写一个返回

| type | n of employees | n of male employees | n of departments |

I've got

SELECT e.type, count(e), count(d) 
FROM Department d 
JOIN d.employees e
WHERE e.dead = maybe 
GROUP BY e.type

That 的语句,当然,缺少“n ofmaleEmployees”。我被困在这里,因为我不确定在哪里指定附加子句 e.gender =male。

我忘了提:HQL 或标准会很好。

I've got the following SQL tables

Department

|name|employees|

Employee

|name|gender|type|dead |
|John|male  |good|yes  |
|Mary|female|bad |no   |
|Joe |male  |ugly|maybe|

I would like to write a statement that returns

| type | n of employees | n of male employees | n of departments |

I've got

SELECT e.type, count(e), count(d) 
FROM Department d 
JOIN d.employees e
WHERE e.dead = maybe 
GROUP BY e.type

That's missing the 'n of male employees', of course. I'm stuck here, since I'm not sure, where to specify the additional clause e.gender = male.

I forgot to mention: HQL or criteria would be nice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

隔岸观火 2024-12-06 12:22:44

假设您的原始查询和架构是正确的:

SELECT 
   e.type, 
   count(e), 
   count(d), 
   count (case when gender = 'male' then 1 else NULL end) AS NumberOfMaleEmployees
from Department d 
JOIN d.employees e
WHERE e.dead = 'maybe' 
GROUP BY e.type

Assuming your original query and schema is correct:

SELECT 
   e.type, 
   count(e), 
   count(d), 
   count (case when gender = 'male' then 1 else NULL end) AS NumberOfMaleEmployees
from Department d 
JOIN d.employees e
WHERE e.dead = 'maybe' 
GROUP BY e.type
七七 2024-12-06 12:22:44

仅供参考:

SELECT 
   e.type, 
   count(e), 
   count(d), 
   sum(case when gender = 'male' then 1 else 0 end)
from Department d 
JOIN d.employees e
WHERE e.dead = 'maybe' 
GROUP BY e.type

适用于 HQL。谢谢大家!

Just for reference:

SELECT 
   e.type, 
   count(e), 
   count(d), 
   sum(case when gender = 'male' then 1 else 0 end)
from Department d 
JOIN d.employees e
WHERE e.dead = 'maybe' 
GROUP BY e.type

works in HQL. Thanks everyone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文