简体   繁体   English

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

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

I have two tables that I need to union.我有两个需要联合的表。 Both of them have the same logic, just different source tables.两者逻辑相同,只是源表不同。 Query looks like this:查询看起来像这样:

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

This query works perfectly fine, but when I add the same exact query below that is pulling data from table_2 and use UNION ALL I have the following error: Syntax error: Expected "(" or keyword SELECT but got keyword WITH So the queries can not start with with table as . Now to UNION two tables with this logic?这个查询工作得很好,但是当我在下面添加从table_2中提取数据的完全相同的查询并使用UNION ALL我有以下错误: Syntax error: Expected "(" or keyword SELECT but got keyword WITH所以查询不能从 with with table as开始。现在用这个逻辑 UNION 两个表?

I would think this would be close... Need more detail to the nature of your problem or an ability to recreate it to isolate the issue...我认为这会很接近......需要更多关于问题性质的细节或重新创建它以隔离问题的能力......

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