一个稍复杂些的交叉表查询。

liuchunhuith 2003-05-13 03:17:53
源表:
收费员 交费方式 金额 月份
张三   现金  200  2
李四   协议  100  2
王五   顶帐  300  4
李四   现金  400  1
王五   现金  200  12
张三   现金  100  1
李四   协议  500  3 

输出:某月份汇总(按收费员分组)本例为2月份
收费员 现金 顶帐 协议 本月合计(注:等于"现金"+"顶帐"+"协议") 全年合计
张三  200  0   0   200                 300
李四   0  0   100  100                 1000
王五   0  0   0    0                 500
总计  200  0   100  300                1800 

注:统计"全年合计"时若当前为8月份,则只统计截止到8月份的数据,而9-12月份的数据不统计,若能实现这个功能就更好了,如不能也没关系。
...全文
26 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
happyforevery 2003-05-16
  • 打赏
  • 举报
回复
study, mark
liuyun2003 2003-05-16
  • 打赏
  • 举报
回复
learning
yoki 2003-05-16
  • 打赏
  • 举报
回复
---统计"全年合计"时若当前为8月份,则只统计截止到8月份的数据
这是只要在 "全年合计 from abc "后面加上 where 月份<=8即可
yoki 2003-05-16
  • 打赏
  • 举报
回复
select a.收费员,isnull(c.现金,0)现金,isnull(c.顶帐,0)顶帐,isnull(c.协议,0)协议,isnull(现金+顶帐+协议,0) as 本月合计,b.全年合计
into #temp
from (select distinct 收费员 from abc) a left join
(select 收费员,
sum(case 交费方式 when '现金' then 金额 else 0 end ) +
sum(case 交费方式 when '顶帐' then 金额 else 0 end )+
sum(case 交费方式 when '协议' then 金额 else 0 end ) as 全年合计
from abc
group by 收费员) b on a.收费员=b.收费员
left join
(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 abc
where 月份=2
group by 收费员) c
on b.收费员=c.收费员
select * from #temp
union
select '总计',sum(现金),sum(顶帐),sum(协议),sum(本月合计),sum(全年合计) from #temp
drop table #temp
liuchunhuith 2003-05-14
  • 打赏
  • 举报
回复
谢谢各位能够帮助解答,但希望最好都能事先测试一下。这是创建表的源代码。其实我感觉本问题的难点在于同时计算“全年合计”的统计上。
USE pubs
go
CREATE TABLE abc
( 收费员 varchar(8),
交费方式 varchar(6),
金额 int,
月份 int
)
go
insert into abc values ('张三','现金',200,2)
insert into abc values ('李四','协议',100,2)
insert into abc values ('王五','顶帐',300,4)
insert into abc values ('李四','现金',400,1)
insert into abc values ('王五','现金',200,12)
insert into abc values ('张三','现金',100,1)
insert into abc values ('李四','协议',500,3)
go
caiyunxia 2003-05-13
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql= ''
select @sql=@sql+',isnull(sum(case 交费方式 when '''+交费方式+''' then 金额 else 0 end ),0) as '+交费方式
from (select distinct 交费方式 from 源表 where 月份 = 8 ) as aaa

select @sql='select a.收费员,sum(交费方式) as 本月合计,全年合计 ' +@sql+'
from 源表 a ,(select 收费员,sum(交费方式) as 全年合计 from 源表 group by 收费员) b where a.收费员=b.收费员 and 月份=8 group by a.收费员 '
print @sql

exec(@sql)
caiyunxia 2003-05-13
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql= ''
select @sql=@sql+',isnull(sum(case 交费方式 when '''+交费方式+''' then 金额 else 0 end ),0) as '+交费方式
from (select distinct 交费方式 from 源表 where 月份 = 8 ) as aaa

select @sql='select a.收费员,sum(交费方式) as 本月合计,全年合计,' +@sql+'
from 源表 a ,(select 收费员,sum(交费方式) as 全年合计 from 源表 group by 收费员) b where a.收费员=b.收费员 and 月份=8 group by a.收费员 '
print @sql

exec(@sql)
psxfghost 2003-05-13
  • 打赏
  • 举报
回复
八月分之前的合计:
declare @sql varchar(8000)
set @sql='select 收费员 '
select @sql=@sql+',isnull(sum(case 交费方式 when '''+a.交费方式+''' then 金额 end ),0) as '+a.交费方式
from (select distinct 交费方式 from 源表) as a


select @sql=@sql+' into ##temp from 源表 where 月份<8 group by 收费员 '
print @sql
exec(@sql)

select *,顶帐+现金+协议 as 八月分之前的合计 from ##temp
one_bird 2003-05-13
  • 打赏
  • 举报
回复
学习
psxfghost 2003-05-13
  • 打赏
  • 举报
回复
这是全年的,先试试:
declare @sql varchar(8000)
set @sql='select 收费员 '
select @sql=@sql+',isnull(sum(case 交费方式 when '''+a.交费方式+''' then 金额 end ),0) as '+a.交费方式
from (select distinct 交费方式 from 源表) as a

select @sql=@sql+' from 源表 group by 收费员'
print @sql

exec(@sql)
joygxd 2003-05-13
  • 打赏
  • 举报
回复
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 协议,
现金+顶帐+协议 as 本月合计
from (select distinct 交费方式 from tablea )
where 月份='某月'
group by 收费员
joygxd 2003-05-13
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1775/1775494.xml?temp=.8407404

你去看看
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 协议,
现金+顶帐+协议 as 本月合计
from (select distinct 交费方式 from tablea )
group by 收费员

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