sql - 无法使用按功能分组解决问题
问题描述
任务:按字母顺序获取至少担任过 30 个主演角色的演员名单。
我的代码:
select name, count(ord=1)
from casting
join actor on actorid=actor.id
where ord=1 and count(ord=1) and exists ( select 1 from casting
where count(movieid)>=30)
group by actorid,name
order by name
它给了我错误,-按功能分组无效。
解决方案
加入表格,按参与者分组并将条件放在有子句中。
select
a.name,
sum(case c.ord when 1 then 1 else 0 end) starringroles
from actor a inner join casting c
on c.actorid = a.id
group by a.id, a.name
having sum(case c.ord when 1 then 1 else 0 end) >= 30
order by a.name
该表达式sum(case c.ord when 1 then 1 else 0 end)
将计算主演角色的数量(带有ord = 1
)。
推荐阅读
- python - 热修复eventstudy股票价格的错误
- javascript - 如何让浏览器保存未屏蔽的密码输入?
- ssl-certificate - 我可以只为 www.mydomain.com 购买证书,还是我也需要为 mydomain.com 购买证书?
- python - 为什么使用 exec 在类方法中动态创建函数不起作用?
- openssl - RSA SHA512 签名由 Windows CNG(下一代加密)NCryptSignHash 与 openssl RSA_sign 生成的签名不匹配
- json - 颤振:FormatException:解码长流响应时未终止的字符串
- amazon-web-services - 使用批处理作业时,“调用 ListObjectsV2 操作时发生错误 (AccessDenied):访问被拒绝”
- java - 在 Struts 和 Spring 或 Spring 和 Struts 之间解析的错误/成功消息
- ruby-on-rails - 使用 React Router 重定向到设计视图
- python - 在 pandas 中查找本地重复项(彼此跟随)