select a.TypeName,sum(b.Amount) as Amount
from 表1 a,表2 b
where b.TypeID like rtrim(a.TypeID)+'%'
group by a.TypeName
union all
select '合计' as TypeName,sum(Amount) as Amount
from 表2

解决方案 »

  1.   

    Select A.Name ,B.Amount from table1 A left join  Table2 B on A.TypeID=b.TypeID
      

  2.   

    select a.TypeName,b.Amount from 表1 a ,(select TypeID,Amount from 表2 union all select left(TypeID,1),sum(Amount) from 表2 group by left(TypeID,1)) b where a.TypeID=b.TypeID
      

  3.   

    select a.TypeName,b.Amount from 表1 a ,(select TypeID,Amount from 表2 union all select left(TypeID,1),sum(Amount) from 表2 group by left(TypeID,1)) b where a.TypeID=b.TypeID union all select '合计',sum(Amount) from 表2
      

  4.   

    假如TypeID最大长度是10位,如1111111111,又该怎么处理呢 
      

  5.   

    你的电视手机等的Amount是从哪里得来的???
      

  6.   

    select a.TypeName, (select sum(b.Amount) from table1 c, 
    table2 b where (a.TypeId = Left(c.TypeID, 1) or
    a.TypeID = c.TypeID) and b.TypeID = c.TypeID) from table1 a
    union all
    select '合计', sum(b.Amount) from table1 a, table2 b
    where a.typeid = b.typeid  
      

  7.   

    to  CrazyFor(蚂蚁) 
    Amount 是已经计算好的字段
      

  8.   

    to  pengdali(大力 V2.0) 
      运行效率不高,45836条纪录2分钟都出不了结果