select a.a,a.b,c=sum(c.b)
from 表A a,表B b,表C c
where b.id=c.id
and b.a like a.a+'%'
group by a.a,a.b

解决方案 »

  1.   

    --测试--测试数据
    create table 表A(a varchar(10),b varchar(10),c bit)
    insert 表A select '01'    ,'人员支出',0
    union  all select '0101'  ,'工资'    ,0
    union  all select '010101','出差费'  ,1
    union  all select '010102','加班费'  ,1  
    union  all select '0102'  ,'资金'    ,1
    union  all select '02'    ,'集体支出',0
    union  all select '0201'  ,'文具'    ,1   create table 表B(id int,a varchar(10))
    insert 表B select 1,'010101'
    union  all select 2,'010101'
    union  all select 3,'010102'
    union  all select 4,'0102'
    union  all select 5,'0201'create table 表C(ID int,b int)
    insert 表C select 1,20
    union  all select 2,40
    union  all select 3,30
    union  all select 4,50
    union  all select 5,10
    go--查询
    select a.a,a.b,c=sum(c.b)
    from 表A a,表B b,表C c
    where b.id=c.id
    and b.a like a.a+'%'
    group by a.a,a.b
    order by a.a
    go--删除测试
    drop table 表A,表B,表C/*--测试结果a          b          c           
    ---------- ---------- ----------- 
    01         人员支出       140
    0101       工资         90
    010101     出差费        60
    010102     加班费        30
    0102       资金         50
    02         集体支出       10
    0201       文具         10(所影响的行数为 7 行)--*/
      

  2.   

    好像是这样,厉害,最主要是 and b.a like a.a+'%'
    我都写晕了,怎么就没想到,呵呵
    试一下,晚上结贴