现有表table1,字段分别为:地区  金额   日期         类别
                        aa    1000   2006-07-01   汇款   
                        aa    200    2006-07-01   发货   
                        bb    2000   2006-07-01   退货                  
                        bb    1000   2006-07-01   汇款   
                        aa    1000   2006-07-01   汇款   注:表table1中一共有3种"类别"分别为:汇款,发货,退货现在我要得出某一个月的所有地区所以发生金额的汇总. 报表:
   地区  汇款  发货   退货
   aa    2000  200    0
   bb    1000  0      2000
   总计  3000  200    2000

解决方案 »

  1.   

    --在sqlserver中实现如下:
    Create table #tt(地区 varchar(20),  金额 money, 日期 datetime,类别 varchar(20))
    Insert into #tt
    Select 'aa',    1000   ,'2006-07-01','汇款'
    union all Select 'aa',    200    ,'2006-07-01','发货'
    union all Select 'bb',    2000   ,'2006-07-01','退货'  
    union all Select 'bb',    1000   ,'2006-07-01','汇款'  
    union all Select 'aa',    1000   ,'2006-07-01','汇款'Select 地区=(case when grouping(地区)=1 then '总计' else 地区 end),
           汇款=sum(case when 类别='汇款' then 金额 else 0 end),
           发货=sum(case when 类别='发货' then 金额 else 0 end),
           退货=sum(case when 类别='退货' then 金额 else 0 end)
    from #tt group by 地区 with cubedrop table #tt
    go--结果 
    地区    汇款            发货             退货
    --------------------------
    aa 2000.0000 200.0000 .0000
    bb 1000.0000 .0000         2000.0000
    总计 3000.0000 200.0000 2000.0000
      

  2.   

    借WangZWang(先来) 的数据表一用,
    select 地区,sum(Case when 类别='汇款' then 金额 else 0 end) as 汇款,
           sum(Case when 类别='发货' then 金额 else 0 end) as 发货,
           sum(Case when 类别='退货' then 金额 else 0 end) as 退货
    from #tt
    Group by 地区
    Union all
    Select '合计' as 地区,sum(Case when 类别='汇款' then 金额 else 0 end) as 汇款,
           sum(Case when 类别='发货' then 金额 else 0 end) as 发货,
           sum(Case when 类别='退货' then 金额 else 0 end) as 退货 from  #tt也可以得到你想要的结果。
      

  3.   

    Select 地区=(case when grouping(地区)=1 then '总计' else 地区 end),
           汇款=sum(case when 类别='汇款' then 金额 else 0 end),
           发货=sum(case when 类别='发货' then 金额 else 0 end),
           退货=sum(case when 类别='退货' then 金额 else 0 end)
    from #tt group by 地区 with cube这样运行,老是报"WITH CUBE 附近有语法错误" 正确的写法应该怎样???
      

  4.   

    Select (case when grouping(地区)=1 then '总计' else 地区 end) as 地区,
          sum(case when 类别='汇款' then 金额 else 0 end) AS 汇款,
          sum(case when 类别='发货' then 金额 else 0 end) AS 发货,
          sum(case when 类别='退货' then 金额 else 0 end) AS 退货 
    from #tt group by 地区 with cube这样运行,老是报"WITH CUBE 附近有语法错误" 正确的写法应该怎样???
      

  5.   

    就在SQL SERVER 下面运行的.老报那个错,是什么原因呢?
      

  6.   

    用这个
    select 地区,sum(Case when 类别='汇款' then 金额 else 0 end) as 汇款,
           sum(Case when 类别='发货' then 金额 else 0 end) as 发货,
           sum(Case when 类别='退货' then 金额 else 0 end) as 退货
    from #tt
    Group by 地区
    Union all
    Select '合计' as 地区,sum(Case when 类别='汇款' then 金额 else 0 end) as 汇款,
           sum(Case when 类别='发货' then 金额 else 0 end) as 发货,
           sum(Case when 类别='退货' then 金额 else 0 end) as 退货 from  #tt
      

  7.   

    兼容性级别不知道在那看,我找过都没看到,但是我SQL SERVER已经打了SP3的补丁.
      

  8.   

    --改兼容性级别
    exec sp_dbcmptlevel N'数据库名称', 80
     
    --建议LZ最好打sp4补丁