数据:
+-------+------------+-----------------------+
| value | metric | timestamp |
+-------+------------+-----------------------+
| 30 | tempA | 2019-01-12T01:00:00 |
| 28 | tempA | 2019-02-12T02:00:00 |
| 25 | tempB | 2019-03-12T03:00:00 |
| 60 | humidityA | 2019-04-12T04:00:00 |
| 15 | tempC | 2019-05-15T01:00:00 |
+-------+------------+-----------------------+
我想获取某些温度指标的最新已知值。我想出了:
SELECT
metric,
value,
timestamp,
seqnum
FROM (
SELECT
metric,
value,
timestamp,
ROW_NUMBER() OVER (PARTITION BY metric ORDER BY timestamp DESC) AS seqnum
FROM
`project.dataset.table`
WHERE
metric IN ('tempA', 'tempB') )
WHERE
seqnum = 1
这会对整个表进行分区,这可能需要一些时间。是否有更高效/高效的方法来获取每个指标的最后一行?
答案 0 :(得分:2)
数组聚合可以更快,因为由于LIMIT 1,它只需要将第一行保留在内存中即可。
SELECT
metric,
ARRAY_AGG(
STRUCT(value, timestamp)
ORDER BY timestamp DESC LIMIT 1
)[OFFSET(0)].*
FROM
`project.dataset.table`
WHERE
metric IN ('tempA', 'tempB')
GROUP BY metric