nicoco nicoco - 16 days ago 10
SQL Question

Get the cancelling rate of a vendor ignoring a certain state, in one query

Let's say I have a table representing sales with these attributes:


  • a
    state
    that can be
    sold
    ,
    waiting
    ,
    unknown
    or
    cancelled

  • a
    vendor

  • a
    buyer

  • a
    cancellor
    that is either the
    vendor
    or the
    buyer
    in case the sale's
    state
    is
    cancelled
    , and
    null
    for other states.



I want to compute the cancelling rate for Joe. Right now I'm doing two SQL requests. The first one gets the number of cancelled sales by Joe:

SELECT COUNT(*) AS n_cancelled FROM sales WHERE cancellor = "Joe"


The second one gets the number of sales where Joe was involved, excluding the ones whose
state
is
unknown
or
waiting
:

SELECT COUNT(*) AS n_sales FROM sales
WHERE
STATE in ("sold", "cancelled")
AND (vendor = "Joe" OR buyer = "Joe")


I then compute my cancelling rate by diving the results of these two requests.

I'm interested in doing so in one request that would output the cancelling rate and also the number of sales used to compute it (
n_sales
here). How would I do that?

Answer

You can use conditional aggregation:

SELECT SUM(cancellor = 'Joe') as n_cancelled,
       SUM(state in ('sold', 'cancelled') AND 'Joe' IN (vendor, buyer)) AS n_sales
FROM sales
WHERE ( (cancellor = 'Joe') or
        (state in ('sold', 'cancelled') AND 'Joe' IN (vendor, buyer))
      );

You can do the division in the query as well.

Note: This keeps the WHERE clause, which is not strictly necessary. But reducing the number of rows before aggregating the data can improve performance.