简体   繁体   English

汇总列返回零

[英]Aggregated columns return zero

I want to aggregate statistics for each app in the table apps 我想汇总表apps程序中每个应用程序的统计信息

I have the following query, but for some reason all the results return 0 我有以下查询,但由于某种原因,所有结果均返回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

I can't tell exactly why everything is zero, but 我无法确切地说出为什么一切都为零,但是

#1: full join should be replaced by left join (due to the where a.test = false ) #1: full join应替换为left join (由于where a.test = false

#2: as you access the same table four times with different conditions this can be probably replaced by a single Select using conditional aggregation. #2:当您在不同条件下访问同一张表四次时,可以使用条件聚合将其替换为单个Select。

Check if this returns the correct counts and then Left Join it to apps . 检查是否返回正确的计数,然后将其左加入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

Why are you doing so many joins when you don't use not a single column from those tables? 当您不使用那些表中的单个列时,为什么要进行这么多的联接?

If you just need to count, you can do just like that: 如果您只需要计算,就可以这样做:

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

Also, why are you using interval to add/subtract days into a date type? 另外,为什么要使用时间间隔将天数加/减为date类型?

You could just do current_date - 90 . 您可以只执行current_date - 90

I recommend you create some indexes in conversations too: 我建议您也在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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM