id date day_of_week hour_of_day minute_of_hour km_cycled
1 1988-01-04 00:48:00-05 1 0 48 45
2 1988-01-18 16:40:00-05 1 16 40 4
3 1988-01-18 11:30:00-05 1 11 30 45
This file contains 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
/** | |
* Recursively computes the nth fibonachi #. This method is super slow because | |
* there is a tonnn of duplicated work. | |
* | |
* runtime: O(n*(n-1)(n-2) + n(n-2)) = O(n^2) | |
* | |
* @param {number} n | |
* @return {number} | |
*/ | |
function naiveFibonacci(n) { |
This file contains 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 sum(km_cycled) as total_km_cycled, | |
case when day_of_week=0 then 'Sunday' | |
when day_of_week=1 then 'Monday' | |
when day_of_week=2 then 'Tuesday' | |
when day_of_week=3 then 'Wednesday' | |
when day_of_week=4 then 'Thursday' | |
when day_of_week=5 then 'Friday' | |
when day_of_week=6 then 'Saturday' | |
end as day | |
from cycling_entry |
This file contains 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 sum(km_cycled) as km_cycled_for_all_mondays | |
from cycling_entry | |
where day_of_week = 1 | |
and hour_of_day > 11; | |
-- result: 39982 | |
-- average time: 7.25ms | |
select sum(km_cycled) as km_cycled_for_all_mondays | |
from cycling_entry | |
where extract(dow from date) = 1 |
This file contains 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 * | |
from cycling_entry | |
where day_of_week = 1; | |
-- average time: 8.75ms | |
select * | |
from cycling_entry | |
where extract(dow from date) = 1; | |
-- average time: 21ms |
This file contains 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 date_fields as ( | |
select id, | |
extract(dow from date) as day_of_week, | |
extract(hour from date) as hour_of_day, | |
extract(minute from date) as minute_of_hour | |
from cycling_entry | |
) | |
update cycling_entry | |
set day_of_week=subquery.day_of_week, | |
hour_of_day=subquery.hour_of_day, |
This file contains 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
create table cycling_entry ( | |
id serial primary key, | |
date timestamptz, | |
day_of_week smallint default 0, | |
hour_of_day smallint default 0, | |
minute_of_hour smallint default 0, | |
km_cycled smallint | |
); |
This file contains 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 days as ( | |
select * | |
from generate_series( | |
'1988-01-01T00:00:00'::timestamptz, | |
'2019-01-01T00:00:00'::timestamptz - '1 day'::interval, | |
'1 day'::interval | |
) as date | |
) | |
-- uncomment to insert into the table |
This file contains 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
-- returns a random number from within a range (inclusive) | |
create or replace function int_between( | |
in low int, | |
in high int, | |
out int | |
) | |
returns int | |
language sql | |
as $$ | |
select floor(random() * (high - low + 1) + low)::int |
This file contains 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
# setup | |
customer = Stripe::Customer.create(email: 'bob@bobsburgers2.soy') | |
customer.sources.create(source: { object: 'card', exp_month: 12, exp_year: 22, number: '4242424242424242', cvc: 123 }) | |
coupon = Stripe::Coupon.create(id: 'THEDUDEABIDES_2', duration: 'forever', percent_off: 25) | |
plan_month_1 = Stripe::Plan.create(id: 'tmp_monthly_plan_1', name: 'MONTH 1', interval: 'month', amount: 9900, currency: 'usd') | |
plan_month_2 = Stripe::Plan.create(id: 'tmp_monthly_plan_2', name: 'MONTH 2', interval: 'month', amount: 9900, currency: 'usd') | |
plan_year = Stripe::Plan.create(id: 'tmp_annual_plan', name: 'YEAR 1', interval: 'year', amount: 999900, currency: 'usd') |
NewerOlder