CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
花落谁家,你作主! 盛大widget设计大赛英雄榜
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Oracle >  开发

求一sql语句,请各位大侠帮忙!

楼主yohar(砍野菜的武當劍)2004-12-02 19:34:44 在 Oracle / 开发 提问

该查询如何写  
  表A  
  col1                         col2  
  2004-09-01               5  
  2004-09-02               11  
  2004-09-05               3  
  2004-09-12               5  
  2004-09-15               33  
  我当我输入2004-09这个条件时需要得到如下结果  
  2004-09-01               5  
  2004-09-02               11  
  2004-09-03                 0  
  2004-09-04                 0  
  2004-09-05                 3  
          .  
          .  
          .  
  2004-09-30                 0  
  请问这个sql怎么写呢 问题点数:50、回复次数:15Top

1 楼ITpassport(分不在高,会了就行)回复于 2004-12-02 19:44:00 得分 2

这好像一个sql语句写不出来啊Top

2 楼yxxx(_小孬)回复于 2004-12-02 20:26:54 得分 2

来学习Top

3 楼CodeMagic(ErrorDetector)回复于 2004-12-02 21:05:48 得分 5

select   to_char(col1,'yyyymmdd'),sum(col2)   from  
  (  
      select   col1,col2   from   tb   where   to_char(col1,'yyyymm')='200409'  
          union  
      select   to_date('20040901','yyyymmdd')+rownum-1,0   from   dba_tables   where   rownum<31  
  )  
  group   by   to_char(col1,'yyyymmdd')Top

4 楼GerryYang(轻尘)回复于 2004-12-02 21:12:11 得分 5

select   to_char(col1,'yyyymmdd'),sum(col2)   from  
  (  
      select   col1,col2   from   tb   where   to_char(col1,'yyyymm')='&YYYYMM'  
          union  
      select   to_date('20040901','yyyymmdd')+rownum-1   col1,0   col2   from   dba_tables   where   rownum<31  
  )  
  group   by   to_char(col1,'yyyymmdd')  
  Top

5 楼ORARichard(没钱的日子......)回复于 2004-12-02 23:55:17 得分 20

 
  select   a.m,b.col2   from    
  (select   to_date('2004-09','yyyy-mm')+rownum-1   m    
  from   dba_tables    
  where   rownum-1<=(select   last_day(to_date('2004-09','yyyy-mm'))-to_date('2004-09','yyyy-mm')   from   dual))   a,表A   b  
  where   a.m=b.col1(+);Top

6 楼ORARichard(没钱的日子......)回复于 2004-12-02 23:58:29 得分 0

--retry:  
   
  select   a.m,b.col2   from    
  (select   to_date('2004-09','yyyy-mm')+rownum-1   m    
  from   dba_tables    
  where   rownum-1<=(select   last_day(to_date('2004-09','yyyy-mm'))-to_date('2004-09','yyyy-mm')   from   dual))   a,表A   b  
  where   trunc(a.m)=b.col1(+);Top

7 楼ATGC(花开蝶舞,木秀鸟栖)回复于 2004-12-03 00:13:49 得分 16

SQL>   select   *   from   aa;  
   
  COL1                           COL2  
  ----------   ----------  
  2004-11-01                     5  
  2004-11-02                   15  
  2004-11-20                   12  
  2004-11-22                   18  
  2004-11-30                   30  
   
  select   b.col1,decode(a.col2,null,0,a.col2)   col2  
  from  
  (select   col1,col2   from   aa   where   substrb(col1,1,7)='2004-11')   a,  
  (  
  select   c.mydate||d.zero   col1,0   col2   from    
  (select   'gene'   gene,substrb(col1,1,7)   mydate   from   aa   where   substrb(col1,1,7)='2004-11'   and   rownum=1)   c,  
  (select   'gene'   gene,'-'||trim(to_char(rownum,'00'))   zero   from   dba_tables   where   rownum<=to_char(last_day(to_date('2004-11','yyyy-mm')),'dd'))   d  
  where   c.gene=d.gene  
  )   b  
  where   b.col1=a.col1(+)  
  /  
   
  COL1                             COL2  
  -----------   ----------  
  2004-11-01                       5  
  2004-11-02                     15  
  2004-11-03                       0  
  2004-11-04                       0  
  ..............略  
  2004-11-27                       0  
  2004-11-28                       0  
  2004-11-29                       0  
  2004-11-30                     30  
   
  已选择30行。  
   
  现在将条件改成'2004-12'  
  select   b.col1,decode(a.col2,null,0,a.col2)   col2  
  from  
  (select   col1,col2   from   aa   where   substrb(col1,1,7)='2004-12')   a,  
  (  
  select   c.mydate||d.zero   col1,0   col2   from    
  (select   'gene'   gene,substrb(col1,1,7)   mydate   from   aa   where   substrb(col1,1,7)='2004-12'   and   rownum=1)   c,  
  (select   'gene'   gene,'-'||trim(to_char(rownum,'00'))   zero   from   dba_tables   where   rownum<=to_char(last_day(to_date('2004-12','yyyy-mm')),'dd'))   d  
  where   c.gene=d.gene  
  )   b  
  where   b.col1=a.col1(+)  
  /  
   
  未选定行  
   
  再将表里的日期改掉  
  SQL>   update   aa   set   col1='2004-12-'||substrb(col1,9,2);  
   
  已更新5行。  
   
  SQL>   commit;  
   
  提交完成。  
   
  SQL>   select   *   from   aa;  
   
  COL1                           COL2  
  ----------   ----------  
  2004-12-01                     5  
  2004-12-02                   15  
  2004-12-20                   12  
  2004-12-22                   18  
  2004-12-30                   30  
   
  再执行语句  
  select   b.col1,decode(a.col2,null,0,a.col2)   col2  
  from  
  (select   col1,col2   from   aa   where   substrb(col1,1,7)='2004-12')   a,  
  (  
  select   c.mydate||d.zero   col1,0   col2   from    
  (select   'gene'   gene,substrb(col1,1,7)   mydate   from   aa   where   substrb(col1,1,7)='2004-12'   and   rownum=1)   c,  
  (select   'gene'   gene,'-'||trim(to_char(rownum,'00'))   zero   from   dba_tables   where   rownum<=to_char(last_day(to_date('2004-12','yyyy-mm')),'dd'))   d  
  where   c.gene=d.gene  
  )   b  
  where   b.col1=a.col1(+)  
  /  
   
  COL1                             COL2  
  -----------   ----------  
  2004-12-01                       5  
  2004-12-02                     15  
  2004-12-03                       0  
  2004-12-04                       0  
  ....................略  
  2004-12-28                       0  
  2004-12-29                       0  
  2004-12-30                     30  
  2004-12-31                       0  
   
  已选择31行。  
   
  =======================  
  这时候select到的是31行,因为12月是31天的。  
  语句会根据给定的月份自动判断是显示30行,28行,29行还是31行  
   
  我已经把条件->"月份"都写在了where子句里,不过要写3个地方  
   
  Top

