Last active
February 13, 2016 18:53
-
-
Save rcackerman/55b8ff81a72482b981a9 to your computer and use it in GitHub Desktop.
coop queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 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 | |
| ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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