查询 phone 条记录

Querying phone records

我想用SQL来分析我拥有的这个通话数据。我想回答的主要问题是:如果来电者拨打一个号码并且第一次没有得到响应,some_factor(布尔列)的值是否会影响来电者在重拨时获得响应的概率以后是同一个号码吗?

这是 table 中的列:

| id | phone_number | callee_name | caller_id | call_duration | some_factor | created_at          |
|----|--------------|-------------|-----------|---------------|-------------|---------------------|
| 1  | 123-456-7890 | John Smith  | 42        |               | t           | 2022-03-07 09:40:40 |

这是我经过一番思考后得出的结论,但我对 SQL 查询相对缺乏经验。如果它们有效,我可以查看查询 1 的计数与查询 2 的计数的比率,然后重做 some_factor 为假的查询。

-- 1. successful retried calls: the callee picked up
SELECT COUNT(*)
FROM phone_calls
WHERE phone_number IN (
    SELECT phone_number
    FROM phone_calls
    WHERE call_duration IS NULL
    AND some_factor
)
AND call_duration IS NOT NULL

-- 2. unsuccessful retried calls: the callee did not pick up
SELECT COUNT(*)
FROM phone_calls
WHERE phone_number IN (
    SELECT phone_number
    FROM phone_calls
    WHERE call_duration IS NULL
    AND some_factor
)
AND call_duration IS NULL

-- then, do math with the results of these queries to get success rate when some_factor is true
-- then, redo but with AND NOT some_factor to get success rate when some_factor is false

但是,我意识到 2 正在将子查询中的条目计入总计数。我希望 COUNT 不包括某人第一次拨打号码的时间,如果他们没有接听的话。也许如果我可以排除与子查询中的条目相对应的 id 条目,那将起作用。但我不知道该怎么做。我错过了什么吗?

据我所知,在第二个查询中您对子查询本身返回的结果不感兴趣。因此,计数会因子查询返回的不同 phone 数字的数量而变大。你只需要减去那个数字:

SELECT COUNT(*) - COUNT(DISTINCT phone_number)
FROM phone_calls
WHERE phone_number IN (
    SELECT phone_number
    FROM phone_calls
    WHERE call_duration IS NULL
    AND some_factor
)
AND call_duration IS NULL