ywy表 存放N个业务员的名字
id,nameyj表  存放业务员的业绩(每一条记录表示他做的一个单子)
id,name,price两个表用name关联现在想要返回一个表包含每个业务员的营业额,和每个业务员的做单数量应该如何查询?
也就是最后得到一个这样的表:
name,sumPrice,sumSlipCount

解决方案 »

  1.   

    看不出来你ywy表有什么用,分表为了减少冗余,而你其它表中又不存ID,还是存name.select name,sum(price) sumprice,count(*) sumSlipCount from yj group by name
      

  2.   

    两个表用name关联了。。还要第一个表干什么?
    你的查询结果与第一个表无关啊。。
      

  3.   

    我明白你的意思,我刚刚做过这个,发个给你看看,不知道是不是最好的方法
    select 姓名,sumPrice,sumSlipCount from (select distinct name as 姓名 from ywy) as tb1 left join (select name as n0,sum(price) as sumPrice,count(*) as sumSlipCount  from yj group by n0) as tb2 on name=n0我的更长
    select type_1,t_sl,t_je,t_sl_jsfw,t_je_jsfw,t_sl_jskf,t_je_jskf,t_sl_jszr,t_je_jszr,t_sl_jszx,t_je_jszx
    from (select buy_mfdq0 as type_1,count(*) as t_sl,sum(co_htcjzje) as t_je from tc where 1=1 group by buy_mfdq0) as tj1,
    (select type_2,t_sl_jsfw,t_je_jsfw from (select distinct buy_mfdq0 as type_2 from tc) as tc_2 left join (select buy_mfdq0 as type_2_1,count(*) as t_sl_jsfw,sum(co_htcjzje) as t_je_jsfw from tc where co_htlb0='技术服务' group by buy_mfdq0) as tc_2_1 on type_2=type_2_1) as tj2,
    (select type_3,t_sl_jskf,t_je_jskf from (select distinct buy_mfdq0 as type_3 from tc) as tc_3 left join (select buy_mfdq0 as type_3_1,count(*) as t_sl_jskf,sum(co_htcjzje) as t_je_jskf from tc where co_htlb0='技术开发' group by buy_mfdq0) as tc_3_1 on type_3=type_3_1) as tj3,
    (select type_4,t_sl_jszr,t_je_jszr from (select distinct buy_mfdq0 as type_4 from tc) as tc_4 left join (select buy_mfdq0 as type_4_1,count(*) as t_sl_jszr,sum(co_htcjzje) as t_je_jszr from tc where co_htlb0='技术转让' group by buy_mfdq0) as tc_4_1 on type_4=type_4_1) as tj4,
    (select type_5,t_sl_jszx,t_je_jszx from (select distinct buy_mfdq0 as type_5 from tc) as tc_5 left join (select buy_mfdq0 as type_5_1,count(*) as t_sl_jszx,sum(co_htcjzje) as t_je_jszx from tc where co_htlb0='技术咨询' group by buy_mfdq0) as tc_5_1 on type_5=type_5_1) as tj5
    where type_1=type_2 and type_1=type_3 and type_1=type_4 and type_1=type_5
    group by type_1,t_sl,t_je,t_sl_jsfw,t_je_jsfw,t_sl_jskf,t_je_jskf,t_sl_jszr,t_je_jszr,t_sl_jszx,t_je_jszx
    哈哈~
      

  4.   

    select name,sum(price) as sumprice,count(*) as sumSlipCount from yj group by name
      

  5.   

    你是单心有些ywy中的业务员不在yj表出现?早说清楚。select a.name name,isnull(sumprice,0) sumprice,isnull(sumsplitcount,0) sumsplitcount
    from ywy a
    left join
    (select name,sum(price) sumprice,count(*) sumSlipCount from yj group by name) b
    on a.name=b.name