创建视图的难问题,高手请进

mbzdh 2003-07-16 01:06:07
现有一医疗台帐表:字段有:年份、月份、本月存入、本月报销、上月节余、本月节余、
我想建一视图,内容包括:年份、1月支出、1月节余、2月支出、2月节余、......,
请问各位高手,能实现吗?
谢谢!!!
...全文
28 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-07-16
  • 打赏
  • 举报
回复
create view v1 as
select 年份,
sum(case when 月份 = 1 then 本月报销 else 0 end) 1月支出,
sum(case when 月份 = 1 then 本月节余 else 0 end) 1月节余,
sum(case when 月份 = 2 then 本月报销 else 0 end) 2月支出,
sum(case when 月份 = 2 then 本月节余 else 0 end) 2月节余,
sum(case when 月份 = 3 then 本月报销 else 0 end) 3月支出,
sum(case when 月份 = 3 then 本月节余 else 0 end) 3月节余,
sum(case when 月份 = 4 then 本月报销 else 0 end) 4月支出,
sum(case when 月份 = 4 then 本月节余 else 0 end) 4月节余,
sum(case when 月份 = 5 then 本月报销 else 0 end) 5月支出,
sum(case when 月份 = 5 then 本月节余 else 0 end) 5月节余,
sum(case when 月份 = 6 then 本月报销 else 0 end) 6月支出,
sum(case when 月份 = 6 then 本月节余 else 0 end) 6月节余,
sum(case when 月份 = 7 then 本月报销 else 0 end) 7月支出,
sum(case when 月份 = 7 then 本月节余 else 0 end) 7月节余,
sum(case when 月份 = 8 then 本月报销 else 0 end) 8月支出,
sum(case when 月份 = 8 then 本月节余 else 0 end) 8月节余,
sum(case when 月份 = 9 then 本月报销 else 0 end) 9月支出,
sum(case when 月份 = 9 then 本月节余 else 0 end) 9月节余,
sum(case when 月份 = 10 then 本月报销 else 0 end) 10月支出,
sum(case when 月份 = 10 then 本月节余 else 0 end) 10月节余,
sum(case when 月份 = 11 then 本月报销 else 0 end) 11月支出,
sum(case when 月份 = 11 then 本月节余 else 0 end) 11月节余,
sum(case when 月份 = 12 then 本月报销 else 0 end) 12月支出,
sum(case when 月份 = 12 then 本月节余 else 0 end) 12月节余
from 医疗台帐表
group by 年份
mbzdh 2003-07-16
  • 打赏
  • 举报
回复
楼上的大哥,不对呀,我的视图的一条记录里包括1---12月的支出、节余情况,而表里的一条记录里只有一个月的情况呀,
CrazyFor 2003-07-16
  • 打赏
  • 举报
回复
select
年份
,sum(case when 月份=1 then 本月报销 else 0 end) 1月支出
,sum(case when .....
....
group by 年份 order by 年份
愉快的登山者 2003-07-16
  • 打赏
  • 举报
回复
create view v1 as
select 年份,
sum(case when 月份 = 1 then 本月报销 else 0 end) 1月支出,
sum(case when 月份 = 1 then 本月节余 else 0 end) 1月节余,
sum(case when 月份 = 2 then 本月报销 else 0 end) 2月支出,
sum(case when 月份 = 2 then 本月节余 else 0 end) 2月节余,
......
sum(case when 月份 = 12 then 本月报销 else 0 end) 12月支出,
sum(case when 月份 = 12 then 本月节余 else 0 end) 12月节余
from 医疗台帐表
group by 年份
dlkfth 2003-07-16
  • 打赏
  • 举报
回复
select 年份 case when 月份=1 then sum(本月报销) end ....
group by 年份 order by 年份

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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