如何联合两个以临时 table 逻辑 BigQuery SQL 开头的 table
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 table as
开头。现在用这个逻辑联合两个表?
我认为这很接近...需要更多关于问题性质的详细信息或重新创建它以隔离问题的能力...
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...
我有两个表需要合并。两者逻辑相同,只是源表不同。查询如下所示:
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 table as
开头。现在用这个逻辑联合两个表?
我认为这很接近...需要更多关于问题性质的详细信息或重新创建它以隔离问题的能力...
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...