简体   繁体   English

在同一查询中具有不同条件的Postgres计数

[英]Postgres Count with different condition on the same query

I'm working on a report which has this following schema: http://sqlfiddle.com/#!15/fd104/2 我正在处理具有以下架构的报告: http : //sqlfiddle.com/#!15/fd104/2

The current query is working fine which looks like this: 当前查询工作正常,如下所示:

在此处输入图片说明

Basically it is a 3 table inner join. 基本上它是一个3表内部联接。 I did not make this query but the developer who left it and I want to modify the query. 我没有进行此查询,但是留下查询的开发人员想修改查询。 As you can see, TotalApplication just counts the total application based on the a.agent_id . 如您所见, TotalApplication仅基于TotalApplication计算总应用程序a.agent_id And you can see the totalapplication column in the result. 您可以在结果中看到totalapplication列。 What I want is to remove that and change the totalapplication to a new two column. 我要删除的是将totalapplication更改为新的两列。 I want to add a completedsurvey and partitalsurvey column. 我想添加一个completedsurveypartitalsurvey列。 So basically this part will become 所以基本上这部分将成为

SELECT a.agent_id as agent_id, COUNT(a.id) as CompletedSurvey
FROM forms a WHERE  a.created_at >= '2015-08-01' AND 
a.created_at <= '2015-08-31' AND disposition = 'Completed Survey'
GROUP BY a.agent_id

I just added AND disposition = 'Completed Survey' But I need another column for partialsurvey which has the same query with completedsurvey being the only difference is 我刚刚添加了AND disposition = 'Completed Survey'但我需要另一列用于partialsurvey AND disposition = 'Completed Survey'该列具有与completedsurvey相同的查询,唯一的区别是

AND disposition = 'Partial Survey'

and

COUNT(a.id) as PartialSurvey

But I dunno where to put that query or how will be the query look like.So the final output has these columns 但是我不知道将查询放在哪里或查询的样子,所以最终输出包含这些列

agent_id, name, completedsurvey, partialsurvey, loginhours, applicationperhour, rph

Once it is ok then applicationperhour and rph I can fix it myself 一切正常之后,可以申请每小时和RPH,我可以自己修复

If I understand you correctly, you are looking for a filtered (conditional) aggregate: 如果我对您的理解正确,那么您正在寻找经过过滤的(有条件的)集合:

SELECT a.agent_id as agent_id, 
       COUNT(a.id) filter (where disposition = 'Completed Survey') as CompletedSurvey, 
       count(a.id) filter (where disposition = 'Partial Survey') as partial_survey
FROM forms a 
WHERE a.created_at >= '2015-08-01' 
  AND a.created_at <= '2015-08-31' 
GROUP BY a.agent_id;

The above assumes the current version of Postgres (which is 9.4 at the time of writing). 上面假设Postgres的当前版本(在撰写本文时为9.4)。 For older versions (< 9.4) you need to use a case statement as the filter condition is not supported there: 对于较旧的版本(<9.4),您需要使用一个case语句,因为此处不支持filter条件:

SELECT a.agent_id as agent_id, 
       COUNT(case when disposition = 'Completed Survey' then a.id end) as CompletedSurvey, 
       COUNT(case when disposition = 'Partial Survey' then a.id end) as partial_survey
FROM forms a 
WHERE a.created_at >= '2015-08-01' 
  AND a.created_at <= '2015-08-31' 
GROUP BY a.agent_id;

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

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