连接两个表后立即查询结果一半

时间:2018-06-27 20:13:21

标签: sql postgresql amazon-redshift

以下查询仅对内部查询(post_engagement,网站购买)产生正确的结果,而所有其他数字均错误地成倍增加。有任何想法吗?谢谢。

两个表的架构:

收藏夹广告(id,campaign_id,campaign_name,目标,展示次数,支出)

操作(id,ads_id,action_type,值)

SELECT
  f.campaign_id,
  f.campaign_name,
  f.objective,
  SUM(f.impressions) AS Impressions,
  SUM(f.spend) AS Spend,
  SUM(a.post_engagement) AS "Post Engagement",
  SUM(a.website_purchases) AS "Website Purchases"
FROM
favorite_ads f
LEFT JOIN (
     SELECT
          ads_id,
          CASE WHEN action_type = 'post_engagement' THEN SUM(value) END AS 
post_engagement,
      CASE WHEN action_type = 'offsite_conversion.fb_pixel_purchase' THEN SUM(value) END AS website_purchases
 FROM Actions a
 GROUP BY ads_id, action_type
) a ON f.id = a.ads_id
WHERE date_trunc('month',f.date_start) = '2018-04-01 00:00:00' AND 
date_trunc('month',f.date_stop) = '2018-04-01 00:00:00' --only get campaigns 
that ran in April, 2018
GROUP BY f.campaign_id, campaign_name, objective
Order by campaign_id

1 个答案:

答案 0 :(得分:1)

在不知道实际的表结构,约束,依赖性和数据的情况下,很难说出问题出在哪里。

您的评论中已经有一些线索,您应该首先考虑。

例如,您编写的该子查询返回了正确的结果:

SELECT ads_id,
       CASE
         WHEN action_type = 'post_engagement'
         THEN SUM(value)
       END AS post_engagement,
       CASE
         WHEN action_type = 'offsite_conversion.fb_pixel_purchase'
         THEN SUM(value)
       END AS website_purchases
  FROM Actions a
 GROUP BY ads_id, action_type

这也能给出正确的结果吗?

SELECT ads_id,
       SUM(
         CASE
           WHEN action_type = 'post_engagement'
           THEN value
         END
       ) AS post_engagement,
       SUM(
         CASE
           WHEN action_type = 'offsite_conversion.fb_pixel_purchase'
           THEN value
         END
       ) AS website_purchases
  FROM Actions
 GROUP BY ads_id

如果是这样,请尝试用该子查询替换子查询。

如果您仍然遇到问题,那么我将调查您的加入条件是否正确(看起来似乎),对于一个广告系列(campaign_id),您可能会有多个具有相同{{1 }},它将乘以子查询结果-具体取决于id中的主键(或唯一约束)。