我正在研究不同温度下设备的功率和速度之间的关系。我可以轻松地针对一个温度范围(例如2C到5C)为此创建SQL,现在我想看看如何在一个查询中以不同的温度范围运行同一查询。我希望能够通过dataconnector在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
答案 0 :(得分:0)
您可以在同一查询中进行多次平均,并将过滤器包含在每个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
答案 1 :(得分:0)
以下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