我使用了 cte 和 joins,但我的输出与预期输出略有不同。 这是我的查询;
> with cte AS
>
> (SELECT id, request_at AS Day, if(status != 'completed',1,0) AS cancelled
> FROM Trips t
> INNER JOIN Users u
> ON t.client_id = u.users_id OR
> t.driver_id = u.users_id
> WHERE banned = 'NO' AND
> (request_at BETWEEN '2013-10-01' AND '2013-10-03')
> )
>
> SELECT
> Day,
> ROUND(SUM(Cancelled) / COUNT(id), 2)
AS 'Cancellation Rate'
FROM cte GROUP BY Day
输出
输出: +------------+------------------+ |天 |取消率 | +------------+------------------+ | 2013-10-01 | 0.43 | | 2013-10-02 | 0.00 | | 2013-10-03 | 0.40 | +------------+-----------------+
预计 |天|取消率 | | :--------:| :----------------:| | 2013-10-01 | 0.33 | | 2013-10-02 | 0 | | 2013-10-03 | 0.5 |
有人可以解释一下我在哪里犯了错误吗?
我也尝试加入 Users 表 2 次,但这也没有用,并抛出错误。