Skip to content

Instantly share code, notes, and snippets.

@rcackerman
Last active February 13, 2016 18:53
Show Gist options
  • Select an option

  • Save rcackerman/55b8ff81a72482b981a9 to your computer and use it in GitHub Desktop.

Select an option

Save rcackerman/55b8ff81a72482b981a9 to your computer and use it in GitHub Desktop.
coop queries
with prices as (select new.name, new.units, old_price, new_price, new_price::float - old_price::float as price_diff, (old_price + 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 pcg_change;
-- works on the server
with prices as (
select
new.name,
new.units,
old_price,
new_price,
new_price::float - old_price::float as price_diff,
(old_price + 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 pcg_change
;
select
new.name,
new.units,
old_price,
new_price,
new_price::float - old_price::float as price_diff,
(old_price + 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment