明确后,Postgresql 总和无法按预期工作
Postgresql sum not working as expected when it is clear
我正在解决以下 Hard Leetcode SQL 问题。
Link 问题:https://leetcode.com/problems/trips-and-users/
(可以直接看解法理解问题)
问题:
行程table:
+----+-----------+-----------+---------+---------------------+------------+
| id | client_id | driver_id | city_id | status | request_at |
+----+-----------+-----------+---------+---------------------+------------+
| 1 | 1 | 10 | 1 | completed | 2013-10-01 |
| 2 | 2 | 11 | 1 | cancelled_by_driver | 2013-10-01 |
| 3 | 3 | 12 | 6 | completed | 2013-10-01 |
| 4 | 4 | 13 | 6 | cancelled_by_client | 2013-10-01 |
| 5 | 1 | 10 | 1 | completed | 2013-10-02 |
| 6 | 2 | 11 | 6 | completed | 2013-10-02 |
| 7 | 3 | 12 | 6 | completed | 2013-10-02 |
| 8 | 2 | 12 | 12 | completed | 2013-10-03 |
| 9 | 3 | 10 | 12 | completed | 2013-10-03 |
| 10 | 4 | 13 | 12 | cancelled_by_driver | 2013-10-03 |
+----+-----------+-----------+---------+---------------------+------------+
用户table:
+----------+--------+--------+
| users_id | banned | role |
+----------+--------+--------+
| 1 | No | client |
| 2 | Yes | client |
| 3 | No | client |
| 4 | No | client |
| 10 | No | driver |
| 11 | No | driver |
| 12 | No | driver |
| 13 | No | driver |
+----------+--------+--------+
输出:
+------------+-------------------+
| Day | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 | 0.33 |
| 2013-10-02 | 0.00 |
| 2013-10-03 | 0.50 |
+------------+-------------------+
这是我的代码:
SELECT request_at,
count(*) c,
sum(case when status='cancelled_by_driver' or status='cancelled_by_client' then 1 else 0 end) s,
round(sum(case when status='cancelled_by_client' or status='cancelled_by_client' then 1 else 0 end)/count(*),2) as Cancellation_rate
FROM trips
WHERE
client_id not in (select users_id from users where banned = 'Yes')
AND
driver_id not in (select users_id from users where banned = 'Yes')
GROUP BY request_at;
输出为:
request_at | c | s | cancellation_rate
------------+---+---+-------------------
2013-10-01 | 3 | 1 | 0.00
2013-10-03 | 2 | 1 | 0.00
2013-10-02 | 2 | 0 | 0.00
当通过查看前面的列 (s/c) 很清楚它应该是 0.33,0.50, 0.00 时,cancellation_rate 是 0.00。
好消息是你只是错别字而已。
在您的示例中,您使用的是 cancelled_by_client or cancelled_by_client
round(sum(case when status='cancelled_by_client' or status='cancelled_by_client' then 1 else 0 end)/count(*),2) as Cancellation_rate
而不是:
.. when status='cancelled_by_driver' or status='cancelled_by_client' then ..
这会 return:
request_at
c
s
cancellation_rate
2013-10-01
3
1
0.33
2013-10-02
2
0
0.00
2013-10-03
2
1
0.50
我正在解决以下 Hard Leetcode SQL 问题。
Link 问题:https://leetcode.com/problems/trips-and-users/ (可以直接看解法理解问题)
问题:
行程table:
+----+-----------+-----------+---------+---------------------+------------+
| id | client_id | driver_id | city_id | status | request_at |
+----+-----------+-----------+---------+---------------------+------------+
| 1 | 1 | 10 | 1 | completed | 2013-10-01 |
| 2 | 2 | 11 | 1 | cancelled_by_driver | 2013-10-01 |
| 3 | 3 | 12 | 6 | completed | 2013-10-01 |
| 4 | 4 | 13 | 6 | cancelled_by_client | 2013-10-01 |
| 5 | 1 | 10 | 1 | completed | 2013-10-02 |
| 6 | 2 | 11 | 6 | completed | 2013-10-02 |
| 7 | 3 | 12 | 6 | completed | 2013-10-02 |
| 8 | 2 | 12 | 12 | completed | 2013-10-03 |
| 9 | 3 | 10 | 12 | completed | 2013-10-03 |
| 10 | 4 | 13 | 12 | cancelled_by_driver | 2013-10-03 |
+----+-----------+-----------+---------+---------------------+------------+
用户table:
+----------+--------+--------+
| users_id | banned | role |
+----------+--------+--------+
| 1 | No | client |
| 2 | Yes | client |
| 3 | No | client |
| 4 | No | client |
| 10 | No | driver |
| 11 | No | driver |
| 12 | No | driver |
| 13 | No | driver |
+----------+--------+--------+
输出:
+------------+-------------------+
| Day | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 | 0.33 |
| 2013-10-02 | 0.00 |
| 2013-10-03 | 0.50 |
+------------+-------------------+
这是我的代码:
SELECT request_at,
count(*) c,
sum(case when status='cancelled_by_driver' or status='cancelled_by_client' then 1 else 0 end) s,
round(sum(case when status='cancelled_by_client' or status='cancelled_by_client' then 1 else 0 end)/count(*),2) as Cancellation_rate
FROM trips
WHERE
client_id not in (select users_id from users where banned = 'Yes')
AND
driver_id not in (select users_id from users where banned = 'Yes')
GROUP BY request_at;
输出为:
request_at | c | s | cancellation_rate
------------+---+---+-------------------
2013-10-01 | 3 | 1 | 0.00
2013-10-03 | 2 | 1 | 0.00
2013-10-02 | 2 | 0 | 0.00
当通过查看前面的列 (s/c) 很清楚它应该是 0.33,0.50, 0.00 时,cancellation_rate 是 0.00。
好消息是你只是错别字而已。
在您的示例中,您使用的是 cancelled_by_client or cancelled_by_client
round(sum(case when status='cancelled_by_client' or status='cancelled_by_client' then 1 else 0 end)/count(*),2) as Cancellation_rate
而不是:
.. when status='cancelled_by_driver' or status='cancelled_by_client' then ..
这会 return:
request_at | c | s | cancellation_rate |
---|---|---|---|
2013-10-01 | 3 | 1 | 0.33 |
2013-10-02 | 2 | 0 | 0.00 |
2013-10-03 | 2 | 1 | 0.50 |