id   leixing   banzu  guoqi   shijian
1      A       班组1   T       2011-7-1
2      A       班组2   F       2009-9-1
3      B       班组2   T       2011-8-1
4      C       班组1   F       2011-9-1
在上面的表中,我要得到一组数据:在时间(shijian)2011-7-31前所有记录中,班组1  的过期(guoqi)的记录多少,班组1的 总记录多少, 类型(leixing)A的记录多少,B的记录多少用一条sql能实现吗?
请赐教

解决方案 »

  1.   

    select sum(case
                 when (banzu = '班组1' and guoqi = 'T') then
                  1
                 else
                  0
               end) as col1,
           sum(case
                 when banzu = '班组1' then
                  1
                 else
                  0
               end) as col2,
           sum(case
                 when leixing = 'A' then
                  1
                 else
                  0
               end) as col3,
           sum(case
                 when leixing = 'B' then
                  1
                 else
                  0
               end) ascol4
      from table
     where sj <= '2011-7-31'
      

  2.   

    with a as (select count(*) from biao where shijian<='2011-7-31')
         b as (select count(*) from biao where banzu = '班组1')
         c as (select count(*) from biao where leixing ='A')
         d as (select count(*) from biao where leixing = 'B')
    SELECT * FROM A,B,C,D
      

  3.   


    select count(case when banzu='班组1' and guoqi='T' then 1 end),
           count(case when banzu='班组1' then 1 end),
           count(decode(leixing,'A',1),
           count(decode(leixing,'B',1)
    from t
    where shijian<to_date('2011-07-31','yyyy-mm-dd')
    /
      

  4.   


    select count(case when banzu='班组1' and guoqi='T' then 1 end)
          ,count(decode(banzu,'班组1',1)),
          ,count(decode(leixing,'A',1))
          ,count(decode(leixing,'B',1))
    from t
    where shijian<to_date('2011-07-31','yyyy-mm-dd')
    /