聚合列返回零

时间:2016-08-19 19:15:31

标签: sql postgresql

我想汇总表apps

中每个应用的统计信息

我有以下查询,但由于某种原因,所有结果都会返回0

select
    a.id,
    'support' as domain,
    'summary' as type,
    90 as interval,
    json_build_object(
        'new', count(new),
        'closed', count(closed_c),
        'reply_rate', count(reply_rate),
        'median_response', max(median_response.response_time)
    ) as data
from apps a
full join (
    SELECT * from conversations c
    WHERE c.started_at::date > (current_date - (90  || ' days')::interval)::date
) as new on new.app_id = a.id
full join (
    SELECT * from conversations c
    WHERE c.closed_at::date > (current_date - (90  || ' days')::interval)::date
) as closed_c on closed_c.app_id = a.id
full join (
    SELECT * from conversations c
    WHERE c.started_at::date > (current_date - (90  || ' days')::interval)::date AND c.first_response_at is not null
) as reply_rate on reply_rate.app_id = a.id
full join (
    SELECT c.app_id, extract(epoch from (c.first_response_at - c.started_at)) as response_time, ntile(2) OVER (ORDER BY (c.first_response_at - c.started_at)) AS bucket FROM conversations c
    WHERE c.started_at::date > (current_date - (90  || ' days')::interval)::date AND c.first_response_at is not null
) as median_response on median_response.app_id = a.id
where a.test = false
group by a.id

2 个答案:

答案 0 :(得分:1)

我无法准确说明为什么一切都为零,但是

#1:full join应替换为left join(由于where a.test = false

#2:当您使用不同条件访问同一个表四次时,可能会被使用条件聚合的单个Select替换。

检查这是否返回正确的计数,然后将其加入apps

select
    app_id,
    sum(new),
    sum(closed_c),
    sum(reply_rate),
    max(case when bucket = 1 then response_time end)
from
 (
    SELECT app_id,

       1 as new,

       case when c.closed_at::date > (current_date - (90  || ' days')::interval)::date then 1 else 0 end as closed_c,

       case when c.first_response_at is not null then 1 else 0 end as reply_rate,

       extract(epoch from (c.first_response_at - c.started_at)) as response_time, 

       ntile(2) OVER (ORDER BY (c.first_response_at - c.started_at)) AS bucket
    FROM conversations c
    -- assuming that closed_at is always after started_at
    WHERE c.started_at::date > (current_date - (90  || ' days')::interval)::date 
 ) as dt
group by app_id

答案 1 :(得分:0)

如果不使用这些表中的单个列,为什么要进行这么多连接?

如果你只需要数数,你可以这样做:

select
    a.id,
    'support' as domain,
    'summary' as type,
    90 as interval,
    json_build_object(
        'new',              (SELECT count(*)                                                      from conversations c WHERE c.app_id = a.id and c.started_at::date  > current_date - 90),
        'closed',           (SELECT count(*)                                                      from conversations c WHERE c.app_id = a.id and c.closed_at::date   > current_date - 90),
        'reply_rate',       (SELECT count(*)                                                      from conversations c WHERE c.app_id = a.id and c.started_at::date  > current_date - 90 and c.first_response_at is not null),
        'median_response',  (SELECT max(extract(epoch from (c.first_response_at - c.started_at))) from conversations c WHERE c.app_id = a.id and c.started_at::date  > current_date - 90 and c.first_response_at is not null)
    ) as data
from apps a
where a.test = false

另外,为什么使用间隔来添加/减去date类型的天数?

你可以current_date - 90

我建议您在conversations中创建一些索引:

create index i_conversations_started_at on conversations (id, started_at::date); 
create index i_conversations_closed_at on conversations (id, closed_at::date);