请问各位大虾如何实现:原数据表:
客户代码,客户名称,业务员代码,业务名称,金额,重要程序
01       a客户     01         a业务员  100   1
02       b客户     01         a业务员  200   1
03       c客户     01         a业务员   10   0
04       d客户     01         a业务员   20   0
05       e客户     02         b业务员   40   1   
06       f客户     02         b业务员   50   1
07       g客户     02         b业务员   20   0
08       h客户     02         b业务员   30   0
其中:1为重要客户,0为非重要客户希望结果如下:客户代码,客户名称,业务员代码,业务员名称,金额
01       a客户     01        a业务员     100
02       b客户     01        a业务员     200
重要客户合计                                300
03       c客户     01        a业务员      10
04       d客户     01        a业务员      20
非重要客户合计                               30
a业务员合计                                 330       
05       e客户     02        b业务员      40
06       f客户     02        b业务员      50
重要客户合计                                 90
07       g客户     02        b业务员      20
08       h客户     02        b业务员      30
非重要客户合计                               50
b业务员合计                                  140
总计                                         470

解决方案 »

  1.   

    晕,自己解了,请问有比我这个更的方法吗?select 客户代码,客户名称,CASE 业务员代码 WHEN '' THEN '' ELSE 业务员代码 END as 业务员代码,业务员名称,金额,重要程度 from 
    (select * from table1 union all
    select 客户代码=case grouping(业务员代码) when 1 then '重要总计' else case grouping(重要程度) when 1 then '重要小计' end end
           ,'',isnull(业务员代码,''),'',sum(金额),1
    from table1 where 重要程度=1 group by 业务员代码,重要程度 with rollup having grouping(业务员代码)=1 or grouping(重要程度)=1
    union all
    select 客户代码=case grouping(业务员代码) when 1 then '非重要总计' else case grouping(重要程度) when 1 then '非重要小计' end end
           ,'',isnull(业务员代码,''),'',sum(金额),0
    from table1 where 重要程度=0 group by 业务员代码,重要程度 with rollup having grouping(业务员代码)=1 or grouping(重要程度)=1
    union all
    select 客户代码=case grouping(业务员代码) when 1 then '业务员总计' else case grouping(业务员名称) when 1 then '业务员小计' end end
           ,'',isnull(业务员代码,''),'',sum(金额),2
    from table1  group by 业务员代码,业务员名称 with rollup having grouping(业务员代码)=1 or grouping(业务员名称)=1
    ) a order by case 业务员代码 when '' then '9999' else 业务员代码 end,重要程度,客户代码
      

  2.   


    if object_id('tab') is not null drop table tab
    create table tab (客户代码 varchar(20),客户名称 varchar(50),
    业务员代码 varchar(20),业务员名称 varchar(50),金额 int,重要程序 int)insert into tab
    select '01','a客户','01','a业务员',100,1 union all
    select '02','b客户','01','a业务员',200,1 union all
    select '03','c客户','01','a业务员',10,0 union all
    select '04','d客户','01','a业务员',20,0 union all
    select '05','e客户','02','b业务员',40,1 union all
    select '06','f客户','02','b业务员',50,1 union all
    select '07','g客户','02','b业务员',20,0 union all
    select '08','h客户','02','b业务员',30,0
    select b.客户代码,isnull(a.客户名称,'') as 客户名称,isnull(a.业务员代码,'') as 业务员代码,
    isnull(a.业务员名称,'') as 业务员名称,b.金额 from 
    (
    select
    case when 客户代码 is null and 业务员代码 is not null and 重要程序 = 1 then '重要客户合计' 
    else case when 客户代码 is null and 业务员代码 is not null and 重要程序 = 0 then '非重要客户合计'
    else case when 客户代码 is null and 业务员代码 is not null and 重要程序 is null then 业务员名称+'合计'
    else case when 客户代码 is null and 业务员代码 is null and 重要程序 is null then '总计'
    else 客户代码
    end end end end as 客户代码,sum(金额) as 金额 from tab
    group by 业务员代码,业务员名称,重要程序,客户代码 with rollup 

    as b left join tab as a on a.客户代码=b.客户代码 where b.客户代码 is not null/*---------------
    03             c客户 01 a业务员 10
    04             d客户 01 a业务员 20
    非重要客户合计 30
    01             a客户 01 a业务员 100
    02             b客户 01 a业务员 200
    重要客户合计         300
    a业务员合计             330
    07             g客户 02 b业务员 20
    08             h客户 02 b业务员 30
    非重要客户合计 50
    05             e客户 02 b业务员 40
    06             f客户 02 b业务员 50
    重要客户合计         90
    b业务员合计             140
    总计                 470
    */
      

  3.   

    因为数据是不确定,希望用标准化的sql语句完成。