表:   T1 
列:   A1 ,A2,A3,A4,A5,A6,A7,A8,A9,A10,……,A49,A50  
     
      其中,列 A6 是金额型的,现在要生成  一个分组求和表 T2(对A6列求和),按 T1的某些列(比如 A2,A7,A9)分组下面这个语句,总报错:select *,sum(A6) into T2 group by   A2,A7,A9报错显示,某些列没有包含在聚合函数或 GROUP BY 子句中难道要把所有的列都写入 GROUP BY 子句中 ?

解决方案 »

  1.   

    select A2,A7,A9,sum(A6) into T2 group by A2,A7,A9
      

  2.   

    select A2,A7,A9,sum(A6) into T2 group by A2,A7,A9
      

  3.   

    ct A2,A7,A9,sum(A6) into T2 group by A2,A7,A9
    或其它字段都用max()
      

  4.   

    select A2,A7,A9,sum(A6) into T2 group by A2,A7,A9
      

  5.   

    select * ,
    (select sum(A6) from table1 where A2=a.A2 and A7=a.A7 and A9=a.A9) as [A6Sum]
    from table1 as a加多一欄顯示可以這樣用
      

  6.   

    select A2,A7,A9,sum(A6) A6 into T2 group by A2,A7,A9
      

  7.   


    select A2,A7,A9,sum(A6) as A6
    into T2 group by A2,A7,A9
    不要重复发帖!
      

  8.   

    你还没有明白分组统计的意义.
    注意这句话,以某个列分组统计,比如,以性别分组计数,就是说男的有多少,女的有多少,在查询时,即为 
    select 性别,count(*) from tb group by 性别
    此时如果在前面加上一个姓氏,那叫以什么分组呢,因为姓氏与性别不是一回事.
    进一步,如果要以姓氏和性别分组,那就是王姓男的有多少之类的统计,那就是:
    select 姓氏,性别,count(*) from tb group by 姓氏,性别
    如果在前面出现一个住址,又不对了.
    那如果要还在统计时加上一个住址,比如,上海王姓男之类的统计,那就该
    select 住址,姓氏,性别,count(*) from tb group by 住址,姓氏,性别
    可见,分组统计要知道是以什么分组,统计什么,分组列必须出现在 select 子句和 group by 子句中.
      

  9.   

    select A2,A7,A9,sum(A6) into T2 group by A2,A7,A9
      

  10.   


    select *,sum(A6) over(partition by A2,A7,A9 ) into T2 from T1
    select * from T2
    --group by 不行可以考虑 上面的partition by..因为他不需要把所有列都放到..分组列里