急求高手解决一个查询语句!
Table 1
Id productNo status
1 101 Active
2 201 IsActive
3 101 Active
4 101 Active
想通过productNo查询出 Active状态总和,isActive的状态总和。
比如给定101 ,查询出如下:
productNo Current Future
101 3 1
------解决方案-------------------- 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