select
DECODE(GROUPING(t.quarter_year)+GROUPING(o.order_mode),0, t.quarter_year,1, DECODE(GROUPING(t.quarter_year),0,'Total for '||t.quarter_year,'Total for '||o.order_mode),2, 'Grand total') "Quarter",
decode(GROUPING(t.quarter_year)+GROUPING(o.order_mode),0, o.order_mode,null) "Order mode",
nvl(sum(order_total),0) "Sum of order amount",
TO_CHAR(100 * nvl(RATIO_TO_REPORT(SUM(order_total)) OVER (PARTITION BY (GROUPING(o.order_mode) || t.quarter_year)),0),'990D0') "% of contribution in a Quarter"
from oehr_time_dim t,oehr_orders o
where t.dat <= to_date('31-Dec-1998', 'dd-Mon-yyyy')
and o.time_dim_id = t.time_dim_id
group by cube(quarter_year,order_mode)
order by to_number(substr(t.quarter_year,4)),to_number(substr(t.quarter_year,2,1)), o.order_mode