Table 1Id   productNo  status
1     101       Active
2     201       IsActive
3     101       Active
4     101       Active想通过productNo查询出 Active状态总和,isActive的状态总和。比如给定101 ,查询出如下:
 productNo     Current  Future 
  101            3         1

解决方案 »

  1.   

    刚才的帖子有点错误,补充下
    Table 1Id   productNo  status
    1     101       Active
    2     201       IsActive
    3     101       Active
    4     101       Active
    5     101       IsActive想通过productNo查询出 Active状态总和,isActive的状态总和。比如给定101 ,查询出如下:
     productNo     Current(Active)  Future (IsActive)
      101            3                1
      

  2.   


    create table tb(Id int , productNo  varchar2(10),status varchar2(20)) ;
    insert into tb
    select '1','101','Active' from dual
    union all
    select '2','201','IsActive' from dual
    union all
    select '3','101','Active' from dual
    union all
    select '4','101','Active' from dual
    union all
    select '5','101','IsActive' from dual
    union all
    select '6','101','IsActive' from dual
    union all
    select '7','201','IsActive' from dual;select productNo, ( select  count(*)     from tb where   trim(status)='Active') as Current_Active,
                    ( select  count(*)   from tb  where    productNo='101' and trim(status)='IsActive') as Future_IsActive
                     from tb  where  productNo='101'  group by productNo
    测试结果如下:
    productNo     Current_Active  Future_IsActive
    101              3                 2
      

  3.   

    SQL> select tt.productNo,
      2         sum(case when status='Active' then 1 else 0 end) as Current_Active,
      3         sum(case when status='IsActive' then 1 else 0 end) as Current_IsActive
      4    from (
      5          select 1 as id,101 as productNo,'Active' as status from dual
      6          union all
      7          select 2 as id,201 as productNo,'IsActive' as status from dual
      8          union all
      9          select 3 as id,101 as productNo,'Active' as status from dual
     10          union all
     11          select 4 as id,101 as productNo,'Active' as status from dual
     12          union all
     13          select 5 as id,101 as productNo,'IsActive' as status from dual
     14         )tt
     15   where tt.productNo = 101
     16   group by tt.productNo; PRODUCTNO CURRENT_ACTIVE CURRENT_ISACTIVE
    ---------- -------------- ----------------
           101              3                1
      

  4.   

    感谢liuzi123();感谢mantisXF(枫の叶) 。但是这道题只有5分,而liuzi123()的方案非常适合我的程序,分数只能给liuzi123()了。