表1     
id       自动编号
姓名     varchar
项目编号 varchar  
表2
id       自动编号
项目编号 varchar
项目费用 varchar
表3
id       自动编号
项目编号 varchar
其他费用 varchar我想统计每个人的其他费用总和,项目费用总和,其他费用总和
请问应该怎样做?select 姓名,count(项目编 as 项目数量 ,sum(表2.项目费用) as 项目费用总和
    sum(表3.其他费用) as  其他费用总和 from 表1,表2,表3
        where 表1.项目编号=表2.项目编号 and
             表2.项目编号=表3.项目编号
group by 姓名
order by 姓名

解决方案 »

  1.   

    select a.id,姓名,count(b.id) as 项目数量,sum(项目费用) as 项目费用总和,sum(其他费用) as 其他费用总和
    from 表1 a,表2 b,表3 c
    where a.项目编号=b.项目编号 and a.项目编号=c.项目编号
    group by a.id,姓名
      

  2.   

    select 每个人的项目数量=(select count(项目编号) from 表1 where 姓名=a.姓名),    
    项目费用总和=(select sum(bb.项目费用) from 表1 aa inner join 表2 bb where aa.项目编号=bb.项目编号 and aa.姓名=a.姓名),
    其他费用总和=(select sum(cc.其他费用) from 表1 aaa inner join 表3 cc where aaa.项目编号=cc.项目编号 and aaa.姓名=a.姓名),
    from 表1 a
      

  3.   

    --测试环境
    declare @t1 table(id int identity(1,1),姓名 varchar(10),项目编号 varchar(20))
    declare @t2 table(id int identity(1,1),项目编号 varchar(20),项目费用 decimal(10,2))
    declare @t3 table(id int identity(1,1),项目编号 varchar(20),其他费用 decimal(10,2))
    insert into @t1
    select 'mico','11' union all
    select 'mico','12' union all
    select 'ming','11' union all
    select 'jj','12' union all
    select 'jj','13' union all
    select 'mico','13' insert into @t2
    select '11',10 union all 
    select '12',15 union all
    select '13',25.5 insert into @t3
    select '11',8 union all 
    select '12',10 union all
    select '13',11.5 select 姓名,count(t1.项目编号) as '项目数量' ,sum(t2.项目费用) as '项目费用总和',
        sum(t3.其他费用) as '其他费用总和' from @t1 t1,@t2 t2,@t3 t3
            where t1.项目编号=t2.项目编号 and
                 t2.项目编号=t3.项目编号
    group by 姓名
    order by 姓名
    ----------------------------------------
    /*
     姓名       项目数量  项目费用总和  其他费用总和
    jj 2 40.50 21.50
    mico 3 50.50 29.50
    ming 1 10.00 8.00
    */
      

  4.   

    多谢各位大侠!:)
    只是小弟的数据库不是自己建的,所有字段类型均为varchar型,好像不能用sum函数来统计,郁闷呀!
      

  5.   

    select 姓名,count(项目编) as 项目数量 ,sum(表2.项目费用) as 项目费用总和
        sum(表3.其他费用) as  其他费用总和 from 表1,表2,表3
            where 表1.项目编号=表2.项目编号 and
                 表2.项目编号=表3.项目编号
    group by 姓名
    order by 姓名