[英]SQL Mathematical operator between queries
I'm wondering what the best method would be of using two separate query results in a mathematical operation.我想知道在数学运算中使用两个单独的查询结果的最佳方法是什么。
select count(*) as french_films
from language l
join film f on l.language_id = f.original_language_id
where l.name = 'French'
group by l.name
select count(*) as non_english_films
from language l
join film f on l.language_id = f.original_language_id
where l.name != 'English'
The code itself produces a result of 12 for the first query and 59 for the second query.代码本身为第一个查询生成12的结果,为第二个查询生成59的结果。
I want to express the 1st number (12) as a percentage of the 2nd (59) which should be around 20.34%.我想将第一个数字(12)表示为第二个(59)的百分比,应该在20.34% 左右。
I've attempted this as 2 separate CTEs however am struggling on what is best to join them on or if that is the best method.我已经尝试将其作为 2 个独立的 CTE,但是我正在努力寻找最好的加入方式,或者这是否是最好的方法。
Thank you in advance for your help.预先感谢您的帮助。
Use conditional aggregation:使用条件聚合:
select sum(case when l.name = 'French' then 1 else 0 end) as french_films,
sum(case when l.name <> 'English' then 1 else 0 end) as non_english_films
from language l join
film f
on l.language_id = f.original_language_id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.