with machineTable
as
(
select '北京' cityid, 1 as statusid,'AAA' as machineID from dual
union
select '北京' , 0 ,'BBB' from dual
union
select '北京' , 0 ,'CCC' from dual
union
select '广州' , 1 ,'DDD' from dual
union
select '上海' , 0 ,'EEE' from dual
)
select mar.*
,decode(a,'0','0',decode(b,'0','0',100*ROUND(b/a,4) ------解决方案-------------------- '%'))as c
from
(
select distinct cityid
,sum(case when 1=1 then 1 else 0 end) over(partition by cityid) a
,sum(case when 1=1 and statusid=1 then 1 else 0 end) over(partition by cityid) b
from machineTable
) mar ------解决方案--------------------
[SYS@myoracle] SQL>with t1 as(
2 select date'2012-01-01' col1, 1000 col2, '0001' col3 from dual union all
3 select date'2012-01-01' col1, 2222 col2, '0004' col3 from dual union all
4 select date'2012-01-01' col1, 3000 col2, '0001' col3 from dual union all
5 select date'2012-01-02' col1, 1000 col2, '0002' col3 from dual union all
6 select date'2012-01-02' col1, 2222 col2, '0004' col3 from dual union all
7 select date'2012-01-02' col1, 3000 col2, '0003' col3 from dual union all
8 select date'2012-01-03' col1, 1000 col2, '0002' col3 from dual union all
9 select date'2012-01-03' col1, 2222 col2, '0004' col3 from dual union all
10 select date'2012-01-03' col1, 3000 col2, '000