0

我有一个查询请求,我必须为每个网站显示一个客户活动,但每个网站必须只有一行,而不是一个客户为每个活动显示多次。以下是我尝试过的查询,但带来了更多行。请帮助我如何避免重复,并为每个活动的每一行只显示一个客户。

SELECT i.customer_id, i.SEGMENT  AS Pistachio_segment,
    (CASE when S.SUBSCRIPTION_TYPE = '5' then 'Y' else  'N' end ) PB_SUBS
    (CASE WHEN S.SUBSCRIPTION_TYPE ='12' THEN 'Y' ELSE 'N' END) Daily_test,
    (CASE when S.SUBSCRIPTION_TYPE ='8' then 'Y' else 'N' end) COOK_4_2
FROM IDEN_WITH_MAIL_ID i  JOIN CUSTOMER_SUBSCRIPTION_FCT S
ON I.IDENTITY_ID = S.IDENTITY_ID and I.CUSTOMER_ID = S.CUSTOMER_ID
WHERE s.site_code ='PB' and s.subscription_end_date is null
4

4 回答 4

0

您可以在 customer_id 上使用聚合 (SUM),但是您希望在其他字段上发生什么?例如,如果您对同一客户(2 行)有 SUBSCRIPTION_TYPE 5 和 13,您想要哪个值?

于 2012-04-06T08:34:59.867 回答
0

也许您正在寻找这样的东西:

SELECT i.customer_id, i.SEGMENT  AS Pistachio_segment,
    MAX(CASE when S.SUBSCRIPTION_TYPE = '5' then 'Y' else  'N' end ) PB_SUBS
    MAX(CASE WHEN S.SUBSCRIPTION_TYPE ='12' THEN 'Y' ELSE 'N' END) Daily_test,
    MAX(CASE when S.SUBSCRIPTION_TYPE ='8' then 'Y' else 'N' end) COOK_4_2
FROM IDEN_WITH_MAIL_ID i  JOIN CUSTOMER_SUBSCRIPTION_FCT S
ON I.IDENTITY_ID = S.IDENTITY_ID and I.CUSTOMER_ID = S.CUSTOMER_ID
WHERE s.site_code ='PB' and s.subscription_end_date is null
GROUP BY i.customer_id, i.SEGMENT

不过,如果不了解所涉及的表的更多信息,我无法确定。

于 2012-04-06T10:46:31.907 回答
0

听起来您需要按 customer_id 分组并为您选择的其他列执行聚合。例如:

sum(case when s.subscription_type = '5' then 1 else 0 end) as pb_subs_count
于 2012-04-06T05:00:47.423 回答
0

您可以尝试以下两种方法之一:

  1. 使用 GROUP BY 语句来组合具有相同 id 的所有记录,例如,

    ...
    WHERE s.site_code ='PB' and s.subscription_end_date is null
    GROUP BY i.customer_id
    
  2. 在 SELECT 中使用 DISTINCT 命令,例如,

    SELECT DISTINCT i.customer_id, i.SEGMENT, ...
    
于 2012-04-06T05:02:16.513 回答