select ditinct a.节目名称 as progname ,a.业务代码 as progcode,count(b.id) as incount , 
count(c.id) as outcount,sum(c.费率) price 
from 表1 as a,
表2 as b,
表3 as c加distinct子句试一下。

解决方案 »

  1.   

    使用中间表(可以是临时表)去掉重复纪录 distinct
    再从中间表中统计
      

  2.   

    一定是你的表的业务代码不唯一:select a.节目名称 as progname ,a.业务代码 as progcode,count(b.id) as incount , 
    count(c.id) as outcount,sum(c.费率) price 
    from 表1 as a,
    (select min(id) as id,业务代码 from 表2 group by 业务代码) as b,
    (select min(id) as id,业务代码,sum(费率) as 费率 from 表3  group by 业务代码) as c
    where a.业务代码=b.业务代码 and a.业务代码=c.业务代码
    group by a.节目名称,a.业务代码
      

  3.   

    加了,在2个count()里加了,sum()也加了,还是有问题!
      

  4.   

    不对,price 对了,但是incount,outcount 的条数不对
      

  5.   

    select a.节目名称 as progname ,a.业务代码 as progcode,count(b.id) as incount , 
    count(c.id) as outcount,sum(c.费率) price 
    from (表1 as a inner join 表2 as b on a.业务代码 =b.业务代码) inner join 表3 as c on a.业务代码=c.业务代码
      

  6.   

    newly_ignorant(不学无术)兄:
    怎样改才好!能不能说清楚!
      

  7.   


    liyunsong2000(我选择,我喜欢)兄:
    象你说的,条数对了,费率不对。还是用的乘法,其实只有表三才有费率!
      

  8.   

    select a.节目名称 as progname ,a.业务代码 as progcode,
    (select count(id) from 表2 where a.业务代码=业务代码) as incount , 
    (select count(id) from 表3 where a.业务代码=业务代码) as outcount , 
    (select sum(费率) from 表3 where a.业务代码=业务代码) as price 
    from 表1 as a
    group by a.节目名称,a.业务代码
      

  9.   

    select a.节目名称 as progname ,a.业务代码 as progcode,
    (select count(id) from 表2 where a.业务代码=业务代码) as incount , 
    (select count(id) from 表3 where a.业务代码=业务代码) as outcount , 
    (select sum(费率) from 表3 where a.业务代码=业务代码) as price 
    from 表1 as a
    group by a.节目名称,a.业务代码
      

  10.   

    OpenVMS(半知半解) 兄:
    你的方法还不错,就是把表一所有的业务代码都显示出来了,能不能只显示表二,表三有的代码!
      

  11.   

    select a.节目名称 as progname ,a.业务代码 as progcode,
    (select count(id) from 表2 where a.业务代码=业务代码) as incount , 
    (select count(id) from 表3 where a.业务代码=业务代码) as outcount , 
    (select sum(费率) from 表3 where a.业务代码=业务代码) as price 
    from 表1 as a
    where a.业务代码 in (select 业务代码 from 表2 union select 业务代码 from 表3)
    group by a.节目名称,a.业务代码
      

  12.   

    select a.节目名称 as progname ,a.业务代码 as progcode,
    (select count(id) from 表2 where a.业务代码=业务代码) as incount , 
    (select count(id) from 表3 where a.业务代码=业务代码) as outcount , 
    (select sum(费率) from 表3 where a.业务代码=业务代码) as price 
    from 表1 as a
    where a.业务代码 in (select 业务代码 from 表2 union select 业务代码 from 表3)
    group by a.节目名称,a.业务代码