2017-05-17 59 views
0

我正在使用BigQuery控制檯,並且需要從12個不同的數據集製作聯合,但信息相同,只更改de dataset_id,因爲日期範圍對所有人都是相同的。BigQuery中的UNION ALL或CONCATENATE數據集

我嘗試將union all函數放在第一個查詢的末尾,接着是另一個查詢,但不起作用。

Error: SELECT list expression references hits.contentgroup.contentgroup2 which is neither grouped nor aggregated at [2:3] 

這是查詢:

SELECT 
    hits.contentgroup.contentgroup2 CampaignGrouping, 
    custd.value member_PK, 
    'Web' Canal, 
    'ES' AS country_id, 
    SUM(hits.contentGroup.contentGroupUniqueViews2) VistasUnicas 
FROM 
    `id_project.11773102.ga_sessions*`, 
    UNNEST(customdimensions) custd, 
    UNNEST(hits) AS hits 
WHERE 
    1 = 1 
    AND PARSE_TIMESTAMP('%Y%m%d', REGEXP_EXTRACT(_table_suffix, r'.*_(.*)')) BETWEEN TIMESTAMP('2017-04-25') AND TIMESTAMP('2017-04-30') 
    AND custd.index=30 
    and hits.contentGroup.contentGroup2 <> '(not set)' 
    AND custd.value <> 'null' 
    AND hits.contentGroup.contentGroupUniqueViews2 IS NOT NULL 
UNION ALL 
SELECT 
    hits.contentgroup.contentgroup2 CampaignGrouping, 
    custd.value member_PK, 
    'Web' Canal, 
    'ES' AS country_id, 
    SUM(hits.contentGroup.contentGroupUniqueViews2) VistasUnicas 
FROM 
    `id_project.11773102.ga_sessions*`, 
    UNNEST(customdimensions) custd, 
    UNNEST(hits) AS hits 
WHERE 
    1 = 1 
    AND PARSE_TIMESTAMP('%Y%m%d', REGEXP_EXTRACT(_table_suffix, r'.*_(.*)')) BETWEEN TIMESTAMP('2017-04-25') AND TIMESTAMP('2017-04-30') 
    AND custd.index=30 
    and hits.contentGroup.contentGroup2 <> '(not set)' 
    AND custd.value <> 'null' 
    AND hits.contentGroup.contentGroupUniqueViews2 IS NOT NULL 
GROUP BY 
    1, 2 
ORDER BY 5 ASC 

感謝。

+1

什麼,如果有的話,錯誤你好嗎。解釋「不起作用」 - IOW,什麼不起作用。 –

+0

對不起@SloanThrasher我現在的問題包括在 –

+0

因此,錯誤消息似乎很清楚。 hits.contentgroup.contentgroup2從哪裏來? –

回答

1

需要使用GROUP BY與工會第一個查詢,如:

SELECT 
    hits.contentgroup.contentgroup2 CampaignGrouping, 
    custd.value member_PK, 
    'Web' Canal, 
    'ES' AS country_id, 
    SUM(hits.contentGroup.contentGroupUniqueViews2) VistasUnicas 
FROM 
    `bigquery-aaaaa-162814.11773102.ga_sessions*`, 
    UNNEST(customdimensions) custd, 
    UNNEST(hits) AS hits 
WHERE 
    1 = 1 
    AND PARSE_TIMESTAMP('%Y%m%d', REGEXP_EXTRACT(_table_suffix, r'.*_(.*)')) BETWEEN TIMESTAMP('2017-04-25') AND TIMESTAMP('2017-04-30') 
    AND custd.index=30 
    and hits.contentGroup.contentGroup2 <> '(not set)' 
    AND custd.value <> 'null' 
    AND hits.contentGroup.contentGroupUniqueViews2 IS NOT NULL 
GROUP BY 1, 2 
UNION ALL 
SELECT ... 

由於UNION ALLGROUP BY一個具體的例子:

#standardSQL 
WITH T AS (
    SELECT 1 AS x, 'foo' AS y UNION ALL 
    SELECT 1, 'bar' UNION ALL 
    SELECT 2, 'foo' 
) 
SELECT x, STRING_AGG(y, ',') AS y 
FROM T 
GROUP BY x 
UNION ALL 
SELECT SUM(x), y 
FROM T 
GROUP BY y; 
+0

RUN !!!非常感謝你,第一個代碼成功運行! :d –