有什么问题?
我的子查询返回NULL。
我想做什么?
我正在使用如下表格:
------------------------------------------------------------------
| url | page_path_1 | page_path_2 | filter |
------------------------------------------------------------------
| e.com/test1/test2/ | test1 | test2 | foo |
| e.com/test1/test2/ | test1 | test2 | bar |
| e.com/test2/test3/ | test2 | test3 | foo |
我想为按降序foo排序的前20个目录组合中的每一个返回20个示例网址。
我目前的查询是什么?
SELECT
url
FROM
[table.data_analysis],
(
SELECT
page_path_1 as pp1, page_path_2 as pp2, count(page_path_1) as count, filter
FROM
[table.data_analysis]
WHERE
filter = foo
GROUP BY pp1, pp2, filter
ORDER BY count desc
LIMIT 20
) AS sub_query
WHERE
filter = foo and
page_path_1 = pp1 and
page_path_2 = pp2
LIMIT 20
如果你自己运行它, sub_query
会返回有效的目录:
Row | pp1 | pp2 | count | filter |
-----------------------------------------------
1 | test1 | test2 | 1 | foo
1 | test2 | test3 | 1 | foo
但是当你将它用作实际的子查询时,请看一下pp1& PP2:
SELECT
pp1, pp2
FROM
[table.data_analysis],
(
SELECT
page_path_1 as pp1, page_path_2 as pp2, count(page_path_1) as count, filter
FROM
[table.data_analysis]
WHERE
filter = foo
GROUP BY pp1, pp2, filter
ORDER BY count desc
LIMIT 20
) AS sub_query
WHERE
filter = foo
LIMIT 20
它返回null。
Row | pp1 | pp2
----------------------
1 | null | null
2 | null | null
3 | null | null
我完全难过了。我怎么错误地使用这个?
答案 0 :(得分:1)
我怎么错误地使用它?
希望下面的简化示例可以让您了解这里的错误
SELECT filter, pp1, pp2
FROM (
SELECT a, b, filter
FROM
(SELECT 1 AS a, 21 AS b, 'foo' AS filter),
(SELECT 2 AS a, 22 AS b, 'foo' AS filter),
(SELECT 3 AS a, 23 AS b, 'foo1' AS filter),
(SELECT 4 AS a, 24 AS b, 'foo1' AS filter),
(SELECT 5 AS a, 25 AS b, 'foo' AS filter)
), (
SELECT pp1, pp2, filter
FROM
(SELECT 1 AS pp1, 21 AS pp2, 'foo' AS filter),
(SELECT 2 AS pp1, 22 AS pp2, 'foo1' AS filter),
(SELECT 3 AS pp1, 23 AS pp2, 'foo' AS filter),
(SELECT 4 AS pp1, 24 AS pp2, 'foo' AS filter),
)
WHERE filter = 'foo'
LIMIT 2
结果是
filter pp1 pp2
foo null null
foo null null
在这里,您使用的是UNION ALL BigQuery Legacy SQL的逗号样式 UNION中的第一篇文章没有pp1和pp2,这就是为什么它们带有NULL