![](/img/trans.png)
[英]Get SQL Server query to return zero records instead a single null record for aggregated no group by?
[英]Aggregated columns return zero
我想匯總表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
我無法確切地說出為什么一切都為零,但是
#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
當您不使用那些表中的單個列時,為什么要進行這么多的聯接?
如果您只需要計算,就可以這樣做:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.