Untitled

mail@pastecode.io avatar
unknown
pgsql
2 years ago
673 B
3
Indexable
SELECT cast(CancelsByNotBlockeds * 1.0 / AllordersByNotBlockeds AS DECIMAL(2, 2)) cancelation_rates
FROM (SELECT count(*) CancelsByNotBlockeds
      FROM orders o
               left join customers c on c.id = o.customer_id
      where (o.status = 'canceled' and
             c.is_blocked = '0')
         or (o.status = 'canceled' and
             c.is_blocked is null)) CancelsByNotBlockeds
         JOIN (SELECT count(*) AllordersByNotBlockeds
               FROM orders o
                        left join customers c on c.id = o.customer_id
               where c.is_blocked = '0'
                  or c.is_blocked is null) AllordersByNotBlockeds on 1 = 1