Combined APEX chart with ORDERS table

This demo is base on the standard demo tables ORDERS et ORDER_ITEMS.
The goal is to produce a chart with a monthly amount stacked with variance and a average monthly amount.

Build a combined chart and and two series from the tow following queries

Statement 1: implemented as a Stacked bar serie

select mm, b.mnt, 'mnt' serie from
(
select trunc(order_datetime,'MM') mm,
sum((unit_price *quantity)) mnt,
trunc(variance((unit_price *quantity)),2) vari
from orders d,
order_items i
where i.order_id = d.order_id
group by trunc(order_datetime,'MM')
order by 1
) b
UNION
select mm, b.mnt , 'variance' serie from
(
select trunc(order_datetime,'MM') mm,
trunc(variance((unit_price *quantity)),2) mnt
from orders d,
order_items i
where i.order_id = d.order_id
group by trunc(order_datetime,'MM')
order by 1
) b

Statement 2: implemented as a line serie

select b.mm,a.avgi, b.mnt, b.vari from
( select trunc(avg(mnt),2) avgi
from (
select trunc(order_datetime,'MM') mm,
sum((unit_price *quantity)) mnt
from orders d,
order_items i
where i.order_id = d.order_id
group by trunc(order_datetime,'MM')
)
) a,
(
select trunc(order_datetime,'MM') mm,
sum((unit_price *quantity)) mnt,
trunc(variance((unit_price *quantity)),2) vari
from orders d,
order_items i
where i.order_id = d.order_id
group by trunc(order_datetime,'MM')
order by 1
) b

About the author

GPM Factory