教师姓名  月 捐献金额 
王1        1   234.56
张4        10   23.56
赵5        9  223.56
齐0        8   235.56
王1        10   234.56
张4        10   23.56
赵5        9  223.56
齐0        8   235.56
王1        12   234.56
张4        10   23.56
赵5        9  223.56
齐0        8   235.56
王1        1   234.56
张4        10   23.56
赵5        5  223.56
齐0        8   235.56要求输出月  累计捐献金额 
1      xxxxx
2     xxxxx
3     xxxxx
4    xxxxx
5    xxxxx    
6    xxxxx
7    xxxxx
8    xxxxx
9    xxxxx
10    xxxxx
12    xxxxx
 
只有教师1-12月都有捐献金额的时候 才回被统计进入数据
******而且*****
我想知道教师的详细名单! 因为用一维表很难表诉出来

解决方案 »

  1.   

    select 教师姓名,月,sum(捐献金额) 
    from tablename 
    group by 教师姓名,月
    having sum(月) = 78
      

  2.   

    having sum(月) = 7878是什么意思?
      

  3.   

    select 教师姓名,月,sum(捐献金额) 
    from table
    group by 教师姓名,月
    having sum(月) = 78
      

  4.   


    select 月,捐献金额 
    from tttt
    where 教师姓名 in(
    select 教师姓名
    from tttt 
    group by 教师姓名
    having sum(月) = 78)
    order by 月
      

  5.   


    create table tttt(教师姓名  varchar(10),     月 int , 捐献金额 numeric(9,2))
    insert tttt
    select '王1',  1 ,  234.56 union all
    select '王1',  2 ,  234.56 union all
    select '王1',  3 ,  234.56 union all
    select '王1',  4 ,  234.56 union all
    select '王1',  5 ,  234.56 union all
    select '王1',  6 ,  234.56 union all
    select '王1',  7 ,  234.56 union all
    select '王1',  8 ,  234.56 union all
    select '王1',  9 ,  234.56 union all
    select '王1',  10 ,  234.56 union all
    select '王1',  11 ,  234.56 union all
    select '王1',  12 ,  234.56 union all
    select '张4',  1 ,  234.56 union all
    select '张4',  4 ,  234.56 union all
    select '赵5',  8 ,  234.56 union all
    select '赵5',  9 ,  234.56 union all
    select '赵5',  10 ,  234.56 union all
    select '赵5',  11 ,  234.56 union all
    select '赵5',  12 ,  234.56 union all
    select '齐0',  1 ,  234.56 union all
    select '齐0',  4 ,  234.56 
    select 月,捐献金额 
    from tttt
    where 教师姓名 in(
    select 教师姓名
    from tttt 
    group by 教师姓名
    having sum(月) = 78)
    order by 月
    ----78是从1加道12的和
      

  6.   

    ccwwllccwwrr() ( ) 信誉:100    Blog   加为好友  2007-04-23 11:41:01  得分: 0  
     
     
       having sum(月) = 7878是什么意思?
      
    ------------------------------------------------------------------------------------ 1+2+3..+12=78
      

  7.   

    奇怪的问题
    我使用
    select a.月,sum(a.捐献金额)as 捐献金额 from 
    (
    SELECT 月,捐献金额 ,教师姓名
    FROM table )a
    group by a.月 having sum(a.月)=12
    order by a.月
    检索不出数据
    但我可以肯定 有一个或几个教师 1-12月份都有捐献
    为什么统计不出来?
      

  8.   

    group by a.月 having sum(a.月)=78group by a.月 having count(discnt(a.月))=78都不行
      

  9.   

    借用楼上列子:
    declare @t table(教师姓名  varchar(10),     月 int , 捐献金额 numeric(9,2))
    insert @t
    select '王1',  1 ,  234.56 union all
    select '王1',  2 ,  234.56 union all
    select '王1',  3 ,  234.56 union all
    select '王1',  4 ,  234.56 union all
    select '王1',  5 ,  234.56 union all
    select '王1',  6 ,  234.56 union all
    select '王1',  7 ,  234.56 union all
    select '王1',  8 ,  234.56 union all
    select '王1',  9 ,  234.56 union all
    select '王1',  10 ,  234.56 union all
    select '王1',  11 ,  234.56 union all
    select '王1',  12 ,  234.56 union all
    select '张4',  1 ,  234.56 union all
    select '张4',  4 ,  234.56 union all
    select '赵5',  8 ,  234.56 union all
    select '赵5',  9 ,  234.56 union all
    select '赵5',  10 ,  234.56 union all
    select '赵5',  11 ,  234.56 union all
    select '赵5',  12 ,  234.56 union all
    select '齐0',  1 ,  234.56 union all
    select '齐0',  4 ,  234.56 select 教师姓名,月,
    [捐献金额]=sum(捐献金额)
    from @t t
    where (select count(distinct 月) from @t where 教师姓名=t.教师姓名)=12
    group by 教师姓名,月教师姓名       月           捐献金额
    ---------- ----------- ---------------------------------------
    王1         1           234.56
    王1         2           234.56
    王1         3           234.56
    王1         4           234.56
    王1         5           234.56
    王1         6           234.56
    王1         7           234.56
    王1         8           234.56
    王1         9           234.56
    王1         10          234.56
    王1         11          234.56
    王1         12          234.56(12 行受影响)
      

  10.   

    为什么我
    SELECT 教师姓名,月,
    捐献金额
    FROM t
    and 教师姓名='王1 ' 
    可以清楚显示该教师的12个月值
    但是
    一旦加以限定 
    select a.月,sum(a.捐献金额)as 捐献金额 from 
    (
    SELECT 月,捐献金额 ,教师姓名
    FROM table )a
    group by a.月 having sum(a.月)=78 或--having count(a.月)=12 
    order by a.月
    就检索不出数据
    为什么?????????????/
      

  11.   

    having count(a.月)=12 --加上 distinct去掉重复记录的记录数
      

  12.   

    结果还是一样
    无论 having count(distinct(a.月))=12 
    having sum(a.月)=78 
    只要加上此限定
    就检索不出数据
      

  13.   


    你的写法就有问题select a.月,sum(a.捐献金额)as 捐献金额 from 
    (SELECT 月,捐献金额 ,教师姓名 FROM table )a   ---这个地方为什么用个查询啊!
    group by a.月                                 ---这个地方怎么能用“月”来分组呢?
    having sum(a.月)=78 或--having count(a.月)=12 
    order by a.月-----你这样写看看
    select 月,捐献金额=sum(捐献金额)  from [table]
    group by 教师姓名                      
    having sum(月)=78 
    order by 月
      

  14.   


    select 教师姓名,sum(a.捐献金额)as 捐献金额 from 
    (
    SELECT 月,捐献金额 ,教师姓名
    FROM tttt )a
    group by 教师姓名
     having sum(distinct a.月)=78 --或--having count(a.月)=12 
    order by 教师姓名
      

  15.   

    select a.月, sum(a.捐献金额)as 捐献金额
     from 
    (
    SELECT 月,捐献金额
    FROM ttt  where  dyear='2007' 
    )a
    group by a.月 having count(distinct(a.月))=12依旧不行
    太奇怪了
      

  16.   

    create table tttt(教师姓名  varchar(10),     月 int , 捐献金额 numeric(9,2))
    insert tttt
    select '王1',  1 ,  234.56 union all
    select '王1',  2 ,  234.56 union all
    select '王1',  3 ,  234.56 union all
    select '王1',  4 ,  234.56 union all
    select '王1',  5 ,  234.56 union all
    select '王1',  6 ,  234.56 union all
    select '王1',  7 ,  234.56 union all
    select '王1',  8 ,  234.56 union all
    select '王1',  9 ,  234.56 union all
    select '王1',  10 ,  234.56 union all
    select '王1',  11 ,  234.56 union all
    select '王1',  12 ,  234.56 union all
    select '张4',  1 ,  234.56 union all
    select '张4',  4 ,  234.56 union all
    select '赵5',  8 ,  234.56 union all
    select '赵5',  9 ,  234.56 union all
    select '赵5',  10 ,  234.56 union all
    select '赵5',  11 ,  234.56 union all
    select '赵5',  12 ,  234.56 union all
    select '齐0',  1 ,  234.56 union all
    select '齐0',  4 ,  234.56 
    select 月,捐献金额=sum(捐献金额) from tttt where 教师姓名 in
    (select 教师姓名 from tttt group by 教师姓名 having sum(月)=78) group by 月
      

  17.   

    用以上方法:
    select 教师姓名,月,
    [捐献金额]=sum(捐献金额)
    from @t t
    where (select count(distinct 月) from @t where 教师姓名=t.教师姓名)=12
    group by 教师姓名,月
      

  18.   

    ccwwllccwwrr() ( ) 信誉:100    Blog   加为好友  2007-04-23 14:41:29  得分: 0  
     
     
       select a.月, sum(a.捐献金额)as 捐献金额
     from 
    (
    SELECT 月,捐献金额
    FROM ttt  where  dyear='2007' 
    )a
    group by a.月 having count(distinct(a.月))=12    ---这个地方有问题依旧不行
    太奇怪了
      
     
    ----------------------有点不懂lz为什么老是用“月”进行分组呢?说过了要按照“教师姓名”分组的
      

  19.   

    lz是要那种效果,是下边这种吗?月  累计捐献金额 捐献人名单
    1      xxxxx     王1
    2     xxxxx      王1
    3     xxxxx      王1
    4    xxxxx       王1 
    5    xxxxx       王1
    6    xxxxx       王1
    7    xxxxx       王1
    8    xxxxx       王1
    9    xxxxx       王1
    10    xxxxx      王1
    12    xxxxx      王1
      

  20.   

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tttt]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[tttt]
    GO
    create table tttt(教师姓名  varchar(10),     月 int , 捐献金额 numeric(9,2))
    insert tttt
    select '王1',  1 ,  234.56 union all
    select '王1',  2 ,  234.56 union all
    select '王1',  3 ,  234.56 union all
    select '王1',  4 ,  234.56 union all
    select '王1',  5 ,  234.56 union all
    select '王1',  6 ,  234.56 union all
    select '王1',  7 ,  234.56 union all
    select '王1',  8 ,  234.56 union all
    select '王1',  9 ,  234.56 union all
    select '王1',  10 ,  234.56 union all
    select '王1',  11 ,  234.56 union all
    select '王1',  12 ,  234.56 union all
    select '张4',  1 ,  234.56 union all
    select '张4',  4 ,  234.56 union all
    select '赵5',  8 ,  234.56 union all
    select '赵5',  9 ,  234.56 union all
    select '赵5',  10 ,  234.56 union all
    select '赵5',  11 ,  234.56 union all
    select '赵5',  12 ,  234.56 union all
    select '齐0',  1 ,  234.56 union all
    select '齐0',  4 ,  234.56 union all
    select '王2',  1 ,  234.56 union all
    select '王2',  2 ,  234.56 union all
    select '王2',  3 ,  234.56 union all
    select '王2',  4 ,  234.56 union all
    select '王2',  5 ,  234.56 union all
    select '王2',  6 ,  234.56 union all
    select '王2',  7 ,  234.56 union all
    select '王2',  8 ,  234.56 union all
    select '王2',  9 ,  234.56 union all
    select '王2',  10 ,  234.56 union all
    select '王2',  11 ,  234.56 union all
    select '王2',  12 ,  21.1 
    declare   @user   nvarchar(1000)  
     
      select   @user=''     select   @user=case when @user='' then 教师姓名 else @user+','+教师姓名 end
      from  (select 教师姓名 from tttt group by 教师姓名 having sum(月)=78) a  
      print   @user  select 月,捐献金额=sum(捐献金额),捐献名单=@user from tttt
    where 教师姓名 in(select 教师姓名 from tttt group by 教师姓名 having sum(月)=78) group by 月