我有一个查询请求,我必须为每个网站显示一个客户活动,但每个网站只需要一行,而不是每个活动多次显示一个客户。 以下是我尝试的查询,但会带来更多行。请帮助我,因为我可以避免重复,并且每个活动的每一行只显示一位客户。
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
答案 0 :(得分:0)
听起来您需要按customer_id进行分组,并为您选择的其他列执行聚合。例如:
sum(case when s.subscription_type = '5' then 1 else 0 end) as pb_subs_count
答案 1 :(得分:0)
您可以尝试以下两种方法之一:
使用GROUP BY语句组合具有相同ID的所有记录,例如
...
WHERE s.site_code ='PB' and s.subscription_end_date is null
GROUP BY i.customer_id
在SELECT中使用DISTINCT命令,例如
SELECT DISTINCT i.customer_id, i.SEGMENT, ...
答案 2 :(得分:0)
您可以在customer_id上使用聚合(SUM),但您希望在其他字段上发生什么?例如,如果您对同一客户(2行)有SUBSCRIPTION_TYPE 5和13,您想要哪个值?
答案 3 :(得分: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
但是,如果不了解所涉及的表格,我无法确定。