繁体   English   中英

如何合并以临时表逻辑 BigQuery SQL 开头的两个表

[英]how to union two tables that starts with temporary table logic BigQuery SQL

我有两个需要联合的表。 两者逻辑相同,只是源表不同。 查询看起来像这样:

with origin_table as (
    SELECT
        date,
        (
            SELECT
                value
            FROM
                UNNEST(hits.customDimensions)
            WHERE
                INDEX = 10
        ) AS second_scroll,
        (
            SELECT
                value
            FROM
                UNNEST(hits.customDimensions)
            WHERE
                INDEX = 11
        ) AS dwell
    FROM
        (
            SELECT
                date,
                hits,
            FROM
                `table_1`,
                UNNEST(hits) AS hits
        )
    GROUP BY
        1
    select
        date,
        case
            when second_scroll is not null
            AND dwell is not null then 1
            when second_scroll is null
            AND dwell is not null then 0
            when second_scroll is not null
            AND dwell is null then 0
        end as ENGAGEMENT
    from
        origin_table

这个查询工作得很好,但是当我在下面添加从table_2中提取数据的完全相同的查询并使用UNION ALL我有以下错误: Syntax error: Expected "(" or keyword SELECT but got keyword WITH所以查询不能从 with with table as开始。现在用这个逻辑 UNION 两个表?

我认为这会很接近......需要更多关于问题性质的细节或重新创建它以隔离问题的能力......

with origin_table as (
    SELECT
        date,
        (
            SELECT
                value
            FROM
                UNNEST(hits.customDimensions)
            WHERE
                INDEX = 10
        ) AS second_scroll,
        (
            SELECT
                value
            FROM
                UNNEST(hits.customDimensions)
            WHERE
                INDEX = 11
        ) AS dwell
    FROM
        (
            SELECT
                date,
                hits,
            FROM
                `table_1`,
                UNNEST(hits) AS hits
        )
    GROUP BY
        1),  --added the comma for 2nd cte.

 origin_table2 as ( --Begin 2nd CTE
    SELECT
        date,
        (
            SELECT
                value
            FROM
                UNNEST(hits.customDimensions)
            WHERE
                INDEX = 10
        ) AS second_scroll,
        (
            SELECT
                value
            FROM
                UNNEST(hits.customDimensions)
            WHERE
                INDEX = 11
        ) AS dwell
    FROM
        (
            SELECT
                date,
                hits,
            FROM
                `table_2`,  --changed to table 2
                UNNEST(hits) AS hits
        )
    GROUP BY
        1)

    select
        date,
        case
            when second_scroll is not null
            AND dwell is not null then 1
            when second_scroll is null
            AND dwell is not null then 0
            when second_scroll is not null
            AND dwell is null then 0
        end as ENGAGEMENT
    from
        origin_table
    UNION ALL  --here's the union 
    select
        date,
        case
            when second_scroll is not null
            AND dwell is not null then 1
            when second_scroll is null
            AND dwell is not null then 0
            when second_scroll is not null
            AND dwell is null then 0
        end as ENGAGEMENT
    from
        origin_table2 --and selecting from 2nd CTE to union...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM