我试图将总代码和占用代码显示为列和行内的dcode计数,但它给了我一个错误:
不能在用于表达式的表达式中使用聚合或子查询 按GROUP BY子句列表分组。
select count(*) as total
,(select count(dcode) from c122117a_A I where I.dcode = o.dcode and occupation='49' and left(zip,3) between '900' and '934') as 'Doctors-49'
,(select count(dcode) from c122117a_A I where I.dcode = o.dcode and occupation='17' and left(zip,3) between '900' and '934') as 'Health Services-17'
,(select count(dcode) from c122117a_A I where I.dcode = o.dcode and occupation='53' and left(zip,3) between '900' and '934') as '53=Insurance/Underwriters'
,(select count(dcode) from c122117a_A I where I.dcode = o.dcode and occupation='41' and left(zip,3) between '900' and '934') as '41=Occupational Therapy'
,(select count(dcode) from c122117a_A I where I.dcode = o.dcode and occupation='48' and left(zip,3) between '900' and '934') as '48=Nurses'
,(select count(dcode) from c122117a_A I where I.dcode = o.dcode and occupation='43' and left(zip,3) between '900' and '934') as '43=Psychologists'
,(select count(dcode) from c122117a_A I where I.dcode = o.dcode and occupation='21' and left(zip,3) between '900' and '934') as '21=Teacher/Educator'
from c122117a_A o
group by count(*)
order by count(*)
答案 0 :(得分:1)
尝试这样的事情,完成省略的列
select count(o.dcode) as total
,sum(case when occupation='49' and left(zip,3) between '900' and '934' then 1 else 0 end) as 'Doctors-49'
,sum(case when occupation='17' and left(zip,3) between '900' and '934' then 1 else 0 end) as 'Health Services-17'
...
from c122117a_A o
group by o.dcode
答案 1 :(得分:0)
将group by count(*)
替换为group by dcode
。因为您在子查询中引用了o.dcode,所以必须指定哪一个。如果你不按它们分组,所有的dcode都会被放在一起,数据库无法确定使用哪一个(值)。