with t as
(select 
       sum(case when to_char(a.issued_date,'yyyymm')<= '200301' then 1 else 0 end) a,
       sum(case when to_char(a.issued_date,'yyyymm')<= '200302' then 1 else 0 end) b,
       sum(case when to_char(a.issued_date,'yyyymm')<= '200303' then 1 else 0 end) c,
       sum(case when to_char(a.issued_date,'yyyymm')<= '200304' then 1 else 0 end) d,
       sum(case when to_char(a.issued_date,'yyyymm')<= '200305' then 1 else 0 end) e
from ecc_bj.ecc_card a
where a.issued_date is not null)
select '200301', t.a from t
union all
select '200302', t.b from t
union all
select '200303', t.c from t
union all
select '200304', t.d from t
union all
select '200305', t.e from t

解决方案 »

  1.   

    select m.issue_month, count(ds.issue_date)
    from ( select * from ecc_bj.ecc_card where issued_date is not null ) ds,
         ( select distinct to_char(issue_date, 'YYYYMM') issue_month
          from ecc_bj.ecc_card where issued_date is not null ) m
    where to_char(ds.issue_date, 'YYYYMM') <= m.issue_month
    group by m.issue_month
      

  2.   


    select issue_month, counts
    from 
        ( select m.issue_month, count(ds.issue_date) counts
          from ( select * from ecc_bj.ecc_card where issued_date is not null ) ds,
               ( select distinct to_char(issue_date, 'YYYYMM') issue_month
                from ecc_bj.ecc_card where issued_date is not null ) m
          where to_char(ds.issue_date, 'YYYYMM') <= m.issue_month
          group by m.issue_month ) t
    where t.issue_month >= '200301'
    and t.issue_month <= '200305'
      

  3.   

    谢谢俩位,我想问一下  with t as这个语句的用法是什么呢?谢谢
      

  4.   

    with t as()
    是把括号里的查询结果作为一个表,别名为t