8 楼ATGC(花开蝶舞,木秀鸟栖)回复于 2004-12-03 00:30:44 得分 0

to   ORARichard(没钱的日子好难过啊)    
        CodeMagic(ErrorDetector)    
   
        你们的语句,如果表里没有"2004-09“,也会select出来30条记录  
        不知道楼主是不是,在表里没有记录的情况下,也要显示该月的记录,都是0?  
   
  to     GerryYang(轻尘)  
        你的语句不管你输入什么值,都显示2004年09月的记录  
   
  我觉得条件不应该写在字段里  
  比如select   to_date('2004-09','yyyy-mm')   from   ......  
  我已经把条件都写在了where字句里  
  并且如果表里没有2004年9月的记录,我是不会select出来记录的,当然这也许我理解错误  
  也许应该如ORARichard(没钱的日子好难过啊)   和   CodeMagic(ErrorDetector那样  
  即使表里没有记录,也要显示30个或31个或28,或29条值为0的记录?  
  Top

9 楼ATGC(花开蝶舞,木秀鸟栖)回复于 2004-12-03 00:32:31 得分 0

补充,我的表的字段col1是varchar2型的,其它几位用的是date型的Top

10 楼yohar(砍野菜的武當劍)回复于 2004-12-03 09:12:06 得分 0

不知道楼主是不是,在表里没有记录的情况下,也要显示该月的记录,都是0?  
  --------------------是啊,如果该表中某一天没有记录,在查询出来的结果中补上默认值为0,  
  也就是该月如果为31天,就显示31条记录,如果为28天就显示28条记录Top

11 楼ORARichard(没钱的日子......)回复于 2004-12-03 09:15:01 得分 0

select   a.m,decode(b.col2,null,0,b.col2)   from    
  (select   to_date('2004-09','yyyy-mm')+rownum-1   m    
  from   dba_tables    
  where   rownum-1<=(select   last_day(to_date('2004-09','yyyy-mm'))-to_date('2004-09','yyyy-mm')   from   dual))   a,表A   b  
  where   trunc(a.m)=b.col1(+);  
   
  这句话会将col2的null值改成0;Top

12 楼ATGC(花开蝶舞,木秀鸟栖)回复于 2004-12-03 09:22:26 得分 0

那就行了,如ORARichard(没钱的日子好难过啊)   的就可以  
  我的改一下也可以  
  Top

13 楼hero1981(英雄)回复于 2004-12-03 09:27:40 得分 0

学习Top

14 楼yohar(砍野菜的武當劍)回复于 2004-12-03 09:44:44 得分 0

ATGC(我还以为我会永远守在她身旁。。)   的是对的,谢谢你,同时也谢谢大家的参与,给分Top

15 楼yohar(砍野菜的武當劍)回复于 2004-12-03 09:46:36 得分 0

哦,刚才忘了ORARichard(没钱的日子好难过啊),一起谢谢!Top

相关问题

  • 关于sql语句,各位请帮忙!
  • 请教各位大哥!!急,SQL语句。
  • 请教各位,SQL语句如何写?
  • 请教请教SQL SQL 语句语句^_^
  • 80分向各位SQL高手请教一条SQL语句?
  • SQL语句请教
  • 请教 SQL 语句
  • 请教SQL语句
  • 请教sql语句
  • sql语句请教?

关键词

  • 语句
  • dba
  • date
  • sql
  • null
  • col
  • 记录
  • substrb
  • gene
  • 表里

得分解答快速导航

  • 帖主:yohar
  • ITpassport
  • yxxx
  • CodeMagic
  • GerryYang
  • ORARichard
  • ATGC

相关链接

  • Oracle类图书

广告也精彩

反馈

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