现在有一条SQL统计语句是这样的select 收费类型, count(id) 人数, sum(学费) 应收学费, sum(学费实收金额) 实收学费, sum(优惠金额) 优惠金额, sum(体检费实收金额) 体检费, 补费金额=(
select sum(补费金额) from 补费表 where 补费类型='类型一' and  登记编号 in (select 登记编号 from 登记单表 where 已交费=1 and 报名时间 < '2010-11-1')
)
from 登记单表 a
where 已交费=1 and 报名时间 < '2010-11-1' group by 收费类型即 “select sum(补费金额) from 补费表 where 补费类型='类型一' and  登记编号 in” 后面的select语句的条件 和后面的select条件是一样的,能不能把这个SQL简化下?

解决方案 »

  1.   

    select 收费类型, count(id) 人数, sum(学费) 应收学费, sum(学费实收金额) 实收学费, sum(优惠金额) 优惠金额,
     sum(体检费实收金额) 体检费, 补费金额=(
    select sum(补费金额) from 补费表 where 补费类型='类型一' and  登记编号=a.登记编号
    )
    from 登记单表 a
    where 已交费=1 and 报名时间 < '2010-11-1' group by 收费类型
      

  2.   


    select 收费类型, count(id) 人数, sum(学费) 应收学费, sum(学费实收金额) 实收学费, sum(优惠金额) 优惠金额,
     sum(体检费实收金额) 体检费, 补费金额from 登记单表 a 
    left join
    (select 登记编号,sum(补费金额) 补费金额 from 补费表 where 补费类型='类型一' group by 登记编号 ) b on a.登记编号=b.登记编号
    where a.已交费=1 and a.报名时间 < '2010-11-1' group by 收费类型,补费金额
      

  3.   

    谢谢楼上两位,但是都不行,都报错1楼的报:
    服务器: 消息 8120,级别 16,状态 1,行 14
    列 'a.登记编号' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。2楼的报:
    服务器: 消息 170,级别 15,状态 1,行 15
    第 15 行: 'a' 附近有语法错误。
    服务器: 消息 170,级别 15,状态 1,行 17
    第 17 行: 'b' 附近有语法错误。
      

  4.   


    select a.收费类型, count(a.id) 人数, sum(a.学费) 应收学费, sum(a.学费实收金额) 实收学费, sum(a.优惠金额) 优惠金额, 
    sum(a.体检费实收金额) 体检费, 补费金额=sum(b.补费金额)
    from 登记单表 a, 补费表 b
    where a.已交费=1 and a.报名时间 < '2010-11-1' and b.补费类型='类型一' and a.登记编号 = b.登记编号
    group by a.收费类型