运行 Google Bigquery 中不同 GROUP_CONCAT 的语法
Syntax to run a distinct GROUP_CONCAT in Google Bigquery
我有这个问题:
SELECT campaign.id AS campaign_id,
GROUP_CONCAT(utm.campaign) AS utm_campaign
FROM [email_event]
WHERE (TIMESTAMP BETWEEN SEC_TO_TIMESTAMP(1412136000) AND SEC_TO_TIMESTAMP(1414814340))
GROUP BY campaign_id;
而且我很想 运行 一个不同的 GROUP_CONCAT,因为现在相同的条目在输出中重复。
更新
我已将您的解决方案扩展到此:
SELECT campaign.id AS campaign_id,
GROUP_CONCAT(utm.campaign) AS utm_campaign,
GROUP_CONCAT(utm.content) AS utm_content
FROM
(SELECT *
FROM
(SELECT 507 AS campaign.id,
'remarketingemail' AS utm.campaign,
'newsletter_feb' AS utm.content),
(SELECT 508 AS campaign.id,
'remarketingemail' AS utm.campaign,
'newsletter_jan' AS utm.content),
(SELECT 508 AS campaign.id,
'remarketingemail' AS utm.campaign,
'newsletter_feb' AS utm.content),
(SELECT 508 AS campaign.id,
'adwordscamp' AS utm.campaign,
'cyber_monday' AS utm.content) )
GROUP BY campaign_id;
但现在我得到了 utm_campaign 的重复值。
+-----+------------------------------------------+--------------------------------------+
| 507 | remarketingemail | newsletter_feb |
| 508 | remarketingemail,remarketingemail,adw... | newsletter_jan,newsletter_feb,cyb... |
+-----+------------------------------------------+--------------------------------------+
这是子查询的原始输出,在 qroup by
之前
+-----+-----------------------------------+-------------------------------+
| 507 | remarketingemail | newsletter_feb |
| 508 | remarketingemail | newsletter_jan |
| 508 | remarketingemail | newsletter_feb |
| 508 | adwordscamp | cyber_monday |
+-----+-----------------------------------+-------------------------------+
使用子查询进行分组并获取不同的值。像这样:
SELECT campaign.id AS campaign_id,
GROUP_CONCAT(utm.campaign) AS utm_campaign
FROM
(Select campaign.id,utm.campaign
FROM [email_event]
WHERE (TIMESTAMP BETWEEN SEC_TO_TIMESTAMP(1412136000) AND SEC_TO_TIMESTAMP(1414814340))
GROUP EACH BY campaign.id,utm.campaign)
GROUP BY campaign_id;
少数聚合字段的另一种选择是分阶段进行...
SELECT campaign_id ,
GROUP_CONCAT(utm_campaign) as utm_campaign,
utm_content
From
(SELECT campaign.id AS campaign_id,
utm.campaign as utm_campaign,
GROUP_CONCAT(utm.content) AS utm_content
FROM
(
SELECT *
FROM
( SELECT 507 AS campaign.id,
'remarketingemail' AS utm.campaign,
'newsletter_feb' AS utm.content),
( SELECT 507 AS campaign.id,
'remarketingemail2' AS utm.campaign,
'newsletter_feb' AS utm.content),
(SELECT 508 AS campaign.id,
'remarketingemail' AS utm.campaign,
'newsletter_jan' AS utm.content),
(SELECT 508 AS campaign.id,
'remarketingemail' AS utm.campaign,
'newsletter_feb' AS utm.content)
)
GROUP BY utm_campaign,campaign_id)
GROUP BY utm_content,campaign_id
;
我有这个问题:
SELECT campaign.id AS campaign_id,
GROUP_CONCAT(utm.campaign) AS utm_campaign
FROM [email_event]
WHERE (TIMESTAMP BETWEEN SEC_TO_TIMESTAMP(1412136000) AND SEC_TO_TIMESTAMP(1414814340))
GROUP BY campaign_id;
而且我很想 运行 一个不同的 GROUP_CONCAT,因为现在相同的条目在输出中重复。
更新
我已将您的解决方案扩展到此:
SELECT campaign.id AS campaign_id,
GROUP_CONCAT(utm.campaign) AS utm_campaign,
GROUP_CONCAT(utm.content) AS utm_content
FROM
(SELECT *
FROM
(SELECT 507 AS campaign.id,
'remarketingemail' AS utm.campaign,
'newsletter_feb' AS utm.content),
(SELECT 508 AS campaign.id,
'remarketingemail' AS utm.campaign,
'newsletter_jan' AS utm.content),
(SELECT 508 AS campaign.id,
'remarketingemail' AS utm.campaign,
'newsletter_feb' AS utm.content),
(SELECT 508 AS campaign.id,
'adwordscamp' AS utm.campaign,
'cyber_monday' AS utm.content) )
GROUP BY campaign_id;
但现在我得到了 utm_campaign 的重复值。
+-----+------------------------------------------+--------------------------------------+
| 507 | remarketingemail | newsletter_feb |
| 508 | remarketingemail,remarketingemail,adw... | newsletter_jan,newsletter_feb,cyb... |
+-----+------------------------------------------+--------------------------------------+
这是子查询的原始输出,在 qroup by
之前+-----+-----------------------------------+-------------------------------+
| 507 | remarketingemail | newsletter_feb |
| 508 | remarketingemail | newsletter_jan |
| 508 | remarketingemail | newsletter_feb |
| 508 | adwordscamp | cyber_monday |
+-----+-----------------------------------+-------------------------------+
使用子查询进行分组并获取不同的值。像这样:
SELECT campaign.id AS campaign_id,
GROUP_CONCAT(utm.campaign) AS utm_campaign
FROM
(Select campaign.id,utm.campaign
FROM [email_event]
WHERE (TIMESTAMP BETWEEN SEC_TO_TIMESTAMP(1412136000) AND SEC_TO_TIMESTAMP(1414814340))
GROUP EACH BY campaign.id,utm.campaign)
GROUP BY campaign_id;
少数聚合字段的另一种选择是分阶段进行...
SELECT campaign_id ,
GROUP_CONCAT(utm_campaign) as utm_campaign,
utm_content
From
(SELECT campaign.id AS campaign_id,
utm.campaign as utm_campaign,
GROUP_CONCAT(utm.content) AS utm_content
FROM
(
SELECT *
FROM
( SELECT 507 AS campaign.id,
'remarketingemail' AS utm.campaign,
'newsletter_feb' AS utm.content),
( SELECT 507 AS campaign.id,
'remarketingemail2' AS utm.campaign,
'newsletter_feb' AS utm.content),
(SELECT 508 AS campaign.id,
'remarketingemail' AS utm.campaign,
'newsletter_jan' AS utm.content),
(SELECT 508 AS campaign.id,
'remarketingemail' AS utm.campaign,
'newsletter_feb' AS utm.content)
)
GROUP BY utm_campaign,campaign_id)
GROUP BY utm_content,campaign_id
;