MySQL: Count(*) NULL -> 0 with right join
MySQL: Count(*) NULL -> 0 with right join
我即将完成一道 LeetCode 难题。假设 driver 或客户都未被禁止使用拼车服务,我们的想法是找到某个日期范围内的取消率(百分比)。
我 运行 遇到的问题是 10 月 2 日没有取消;使用内部联接时,该日期将被省略。使用右连接,该值返回为 NULL。我需要将此 NULL 值映射到 0。
问题:https://leetcode.com/problems/trips-and-users/
我的代码:
SELECT output2.day AS "Day", ROUND(output1.failed/output2.success,2) AS "Cancellation Rate"
FROM
((SELECT t1.request_at AS Day, COUNT(*) AS failed
FROM Trips T1
WHERE T1.Status != 'completed'
AND (T1.request_at BETWEEN '2013-10-01' AND '2013-10-03')
AND T1.client_id IN
(SELECT users_id
FROM users
WHERE role = 'client'
AND banned = 'No')
AND t1.driver_id IN
(SELECT users_id
FROM users U2
WHERE role = 'driver'
AND banned = 'No')
GROUP BY t1.request_at
) AS output1
RIGHT JOIN
(SELECT t2.request_at AS Day, COUNT(*) AS success
FROM Trips T2
WHERE (T2.request_at BETWEEN '2013-10-01' AND '2013-10-03')
AND T2.client_id IN
(SELECT users_id
FROM users
WHERE role = 'client'
AND banned = 'No')
AND t2.driver_id IN
(SELECT users_id
FROM users
WHERE role = 'driver'
AND banned = 'No')
GROUP BY t2.request_at
) AS output2
ON output1.Day = output2.Day)
GROUP BY output2.day
我的输出:
{"headers": ["Day", "Cancellation Rate"], "values": [["2013-10-01", 0.33], ["2013-10-02", null], ["2013-10-03", 0.50]]}
期望的输出:
{"headers": ["Day", "Cancellation Rate"], "values": [["2013-10-01", 0.33], ["2013-10-02", 0.00], ["2013-10-03", 0.50]]}
我看过各种社区推荐,例如使用函数 ISNULL 或 IFNULL。但是都返回了一个错误。有什么想法吗?
您可以在最外层的查询中使用 COALESCE()
将 NULL
值转换为 0
s:
SELECT
output2.day AS "Day",
COALESCE(ROUND(output1.failed/output2.success,2), 0) AS "Cancellation Rate"
FROM ...
但是请注意,使用联接和条件聚合可以大大简化您的查询。像这样的东西应该接近你想要的:
select
t.request_at as day,
avg(t.status != 'completed') as cancellation_rate
from trips t
inner join users uc
on uc.users_id = t.client_id and uc.role = 'client' and uc.banned = 'no'
inner join users ud
on ud.users_id = t.client_id and ud.role = 'driver' and ud.banned = 'no'
where t.request_at between '2013-10-01' and '2013-10-03'
group by t.request_at
我即将完成一道 LeetCode 难题。假设 driver 或客户都未被禁止使用拼车服务,我们的想法是找到某个日期范围内的取消率(百分比)。
我 运行 遇到的问题是 10 月 2 日没有取消;使用内部联接时,该日期将被省略。使用右连接,该值返回为 NULL。我需要将此 NULL 值映射到 0。
问题:https://leetcode.com/problems/trips-and-users/
我的代码:
SELECT output2.day AS "Day", ROUND(output1.failed/output2.success,2) AS "Cancellation Rate"
FROM
((SELECT t1.request_at AS Day, COUNT(*) AS failed
FROM Trips T1
WHERE T1.Status != 'completed'
AND (T1.request_at BETWEEN '2013-10-01' AND '2013-10-03')
AND T1.client_id IN
(SELECT users_id
FROM users
WHERE role = 'client'
AND banned = 'No')
AND t1.driver_id IN
(SELECT users_id
FROM users U2
WHERE role = 'driver'
AND banned = 'No')
GROUP BY t1.request_at
) AS output1
RIGHT JOIN
(SELECT t2.request_at AS Day, COUNT(*) AS success
FROM Trips T2
WHERE (T2.request_at BETWEEN '2013-10-01' AND '2013-10-03')
AND T2.client_id IN
(SELECT users_id
FROM users
WHERE role = 'client'
AND banned = 'No')
AND t2.driver_id IN
(SELECT users_id
FROM users
WHERE role = 'driver'
AND banned = 'No')
GROUP BY t2.request_at
) AS output2
ON output1.Day = output2.Day)
GROUP BY output2.day
我的输出:
{"headers": ["Day", "Cancellation Rate"], "values": [["2013-10-01", 0.33], ["2013-10-02", null], ["2013-10-03", 0.50]]}
期望的输出:
{"headers": ["Day", "Cancellation Rate"], "values": [["2013-10-01", 0.33], ["2013-10-02", 0.00], ["2013-10-03", 0.50]]}
我看过各种社区推荐,例如使用函数 ISNULL 或 IFNULL。但是都返回了一个错误。有什么想法吗?
您可以在最外层的查询中使用 COALESCE()
将 NULL
值转换为 0
s:
SELECT
output2.day AS "Day",
COALESCE(ROUND(output1.failed/output2.success,2), 0) AS "Cancellation Rate"
FROM ...
但是请注意,使用联接和条件聚合可以大大简化您的查询。像这样的东西应该接近你想要的:
select
t.request_at as day,
avg(t.status != 'completed') as cancellation_rate
from trips t
inner join users uc
on uc.users_id = t.client_id and uc.role = 'client' and uc.banned = 'no'
inner join users ud
on ud.users_id = t.client_id and ud.role = 'driver' and ud.banned = 'no'
where t.request_at between '2013-10-01' and '2013-10-03'
group by t.request_at