create
or replace function tip_percentage(tip_column float, total_column float)
returns numeric
language plpgsql
as $$
declare tip_percentage numeric;
begin if total_column = 0 then tip_percentage = 0;
elsif total_column is null then tip_percentage = 0;
elsif total_column > 0 then tip_percentage = tip_column / total_column;
end if;
return tip_percentage;
end;
$$
select
total_amount,
tip_amount,
tip_percentage(tip_amount, total_amount)
from
nyc_yellow_taxi_0601_0615_2016
order by
pickup_datetime desc
limit
10 offset 10000