现有表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
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
解决方案 »
- 如何远程往数据库里面写入数据
- 使用TWebBrowser时,弹出新页面时如何只使用自己的弹出式窗口而不用IE的?
- 我的安装制作遇到问题了,ADO+SEVER SQL 2000
- 100分:adoconnection连接问题!
- Rave报表汉化的问题?
- 高手请进!
- 怎样以一个控件为模板,动态创建除名称外其它属性都相同的另一个控件??
- 提一个超难问题!我编写了一个修改去掉快捷方式小图标的软件,请问如何不重新启动就刷新桌面的图标呢?(就像超级兔子那样)
- 急,关于有密码的access文件的odbc配置问题????
- 送分:安装delphi5的补丁时的注册码是神马
- Delphi+Access,希望在SQL查询中对数据取整
- 工作五年只有2k,我是不是太可怜了?
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
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也可以得到你想要的结果。
汇款=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 附近有语法错误" 正确的写法应该怎样???
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 附近有语法错误" 正确的写法应该怎样???
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
exec sp_dbcmptlevel N'数据库名称', 80
--建议LZ最好打sp4补丁