消息130,级别15,状态 1,第 9 行 不能对包含聚合或子查询的表达式执行聚合函数。

消息130,级别15,状态 1,第 9 行 不能对包含聚合或子查询的表达式执行聚合函数。

问题描述:

select b.projectcd,b.projectnm,h.devicetypenm,sum(j.sums) as todaysums,sum((
select sum(j.sums)

       from  cloud_cm..cm_user a,cloud_ri..bi_project b,cloud_ri..bi_project_dt c,
       cloud_ri..bi_disaster d,cloud_ri..bi_dt_st_device e,cloud_ri..bi_stbprp_b f,
       cloud_ri..bi_device g,cloud_ri..bi_devicetype h,cloud_ri..ri_receivesum j
      where a.projectcd=b.projectcd and b.projectcd=c.projectcd  and c.dtcd=d.dtcd
      and e.dtcd=d.dtcd  and e.stcd=f.stcd  and e.devicecd=g.devicecd
      and h.devicetypecd=g.devicetypecd  and g.devicecd=j.devicecd  and a.usercd=2
       )) as    allsums

       from  cloud_cm..cm_user a,cloud_ri..bi_project b,cloud_ri..bi_project_dt c,
       cloud_ri..bi_disaster d,cloud_ri..bi_dt_st_device e,cloud_ri..bi_stbprp_b f,
       cloud_ri..bi_device g,cloud_ri..bi_devicetype h,cloud_ri..ri_receivesum j
      where a.projectcd=b.projectcd and b.projectcd=c.projectcd  and c.dtcd=d.dtcd
      and e.dtcd=d.dtcd  and e.stcd=f.stcd  and e.devicecd=g.devicecd
      and h.devicetypecd=g.devicetypecd  and g.devicecd=j.devicecd  
       and DATEDIFF(day,j.tm,GETDATE())=20            and a.usercd=2
      group by b.projectcd,b.projectnm,h.devicetypenm

分组的原因导致 计算失败