Question: I am looking for a way to log not only query itself (like `select * from users where...`) but also result of the query.

for example:

I asked for `User` model in a query and I want to see what was returned from the database, something like: `{user_id: 30, email: 'email@example.com', name: 'whatever'}`.

Answer: 
Use conditional aggregation:

```
SELECT SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) as denom,
       SUM(CASE WHEN status <> 'completed' THEN 1 ELSE 0 END) as num,
       AVG(CASE WHEN status <> 'completed' THEN 1.0 ELSE 0 END) as cancel_rate
FROM trips 
GROUP BY request_at;

```

Note that calculation for the `cancel_rate`. This is simpler to do using `AVG()` rather than dividing the two values. The use of `1.0`is because SQL Server does integer arithmetic, so `1 / 2` is 0 rather than 0.5.
OK, a bit late again, but here is another variation (edited):

```
SELECT SUM(CASE LEFT(status,9) WHEN 'cancelled' THEN 1. ELSE 0 END)
      /COUNT(*) cancellation_rate,
       request_at 
FROM trips GROUP BY request_at ORDER BY request_at

```