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

解决方案 »

  1.   

    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')
      

  2.   

    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')
      

  3.   


    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(+);
      

  4.   

    --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(+);
      

  5.   

    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         30select 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个地方
      

  6.   

    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的记录?
      

  7.   

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

  8.   

    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;
      

  9.   

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

  10.   

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

  11.   

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