由于JOIN问题导致计算错误

时间:2016-08-12 09:58:39

标签: sql tsql

表格

+----+-----------+-----------+---------+---------------------+------------+
| 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 |
+----+-----------+-----------+---------+---------------------+------------+

我的尝试 -

WITH src 
     AS (SELECT Count(status) AS Denom, 
                request_at 
         FROM   trips 
         WHERE  status = 'completed' 
         GROUP  BY request_at), 
     src2 
     AS (SELECT Count(status) AS Num, 
                request_at 
         FROM   trips 
         WHERE  status <> 'completed' 
         GROUP  BY request_at) 
SELECT Cast(Count(num) AS FLOAT)/Cast(Count(Denom) AS FLOAT) AS cancel_rate, 
       trips.request_at 
FROM   src, 
       src2, 
       trips 
GROUP  BY trips.request_at; 

我试图找到每天的取消率但是清除错误(我的输出) -

+-------------+------------+
| cancel_rate | request_at |
+-------------+------------+
|          24 | 2013-10-01 |
|          18 | 2013-10-02 |
|          18 | 2013-10-03 |
+-------------+------------+

2013-10-01的取消率应该 0.5 而不是24.同样的其他日期应该是不同的。

我知道问题在于这个部分,但我不知道什么是正确的方法或如何处理它

SELECT Cast(Count(num) AS FLOAT)/Cast(Count(Denom) AS FLOAT) AS cancel_rate, 
       trips.request_at 
FROM   src, 
       src2, 
       trips 

有没有办法在With NAME as ()子句中放入多个select语句?所以我不会使用任何JOIN或多个表。

2 个答案:

答案 0 :(得分:1)

使用条件聚合:

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;

请注意cancel_rate的计算。使用AVG()而不是除以两个值更简单。使用1.0是因为SQL Server执行整数运算,因此1 / 2为0而不是0.5。

答案 1 :(得分:0)

好的,有点晚了,但这是另一个变种(编辑):

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