Skip to content

Instantly share code, notes, and snippets.

@hampelm
Created December 11, 2016 18:26
Show Gist options
  • Select an option

  • Save hampelm/19942e275e8c0bd1e695b4837282e279 to your computer and use it in GitHub Desktop.

Select an option

Save hampelm/19942e275e8c0bd1e695b4837282e279 to your computer and use it in GitHub Desktop.
with prices as (
select new.name, new.units, old_price, new_price, new_price::float - old_price::float as price_diff, (old_price %2B new_price)/2.0 as price_avg
from (
select name, units, price as old_price
from coop where date = current_date - '7 days'::interval
) old
join (
select name, units, price as new_price
from coop where date = (select max(date) from coop)
) new
on old.name = new.name and old.units = new.units
)
select *, price_diff/price_avg * 100 as pcg_change
from prices order by price_diff;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment