SQL自联命名?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL自联命名?相关的知识,希望对你有一定的参考价值。
我对SQL有点新,所以请温柔......
我正在尝试创建一个[遗留] BigQuery SQL自联合,如:
SELECT * FROM (query 1), (query 1)
从另一个SO回答here找到的语法。但是查询1很长,我不想多次复制/粘贴整个查询。有没有办法命名查询1所以我不必再重复粘贴整个事情?
我正在尝试做类似以下的事情(关于A
的引用):
SELECT
*
FROM
(query 1) AS A,
(SELECT
Category as Hier_Level,
MAX(Department),
Category,
"VARIOUS" AS Subcategory,
SUM(DOLLARS)
FROM
A
GROUPBY
Category) AS B,
(SELECT
Department as Hier_Level,
Department,
"VARIOUS" AS Category,
"VARIOUS" AS Subcategory,
SUM(DOLLARS)
FROM
A
GROUPBY
Department,
Category) AS C,
...
Update
虽然问题集中在不重复查询1的代码,但聚合的最终目标是获取如下数据,这些数据由查询1输出:
Hier_Level Department Category Subcategory Dollars
Subcategory Electronics TV LCD 3500
Subcategory Electronics TV OLED 6000
Subcategory Electronics Phone iPhone 600
并返回相同的表,其中添加了来自groupbys的聚合的行。结果应如下所示:
Hier_Level Department Category Subcategory Dollars
Subcategory Electronics TV LCD 3500
Subcategory Electronics TV OLED 6000
Subcategory Electronics Phone iPhone 600
Category Electronics TV Various 9500
Category Electronics Phone iPhone 600
Department Electronics Various Various 10100
在标准SQL中,我的理解是这样的事情可以使用CTE和union all来完成,但在BQ Legacy SQL中都没有。
答案
不幸的是,我仅限于遗留SQL进行其他几项操作。
您可以创建View命名查询1 - 因此您将其引用为[project:dataset.query1] - 确保在Lagacy模式下创建它,以便您可以在传统模式下从查询中使用它
所以,现在你的查询将完全(几乎)按照你的要求 - I'm trying to do something like the following (with regards to the references to A)
SELECT * FROM
[project:dataset.query1],
(SELECT
'Category' AS Hier_Level,
MAX(Department) Department,
Category,
'Various' AS Subcategory,
SUM(Dollars) AS Dollars
FROM [project:dataset.query1]
GROUP BY Category),
(SELECT
'Department' AS Hier_Level,
Department,
'Various' AS Category,
'Various' AS Subcategory,
SUM(Dollars) AS Dollars
FROM [project:dataset.query1]
GROUP BY Department)
有你的例子 - 结果将如预期的那样
Row Hier_Level Department Category Subcategory Dollars
1 Subcategory Electronics TV LCD 3500
2 Subcategory Electronics TV OLED 6000
3 Subcategory Electronics Phone iPhone 600
4 Category Electronics TV Various 9500
5 Category Electronics Phone Various 600
6 Department Electronics Various Various 10100
另一答案
首先,使用标准SQL,而不是旧版SQL。
我想我会这样做:
select t.*
from (query) q cross join
(select 1 union all select 2 union all select 3);
cross join
将每行乘以第二个select
的次数。
编辑:
必须使用遗留SQL真的很难过。但这样的事情应该有效:
select q.*
from (<query>) q CROSS JOIN
(select n
from (SELECT 1 as n), (SELECT 2 as n), (SELECT 3 as n)
) n
以上是关于SQL自联命名?的主要内容,如果未能解决你的问题,请参考以下文章