Skip to content

Instantly share code, notes, and snippets.

@JoseSoteloCohen
Last active June 25, 2021 06:20
Show Gist options
  • Save JoseSoteloCohen/fa783df9ebadec23b945cd1f0705505e to your computer and use it in GitHub Desktop.
Save JoseSoteloCohen/fa783df9ebadec23b945cd1f0705505e to your computer and use it in GitHub Desktop.
with _data as (
select
date(orders.completed_at) as date,
sum(orders.order_total) as sales_total,
case
when date between '2019-10-01' and '2019-10-31' then 2100000
when date between '2019-11-01' and '2019-11-30' then 2500000
when date between '2019-12-01' and '2019-12-31' then 3300000
else 0
end as goal
from orders
where completed_at between '2019-10-01' and '2019-12-31'
group by date
)
select
date,
sum(sales_total) over (order by date asc rows unbounded preceding) as Total_Running_Sales,
goal,
((Total_Running_Sales/goal)-1)*100 as variance_from_goal
from _data;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment