我编写了一个查询,其中涉及创建单个表上保存的总数字和值的百分比(转储到单行)。每个子查询在 WHERE 参数方面略有不同。例如:
select distinct
count(KS2_PupilMatchingRefAnonymous) AS 'Total Pupils',
(select count(KS2_PupilMatchingRefAnonymous)
from KS2_CandInd_2011
where KS2_MMSCH=1
and KS2_ELIGENGTA=1) AS 'All Eligible Pupils',
(select count(*)
from KS2_CandInd_2011
where KS2_MMSCH=1
and KS2_ELIGENGTA=1
and KS2_ELIGENGTA=1
and FSMeligible=1) AS 'Eligible FSM only',
(select round(
(cast((
select count(*)
from KS2_CandInd_2011
where KS2_MMSCH=1
and KS2_ELIGENGTA=1
and KS2_ELIGENGTA=1
and FSMeligible=1) AS float) /
cast((
select count(KS2_PupilMatchingRefAnonymous)
from KS2_CandInd_2011
where KS2_MMSCH=1
and KS2_ELIGENGTA=1) AS float))*100,2
)) AS '% Eligible FSM'
from KS2_CandInd_2011
where KS2_MMSCH=1
我现在想根据性别使用 COUNT(当然还有 GROUP BY)分解每个子查询。如果将性别的 COUNT/GROUP BY 放在外部查询中,我只会得到“总学生”值的细分/拆分。
如果我尝试在子查询中输入相同的子句,我会被告知
Msg 116, Level 16, State 1, Line 12 当子查询没有用 EXISTS 引入时,只能在选择列表中指定一个表达式。
根据相关子查询,内部查询并没有真正引用外部查询,所以尽管我尽了最大努力,但我不确定如何克服错误。有没有人有什么建议?