Reputation: 1563
Let's say I have a table representing sales with these attributes:
state
that can be sold
, waiting
, unknown
or cancelled
vendor
buyer
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 dividing 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?
Upvotes: 0
Views: 120
Reputation: 142433
To do it all in one query:
SELECT
( SELECT ... ) / -- How many cancelled (as already formulated)
( SELECT ... ); -- Total number (as already formulated)
Upvotes: 1
Reputation: 1270573
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.
Upvotes: 1