PostgreSQL查询限制GROUP BY中的返回值

时间:2017-03-07 18:23:13

标签: postgresql group-by

以下查询不包括所有可用数据,特别是

line12 campaign.campaign_name AS "Campaign Name"

每个"群组名称"应该有4 - 6"广告系列名称"与请求一起返回。发生的事情是每组只有一个活动正在返回任何数据。

SELECT DISTINCT ON (call.call_id) 
   call.call_id AS "Call ID",
   call.call_started AS "Call Date/Time", 
   call.disposition AS "Disposition", 
   call.repeat_call AS "Repeat Call", 
   call.duration AS "Duration",
   call.source AS "Caller ID", 
   call.tracking AS "Tracking Number", 
   call.ring_to AS "Ring to Number", 
   org_unit.org_unit_name AS "Group Name", 
   org_unit.org_unit_parent_id AS "Parent ID",
   campaign.campaign_name AS "Campaign Name",
SUM(CASE WHEN indicator.indicator_name = 'Acquired Name' AND indicator_score.score_value > 0 THEN 1 ELSE 0  END) as "Acquired Name",
SUM(CASE WHEN indicator.indicator_name = 'Acquired Email' AND indicator_score.score_value > 0 THEN 1 ELSE 0  END) as "Acquired Email",
SUM(CASE WHEN indicator.indicator_name = 'Acquired Phone Number' AND indicator_score.score_value > 0 THEN 1 ELSE 0  END) as "Acquired Phone Number",
SUM(CASE WHEN indicator.indicator_name = 'Lead Score (c)' AND indicator_score.score_value > 0 THEN 1 ELSE 0 END) as "Lead Score (c)",
SUM(CASE WHEN indicator.indicator_name = 'Promotion Mention' AND indicator_score.score_value > 0 THEN 1 ELSE 0  END) as "Promotion Mention",
SUM(CASE WHEN indicator.indicator_name = 'Sales Inquiry' AND indicator_score.score_value > 0 THEN 1 ELSE 0  END) as "Sales Inquiry",
SUM(CASE WHEN indicator.indicator_name = 'Voice Message' AND indicator_score.score_value > 0 THEN 1 ELSE 0  END) as "Voice Message"
FROM call
JOIN indicator_score ON indicator_score.call_id = call.call_id
JOIN indicator ON indicator.indicator_id = indicator_score.indicator_id
JOIN org_unit ON org_unit.org_unit_id = call.org_unit_id
JOIN campaign ON campaign.campaign_ou_id = call.org_unit_id
WHERE org_unit.billing_id = 4097
AND DATE(call.call_started) BETWEEN '2017-03-01' and '2017-03-07' 
group by call.call_id, org_unit.org_unit_name, org_unit.org_unit_parent_id,
campaign.campaign_name
ORDER BY call.call_id

这是返回数据的电子表格,以及数据实际应该是什么样子。 Spreadsheet

我希望我能正确解释这个问题,但问题是每个群组只有一个广告系列正在填充,我需要填充所有可用的广告系列。

我尝试通过campaign.campaign_name"删除"组。并不断收到此错误:

  

/ *错误:列" campaign.campaign_name"必须出现在GROUP BY中   条款或用于集合函数LINE 12:
  campaign.campaign_name AS"广告系列名称",                   ^ * /

整个查询可能是垃圾,它由三个业余爱好者拼凑在一起,所以也可能是...

1 个答案:

答案 0 :(得分:0)

如果您发布此查询的结果,可能会有助于更好地理解您的关系:

SELECT c.call_id,
    string_agg(cm.campaign_name,' | ') as campaigns,
    string_agg(i.indicator_name,' | ') as indicators
FROM call c
JOIN indicator_score is ON is.call_id = c.call_id
JOIN indicator i ON i.indicator_id = is.indicator_id
JOIN org_unit o ON o.org_unit_id = c.org_unit_id
JOIN campaign cm ON cm.campaign_ou_id = c.org_unit_id
WHERE o.billing_id = 4097
    AND DATE(c.call_started) BETWEEN '2017-03-01' and '2017-03-07'
group by c.call_id order by c.call_id limit 10

猜猜,但这个版本可能适合你:

SELECT call.call_id AS "Call ID",
    call.call_started AS "Call Date/Time",
    call.disposition AS "Disposition",
    call.repeat_call AS "Repeat Call",
    call.duration AS "Duration",
    call.source AS "Caller ID",
    call.tracking AS "Tracking Number",
    call.ring_to AS "Ring to Number",
    org_unit.org_unit_name AS "Group Name",
    org_unit.org_unit_parent_id AS "Parent ID",
    string_agg(campaign.campaign_name,' | ') as "Campaign Names",
    SUM(CASE WHEN indicator.indicator_name = 'Acquired Name' AND indicator_score.score_value > 0 THEN 1 ELSE 0  END) as "Acquired Name",
    SUM(CASE WHEN indicator.indicator_name = 'Acquired Email' AND indicator_score.score_value > 0 THEN 1 ELSE 0  END) as "Acquired Email",
    SUM(CASE WHEN indicator.indicator_name = 'Acquired Phone Number' AND indicator_score.score_value > 0 THEN 1 ELSE 0  END) as "Acquired Phone Number",
    SUM(CASE WHEN indicator.indicator_name = 'Lead Score (c)' AND indicator_score.score_value > 0 THEN 1 ELSE 0 END) as "Lead Score (c)",
    SUM(CASE WHEN indicator.indicator_name = 'Promotion Mention' AND indicator_score.score_value > 0 THEN 1 ELSE 0  END) as "Promotion Mention",
    SUM(CASE WHEN indicator.indicator_name = 'Sales Inquiry' AND indicator_score.score_value > 0 THEN 1 ELSE 0  END) as "Sales Inquiry",
    SUM(CASE WHEN indicator.indicator_name = 'Voice Message' AND indicator_score.score_value > 0 THEN 1 ELSE 0  END) as "Voice Message"
FROM call
JOIN indicator_score ON indicator_score.call_id = call.call_id
JOIN indicator ON indicator.indicator_id = indicator_score.indicator_id
JOIN org_unit ON org_unit.org_unit_id = call.org_unit_id
JOIN campaign ON campaign.campaign_ou_id = call.org_unit_id
WHERE org_unit.billing_id = 4097
AND DATE(call.call_started) BETWEEN '2017-03-01' and '2017-03-07'
GROUP BY call.call_id, call.call_started, call.disposition, call.repeat_call, 
    call.duration, call.source, call.tracking, call.ring_to, 
    org_unit.org_unit_name, org_unit.org_unit_parent_id
ORDER BY call.call_id