select 业务员, (select sum(a) from tb) as '本日总客户', sum(a) as '本日客户', sum(a)/(select sum(a) from tb) as '比率',
       sum(b) as '上日客户', sum(a-b) as '增长数', sum(a - b)/sum(b) as '增长率',
         
from (select 日期,     业务员,   客户名称,    客户编码,   1 as 'a',  0 as 'b'  from tb where 日期 = '20030402' 
      union all
      select 日期,     业务员,   客户名称,    客户编码,   0 as 'a',  1 as 'b'  from tb where 日期 = '20030401' 
     ) as tb
group by 业务员除零的情况自己用case处理吧!

解决方案 »

  1.   

    --查询
    select 业务员=isnull(a.业务员,b.业务员)
    ,本日总客户=isnull(a.本日总客户,0)
    ,本日客户=isnull(a.本日客户,0)
    ,比率=case isnull(a.本日总客户,0) when 0 then '---'
    else cast(cast(isnull(a.本日客户,0)*100.0
    /isnull(a.本日总客户,0) as decimal(10,2)) as varchar)+'%' end
    ,上日客户=isnull(b.上日客户,0)
    ,增长数=isnull(a.本日客户,0)-isnull(b.上日客户,0)
    ,增长率=case isnull(b.上日客户,0) when 0 then '---'
    else cast(cast((isnull(a.本日客户,0)-isnull(b.上日客户,0))*100.0
    /isnull(b.上日客户,0) as decimal(10,2)) as varchar)+'%' end
    from(
    select 业务员,本日客户=count(*),本日总客户=(
    select count(*) from MyTable 
    where 业务员=a.业务员 and 日期<=max(a.日期))
    from MyTable a
    where 日期='20030402'
    group by 业务员
    )a full join(
    select 业务员,上日客户=count(*)
    from MyTable a
    where 日期='20030401'
    group by 业务员
    )b on a.业务员=b.业务员
      

  2.   

    --测试--测试数据
    create table MyTable(日期 char(8),业务员 varchar(10),客户名称 varchar(10),客户编码 varchar(10))
    insert MyTable select '20030401','李一','客户1','A0001'
    union  all     select '20030401','李一','客户1','A0001'
    union  all     select '20030401','张三','客户2','A0002'
    union  all     select '20030401','张三','客户4','A0004'union  all     select '20030402','张三','客户1','A0001'
    union  all     select '20030402','李一','客户1','A0001'
    union  all     select '20030402','李一','客户2','A0002'
    union  all     select '20030402','李一','客户2','A0002'
    union  all     select '20030402','王五','客户3','A0003'
    go--查询
    select 业务员=isnull(a.业务员,b.业务员)
    ,本日总客户=isnull(a.本日总客户,0)
    ,本日客户=isnull(a.本日客户,0)
    ,比率=case isnull(a.本日总客户,0) when 0 then '---'
    else cast(cast(isnull(a.本日客户,0)*100.0
    /isnull(a.本日总客户,0) as decimal(10,2)) as varchar)+'%' end
    ,上日客户=isnull(b.上日客户,0)
    ,增长数=isnull(a.本日客户,0)-isnull(b.上日客户,0)
    ,增长率=case isnull(b.上日客户,0) when 0 then '---'
    else cast(cast((isnull(a.本日客户,0)-isnull(b.上日客户,0))*100.0
    /isnull(b.上日客户,0) as decimal(10,2)) as varchar)+'%' end
    from(
    select 业务员,本日客户=count(*),本日总客户=(
    select count(*) from MyTable 
    where 业务员=a.业务员 and 日期<=max(a.日期))
    from MyTable a
    where 日期='20030402'
    --如果本日是通过系统时间获得,则改条件为:
    --where 日期=convert(char(10),getdate(),112)
    group by 业务员
    )a full join(
    select 业务员,上日客户=count(*)
    from MyTable a
    where 日期='20030401'
    --如果本日是通过系统时间获得,则改条件为:
    --where 日期=convert(char(10),getdate()-1,112)
    group by 业务员
    )b on a.业务员=b.业务员
    go--删除测试
    drop table MyTable/*--测试结果业务员   本日总客户  本日客户  比率      上日客户   增长数  增长率  
    ------- ----------- -------- --------- --------- ------- ----------
    李一    5           3        60.00%    2         1        50.00%
    王五    1           1        100.00%   0         1        ---
    张三    3           1        33.33%    2          -1      -50.00%(所影响的行数为 3 行)--*/
      

  3.   

    zjcxc(邹建)一出马,保证解决
    哈哈
      

  4.   

    请试一下: Select 业务员=isnull(a.业务员,b.业务员)
    ,本日总客户=isnull(a.本日总客户,0)
    ,本日客户=isnull(a.本日客户,0)
    ,比率=case isnull(a.本日总客户,0) when 0 then '---'
    else cast(cast(isnull(a.本日客户,0)*100.0
    /isnull(a.本日总客户,0) as decimal(10,2)) as varchar)+'%' end
    ,上日客户=isnull(b.上日客户,0)
    ,增长数=isnull(a.本日客户,0)-isnull(b.上日客户,0)
    ,增长率=case isnull(b.上日客户,0) when 0 then '---'
    else cast(cast((isnull(a.本日客户,0)-isnull(b.上日客户,0))*100.0
    /isnull(b.上日客户,0) as decimal(10,2)) as varchar)+'%' endfrom(Select 业务员,本日客户=(Select Count(distinct 客户编码) 
         from MyTable Where 业务员=a.业务员),本日总客户=
        (Select Count(distinct 客户编码) 
            from MyTable Where 日期=a.日期)
    from MyTable a
    where 日期='20030402'
    group by 业务员,日期
    )a full join(
    select 业务员,上日客户=count(*)
    from MyTable a
    where 日期='20030401'
    group by 业务员
    )b on a.业务员=b.业务员
      

  5.   

    但也不行!我就可以 @_@******************************
    select 业务员=isnull(a.业务员,b.业务员)
    ,本日总客户=isnull(a.本日总客户,0)
    ,本日客户=isnull(a.本日客户,0)
    ,比率=case isnull(a.本日总客户,0) when 0 then '---'
    else cast(cast(isnull(a.本日客户,0)*100.0
    /isnull(a.本日总客户,0) as decimal(10,2)) as varchar)+'%' end
    ,上日客户=isnull(b.上日客户,0)
    ,增长数=isnull(a.本日客户,0)-isnull(b.上日客户,0)
    ,增长率=case isnull(b.上日客户,0) when 0 then '---'
    else cast(cast((isnull(a.本日客户,0)-isnull(b.上日客户,0))*100.0
    /isnull(b.上日客户,0) as decimal(10,2)) as varchar)+'%' end
    from(
    select 业务员,本日客户=count(distinct 客户编码),本日总客户=(
    select count(distinct 客户编码) from MyTable 
    where  日期=max(a.日期))
    from MyTable a
    where 日期='20030402'
    --如果本日是通过系统时间获得,则改条件为:
    --where 日期=convert(char(10),getdate(),112)
    group by 业务员
    )a full join(
    select 业务员,上日客户=count(distinct 客户编码)
    from MyTable a
    where 日期='20030401'
    --如果本日是通过系统时间获得,则改条件为:
    --where 日期=convert(char(10),getdate()-1,112)
    group by 业务员
    )b on a.业务员=b.业务员
    go