BigQuery:多个查询 SQL 同一条语句中的查询
BigQuery: Multiple queries SQL queries in the same statement
我正在研究设备在不同温度下的功率和速度之间的关系。我可以轻松地为一个温度范围(例如 2C 到 5C)创建 SQL,现在我想看看我如何 运行 同一个查询,只是在一个查询中具有不同的温度范围。我希望能够通过数据连接器 运行 来自 Google 工作表的代码(该位工作正常)然后使用图表
我查看了各种联合,但我在这里找到的不适合我的用例
SELECT
Power,
AVG (Speed) AS Speed_freezing_2
FROM `gcp_project.dataset.table`
WHERE Power > 49 AND Power < 950 AND Temperature <= 2
GROUP BY Power ORDER BY Power
不知道图片是否适用于此处,但这是我想要的:https://imgur.com/a/zY5ICqA
您可以在同一个查询中进行多次平均,并在每个 AVG 内的 CASE 中包含过滤器。像这样:
SELECT
Power,
AVG (case when Temperature<=2 then Speed end) AS Speed_freezing_2
AVG (case when Temperature>2 and Temperature<=5 then Speed end) AS Speed_cold_2_5
.......
FROM `gcp_project.dataset.table`
WHERE Power > 49 AND Power < 950
GROUP BY Power
ORDER BY Power
以下是 BigQuery 标准的示例 SQL
#standardSQL
WITH ranges AS (
SELECT 0 start, 2 finish UNION ALL
SELECT 2, 5 UNION ALL
SELECT 5, 10 UNION ALL
SELECT 10, 20 UNION ALL
SELECT 20, 50
)
SELECT start, finish, Power, AVG (Speed) avg_speed
FROM ranges, `project.dataset.table`
WHERE Power > 49 AND Power < 950
AND Temperature > start AND Temperature <= finish
GROUP BY start, finish, Power
ORDER BY Power, start
我正在研究设备在不同温度下的功率和速度之间的关系。我可以轻松地为一个温度范围(例如 2C 到 5C)创建 SQL,现在我想看看我如何 运行 同一个查询,只是在一个查询中具有不同的温度范围。我希望能够通过数据连接器 运行 来自 Google 工作表的代码(该位工作正常)然后使用图表
我查看了各种联合,但我在这里找到的不适合我的用例
SELECT
Power,
AVG (Speed) AS Speed_freezing_2
FROM `gcp_project.dataset.table`
WHERE Power > 49 AND Power < 950 AND Temperature <= 2
GROUP BY Power ORDER BY Power
不知道图片是否适用于此处,但这是我想要的:https://imgur.com/a/zY5ICqA
您可以在同一个查询中进行多次平均,并在每个 AVG 内的 CASE 中包含过滤器。像这样:
SELECT
Power,
AVG (case when Temperature<=2 then Speed end) AS Speed_freezing_2
AVG (case when Temperature>2 and Temperature<=5 then Speed end) AS Speed_cold_2_5
.......
FROM `gcp_project.dataset.table`
WHERE Power > 49 AND Power < 950
GROUP BY Power
ORDER BY Power
以下是 BigQuery 标准的示例 SQL
#standardSQL
WITH ranges AS (
SELECT 0 start, 2 finish UNION ALL
SELECT 2, 5 UNION ALL
SELECT 5, 10 UNION ALL
SELECT 10, 20 UNION ALL
SELECT 20, 50
)
SELECT start, finish, Power, AVG (Speed) avg_speed
FROM ranges, `project.dataset.table`
WHERE Power > 49 AND Power < 950
AND Temperature > start AND Temperature <= finish
GROUP BY start, finish, Power
ORDER BY Power, start