每人每个月有好几笔奖金都算在一个字段里,现在要汇总出每人1-12个月每月的奖金总额
应该怎么写呢?
再具体点,有这样一张表
id  date bouns
现在要查询2006年度每人每个月的奖金总额?怎么写?谢了

解决方案 »

  1.   

    select sum(bouns) as bouns from tablename group by id,date
      

  2.   

    select id,month(date) 月份,sum(bouns) 
    from tablename 
    where year(date)='2006' 
    group by id,month(date)
      

  3.   

    SELECT SUM(bouns) FROM TABLE GROUP BY [ID] WHERE month(date)>1 and  month(date)<12
      

  4.   

    create table taba(id int,  date datetime, bouns int)
    insert into taba select 1,'2006-1-1',100
    union all select 1,'2006-1-1',100
    union all select 1,'2006-1-4',100
    union all select 1,'2006-1-1',100
    union all select 1,'2007-12-1',100
    union all select 1,'2007-1-12',100
    union all select 2,'2006-1-1',100
    union all select 1,'2006-1-1',100
    union all select 2,'2006-1-1',100
    union all select 1,'2006-1-1',100
    union all select 2,'2006-1-1',100
    union all select 1,'2006-3-1',100
    union all select 1,'2006-3-11',100
    union all select 3,'2006-1-1',100
    union all select 1,'2006-1-1',100
    union all select 3,'2007-7-1',100
    go
    select id,CONVERT(varchar(6),date,112)as date,sum(bouns)as bouns 
    from taba group by id,CONVERT(varchar(6),date,112)