select case when city_code is null then '合计' else city_code end city_code, city, ptuser_daily, agentuser_daily, total_user_daily, ptuser_month, agentuser_month, total_user_month
from(select case when m5.city_code is null then '合计' else m5.city_code end city_code, m5.city, m1.pyuser ptuser_daily, m2.agentuser agentuser_daily, sum(m1.pyuser)+sum(m2.agentuser) total_user_daily, m3.ptuser ptuser_month, m4.agentuser agentuser_month, sum(m3.ptuser) + sum(m4.agentuser) total_user_month from ( select case when city_code is null then '合计' else city_code end city_code, sum(pyuser)pyuser from ( select count(*) pyuser, t.city_code
from MOBILBABY_business_order_T t where substr(t.agent_code, 0, 5) = '10010' and t.operate_date < to_date(to_char(sysdate - 1, 'yyyymmdd'), 'yyyymmdd') and to_char(t.operate_date, 'yyyymmdd') = '20090601'
group by t.city_code
) group by rollup(city_code))m1,
(
select case when city_code is null then '合计' else city_code end city_code, sum(agentuser)agentuser
from
(select count(*) agentuser, t.city_code
from MOBILBABY_business_order_T t where substr(t.agent_code, 0, 5) != '10010' and t.operate_date < to_date(to_char(sysdate - 1, 'yyyymmdd'), 'yyyymmdd') and to_char(t.operate_date, 'yyyymmdd') = '20090601' group by t.city_code
) group by city_code) m2,
( select case when city_code is null then '合计' else city_code end city_code,
sum(ptuser)ptuser from (
select count(*) ptuser, t.city_code
from MOBILBABY_business_order_T t where substr(t.agent_code, 0, 5) = '10010' and t.operate_date between to_date(to_char(sysdate - 1, 'yyyymm'), 'yyyymm') and to_date(to_char(add_months(sysdate, 1), 'yyyymm'), 'yyyymm') and to_char(t.operate_date, 'yyyymmdd') = '20090601' group by t.city_code
)group by rollup(city_code)) m3,
(
select case when city_code is null then '合计' else city_code end city_code, sum(agentuser)agentuser from (
select count(*) agentuser, t.city_code
from MOBILBABY_business_order_T t where substr(t.agent_code, 0, 5) != '10010' and t.operate_date between to_date(to_char(sysdate - 1, 'yyyymm'), 'yyyymm') and to_date(to_char(add_months(sysdate, 1), 'yyyymm'), 'yyyymm') and to_char(t.operate_date, 'yyyymmdd') = '20090601' group by t.city_code
) group by city_code) m4, (select t.city_code, t.city from MOBILBABY_business_order_T t where to_char(t.operate_date, 'yyyymmdd') = '20090601' group by t.city_code, t.city) m5 where m5.city_code = m2.city_code(+) and m5.city_code = m4.city_code(+) and m5.city_code = m1.city_code(+) and m5.city_code = m3.city_code(+)
group by m5.city_code, m5.city, m1.pyuser, m2.agentuser, m3.ptuser, m4.agentuser
) group by rollup(city_code), city, ptuser_daily, agentuser_daily, total_user_daily, ptuser_month, agentuser_month, total_user_month
case when city_code is null
then '合计' else city_code end city_code,
city,
ptuser_daily,
agentuser_daily,
total_user_daily,
ptuser_month,
agentuser_month,
total_user_month
from(select case
when m5.city_code is null then
'合计'
else
m5.city_code
end city_code,
m5.city,
m1.pyuser ptuser_daily,
m2.agentuser agentuser_daily,
sum(m1.pyuser)+sum(m2.agentuser) total_user_daily,
m3.ptuser ptuser_month,
m4.agentuser agentuser_month,
sum(m3.ptuser) + sum(m4.agentuser) total_user_month from (
select
case when city_code is null
then '合计' else city_code end city_code,
sum(pyuser)pyuser
from (
select count(*) pyuser,
t.city_code
from MOBILBABY_business_order_T t
where substr(t.agent_code, 0, 5) = '10010'
and t.operate_date <
to_date(to_char(sysdate - 1, 'yyyymmdd'), 'yyyymmdd')
and to_char(t.operate_date, 'yyyymmdd') = '20090601'
group by t.city_code
) group by rollup(city_code))m1,
(
select
case when city_code is null
then '合计' else city_code end city_code,
sum(agentuser)agentuser
from
(select count(*) agentuser,
t.city_code
from MOBILBABY_business_order_T t
where substr(t.agent_code, 0, 5) != '10010'
and t.operate_date <
to_date(to_char(sysdate - 1, 'yyyymmdd'), 'yyyymmdd')
and to_char(t.operate_date, 'yyyymmdd') = '20090601'
group by t.city_code
) group by city_code) m2,
(
select case when city_code is null
then '合计' else city_code end city_code,
sum(ptuser)ptuser
from (
select count(*) ptuser,
t.city_code
from MOBILBABY_business_order_T t
where substr(t.agent_code, 0, 5) = '10010'
and t.operate_date between
to_date(to_char(sysdate - 1, 'yyyymm'), 'yyyymm') and
to_date(to_char(add_months(sysdate, 1), 'yyyymm'), 'yyyymm')
and to_char(t.operate_date, 'yyyymmdd') = '20090601'
group by t.city_code
)group by rollup(city_code)) m3,
(
select case when city_code is null
then '合计' else city_code end city_code,
sum(agentuser)agentuser from (
select count(*) agentuser,
t.city_code
from MOBILBABY_business_order_T t
where substr(t.agent_code, 0, 5) != '10010'
and t.operate_date between
to_date(to_char(sysdate - 1, 'yyyymm'), 'yyyymm') and
to_date(to_char(add_months(sysdate, 1), 'yyyymm'), 'yyyymm')
and to_char(t.operate_date, 'yyyymmdd') = '20090601'
group by t.city_code
) group by city_code) m4,
(select t.city_code, t.city
from MOBILBABY_business_order_T t
where to_char(t.operate_date, 'yyyymmdd') = '20090601'
group by t.city_code, t.city) m5 where m5.city_code = m2.city_code(+)
and m5.city_code = m4.city_code(+)
and m5.city_code = m1.city_code(+)
and m5.city_code = m3.city_code(+)
group by m5.city_code,
m5.city,
m1.pyuser,
m2.agentuser,
m3.ptuser,
m4.agentuser
)
group by rollup(city_code),
city,
ptuser_daily,
agentuser_daily,
total_user_daily,
ptuser_month,
agentuser_month,
total_user_month
/* order by city_code */
另外你最后的GROUP BY有这么多字段,ROLLUP不一定能符合你要求