create table ta(A_Code int,A_Name varchar(20),A_Amount1 int,A_Amount2 int,A_Amount3 int)
insert into ta select 1001,'张三',1000,2000,3000 
insert into ta select 2001,'李四',4000,5000,6000create table tb(A_Code int,B_Code int,B_Amount int)
insert into tb select 1001,10011,1000 
insert into tb select 1001,10012,2000 
insert into tb select 2001,20011,2000 
insert into tb select 2001,20012,3000 
insert into tb select 2001,20013,5000select a.A_Code,a.A_Name,b.C_TotalNum,b.B_Amount,cast(b.B_Amount*100.0/c.num as decimal(10,2)) C_Per
from ta a,(select A_Code,sum(B_Amount) B_Amount,count(*) C_TotalNum from tb group by A_Code) b
      ,(select A_Code,sum(num) num
        from(select A_Code,A_Amount1 as num from ta
           union all select A_Code,A_Amount2 from ta
           union all select A_Code,A_Amount3 from ta) d
         group by A_Code) c
where a.A_Code=b.A_Code and a.A_Code=c.A_Codedrop table ta,tb/*
A_Code      A_Name               C_TotalNum  B_Amount    C_Per        
----------- -------------------- ----------- ----------- ------------ 
1001        张三                   2           3000        50.00
2001        李四                   3           10000       66.67(所影响的行数为 2 行)
*/

解决方案 »

  1.   

    create table A(A_Code int, A_Name varchar(16), A_Amount1 int, A_Amount2 int, A_Amount3 int) 
    insert A select 1001, '张三',1000,2000,3000 
    union all select 2001,'李四',4000,5000,6000 go
    create table B(A_Code int, B_Code int, B_Amount int) 
    insert B select 1001,10011,1000 
    union all select 1001,10012,2000 
    union all select 2001,20011,2000 
    union all select 2001,20012,3000 
    union all select 2001,20013,5000 go
    select A.A_Code, A_Name
    ,C_TatolNum=Num
    ,C_Amount=Amount
    ,C_Per=rtrim(cast(Amount*100.0/(A_Amount1+A_Amount2+A_Amount3) as decimal(5,2)))+'%'
    from A join 
    (
    select A_Code, Num=sum(case when B_Amount>0 then 1 else 0 end), Amount=sum(B_Amount) from B group by A_Code
    ) B on A.A_Code=B.A_Code/*
    A_Code      A_Name           C_TatolNum  C_Amount    C_Per
    ----------- ---------------- ----------- ----------- ------------------------------------------
    1001        张三               2           3000        50.00%
    2001        李四               3           10000       66.67%(2 row(s) affected)
    */drop table A,B
      

  2.   

    create table ta(A_Code int,A_Name varchar(20),A_Amount1 int,A_Amount2 int,A_Amount3 int)
    insert into ta select 1001,'张三',1000,2000,3000 
    insert into ta select 2001,'李四',4000,5000,6000create table tb(A_Code int,B_Code int,B_Amount int)
    insert into tb select 1001,10011,1000 
    insert into tb select 1001,10012,2000 
    insert into tb select 2001,20011,2000 
    insert into tb select 2001,20012,3000 
    insert into tb select 2001,20013,5000select a.A_Code,a.A_Name,b.C_TotalNum,b.B_Amount,
                cast(cast(b.B_Amount*100.0/c.num as decimal(10,2)) as varchar)+'%' C_Per
    from ta a,(select A_Code,sum(B_Amount) B_Amount,count(*) C_TotalNum from tb group by A_Code) b
          ,(select A_Code,sum(num) num
            from(select A_Code,A_Amount1 as num from ta
               union all select A_Code,A_Amount2 from ta
               union all select A_Code,A_Amount3 from ta) d
             group by A_Code) c
    where a.A_Code=b.A_Code and a.A_Code=c.A_Codedrop table ta,tb/*
    A_Code      A_Name               C_TotalNum  B_Amount    C_Per                           
    ----------- -------------------- ----------- ----------- ------------------------------- 
    1001        张三                   2           3000        50.00%
    2001        李四                   3           10000       66.67%(所影响的行数为 2 行)
    */
      

  3.   

    其中表C的数据来源: 
    C_TotalNum=Select   count(*)   From   B   Where   B_Amount> 0 -----------
    小梁,你好像没用到这个条件,,,
      

  4.   

    是哦..
    那case判断一下就好了...学习一下.
      

  5.   

    强,谢谢两位!SQL使用得很精通哦.羡慕,请问哪里有SQL学习的好书啊?请推荐两本,谢谢!
    结贴.
      

  6.   

    rtrim(cast(Amount*100.0/(A_Amount1+A_Amount2+A_Amount3) as decimal(5,2)))+'%'忘了可以这样子的..害我绕了个大圈..看来得好好学习.
      

  7.   

    呵呵.因为我觉得给50分给dobear_0922不够,因为他写的确实很精简明了,所以就按七三分了.呵呵...不好意思,LiangCK,我再补分给你好吗?
      

  8.   

    我想把这个查询语句做成视图来调用:
    select A.A_Code, A_Name
        ,C_TatolNum=Num
        ,C_Amount=Amount
        ,C_Per=rtrim(cast(Amount*100.0/(A_Amount1+A_Amount2+A_Amount3) as decimal(5,2)))+'%'
    from A join 
    (
        select A_Code, Num=sum(case when B_Amount>0 then 1 else 0 end), Amount=sum(B_Amount) from B group by A_Code
    ) B on A.A_Code=B.A_Code提示错误:查询设计器不支持 CASE SQL 构造。
    怎么办呢?
      

  9.   

    我用do熊的数据create table A(A_Code int, A_Name varchar(16), A_Amount1 int, A_Amount2 int, A_Amount3 int) 
    insert A select 1001, '张三',1000,2000,3000 
    union all select 2001,'李四',4000,5000,6000 go
    create table B(A_Code int, B_Code int, B_Amount int) 
    insert B select 1001,10011,1000 
    union all select 1001,10012,2000 
    union all select 2001,20011,2000 
    union all select 2001,20012,3000 
    union all select 2001,20013,5000 
    gocreate view v_test
    as 
    select A.A_Code, A_Name
        ,C_TatolNum=Num
        ,C_Amount=Amount
        ,C_Per=rtrim(cast(Amount*100.0/(A_Amount1+A_Amount2+A_Amount3) as decimal(5,2)))+'%'
    from A join 
    (
        select A_Code, Num=sum(case when B_Amount>0 then 1 else 0 end), Amount=sum(B_Amount) from B group by A_Code
    ) B on A.A_Code=B.A_Codego