Home > OS >  percentage difference when using CASE WHEN clause POSTGRES
percentage difference when using CASE WHEN clause POSTGRES

Time:01-29

Is this the correct syntax? I am using the traditional method of (new/old)-1 to work out the % difference between yesterday's registrations and a week from yesterday's registrations. I am getting '0' which does not seem right...

select
sum(case when (timestampregistered::date = current_date - 1) then 1 else 0 end)
    / sum(case when timestampregistered::date = current_date - 8 then 1 else 0 end) - 1 as pct_diff
from customers_table

CodePudding user response:

You are doing integer arithmetics, so your answers will be integers. Try

[...]then 1.0 else 0.0 end
  •  Tags:  
  • Related