CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Oracle >  基础和管理

这个sql如何写?

楼主2000lhzh(一叶孤舟)2005-11-16 18:20:42 在 Oracle / 基础和管理 提问

现有表结构如下:  
  year     month   value     code  
  2005       01         100         123123  
  2005       02         300         123123  
  2005       03         300         123123  
  2005       04         40           123123  
  2005       05         40           123123  
   
  2005       01         200           5241  
  2005       02         200           5241  
   
  2005       01       200             241  
  2005       03       200             241  
   
  如何得到如下的结果:  
      code                   1月             2月         3   月           4月       5月         6月         7月   。。。  
  123123                   100             300         300             40       40               0           0          
    5241                     200             200         0                 0           0               0           0  
      241                     200             0             200             0           0               0           0  
  没有数值得月份数据为0、  
   
  如何实现???  
   
  问题点数:20、回复次数:1Top

1 楼wgsasd311(自强不息)回复于 2005-11-16 18:49:53 得分 20

create   table   tb(year   varchar2(4),month   varchar2(2),  
  value   number,code   varchar2(10));  
  insert   into   tb   values(2005,'01',100,123123);  
  insert   into   tb   values(2005,'02',300,123123);  
  insert   into   tb   values(2005,'03',300,123123);  
  insert   into   tb   values(2005,'04',40,   123123);  
  insert   into   tb   values(2005,'05',40,   123123);  
   
  insert   into   tb   values(2005,'01',200,5241);  
  insert   into   tb   values(2005,'02',200,5241);  
   
  insert   into   tb   values(2005,'01',200,241);  
  insert   into   tb   values(2005,'03',200,241);  
   
  select   code,  
  sum(case   month   when   '01'   then   value   else   0   end)     "1月",  
  sum(case   month   when   '02'   then   value   else   0   end)     "2月",  
  sum(case   month   when   '03'   then   value   else   0   end)     "3月",  
  sum(case   month   when   '04'   then   value   else   0   end)     "4月",  
  sum(case   month   when   '05'   then   value   else   0   end)     "5月",  
  sum(case   month   when   '06'   then   value   else   0   end)     "6月",  
  sum(case   month   when   '07'   then   value   else   0   end)     "7月",  
  sum(case   month   when   '08'   then   value   else   0   end)     "8月",  
  sum(case   month   when   '09'   then   value   else   0   end)     "9月",  
  sum(case   month   when   '10'   then   value   else   0   end)     "10月",  
  sum(case   month   when   '11'   then   value   else   0   end)     "11月",  
  sum(case   month   when   '12'   then   value   else   0   end)     "12月"  
  from   tb   group   by   code;  
   
  drop   table   tb;Top

相关问题

  • 这个Sql 如何写?
  • 这个sql如何改写??
  • 这个SQL该如何写?
  • 这个sql如何写?
  • 这个SQL如何写?
  • 这个sql该如何写
  • 如何写这个SQL?
  • 这个sql该如何写?
  • 这个SQL该如何写?
  • 这个SQL如何写

关键词

  • code
  • sum
  • case month
  • insert
  • then value else
  • intotb value
  • varchar2
  • tb

得分解答快速导航

  • 帖主:2000lhzh
  • wgsasd311

相关链接

  • Oracle类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo