【发布时间】:2014-03-20 13:33:24
【问题描述】:
这里重复子查询两次,
(select count(*) from hindi2_MOVIE as yaar where yaar.year = M.year),我不想这样做,有没有办法重用它们,只需运行一次查询?
我需要找到总计数,比如按年份分组的条目的“tot”,然后是其他一些特征的百分比,其中分母是总计数“tot”。在下面的 SQL 查询中,我重复了两次 select 操作,我知道这非常昂贵,是否可以重用相同的操作?
select M.year,
(select count(*)
from hindi2_MOVIE as yaar
where yaar.year = M.year) as tot,
count(M.title)/(select count(*)
from hindi2_MOVIE as yaar
where yaar.year = M.year)*100 as perc
from hindi2_MOVIE AS M
where not exists (select *
from hindi2_M_CAST AS C,hindi2_PERSON AS P
where C.PID=P.PID AND C.MID=M.MID AND P.Gender='M') and
exists(select *
from hindi2_M_CAST AS C,hindi2_PERSON AS P
where C.PID=P.PID AND
C.MID=M.MID and Gender='F')
group by M.year;
【问题讨论】:
-
为什么不能使用
tot作为变量:count(M.title)/tot * 100 as perc -
@SamuelCook 它给了我错误。
Unknown column 'tot' in 'field list'