在 SQL 语言中(AWS timestream)- 如何获取未按列分组的最新值? (最新 - 意思是最近的一行)
In SQL language (AWS timestream) - how to get the latest value of non-grouped by column? (latest - meaning the most recent row)
我的目标 - 是按特定字段对我的数据进行分组,select 也是该组中另一个字段的最新值。
我有以下 table:
| type | count | action | time |
a 5 put 2021-04-21 15:13:02.104308224
b 7 put2 2021-04-20 15:13:02.104308224
b 1 get 2021-04-19 15:13:02.104308224
a 4 put6 2021-04-18 15:13:02.104308224
c 5 get 2021-04-17 15:13:02.104308224
a 6 put 2021-04-17 15:13:02.104308224
我的声明如下:
SELECT
type,
SUM(count) as total,
FROM mydb.mytable
WHERE time between ago(1h) and now()
GROUP type
我最终会得到以下几行:
| type | total
a 15
b 8
c 5
问题是,我需要向每一行(已分组)添加最 最近 行的 action -所以应该是
| type | total | action
a 15 put
b 8 put2
c 5 get
如何实现?谢谢!
如果它支持 window 功能:
select
type,
lastaction,
SUM(count) as total
from (
SELECT *,
first_value(action) over (partition by type order by time desc) lastaction
FROM mydb.mytable
WHERE time between ago(1h) and now()
)
GROUP BY type, lastaction
我的目标 - 是按特定字段对我的数据进行分组,select 也是该组中另一个字段的最新值。
我有以下 table:
| type | count | action | time |
a 5 put 2021-04-21 15:13:02.104308224
b 7 put2 2021-04-20 15:13:02.104308224
b 1 get 2021-04-19 15:13:02.104308224
a 4 put6 2021-04-18 15:13:02.104308224
c 5 get 2021-04-17 15:13:02.104308224
a 6 put 2021-04-17 15:13:02.104308224
我的声明如下:
SELECT
type,
SUM(count) as total,
FROM mydb.mytable
WHERE time between ago(1h) and now()
GROUP type
我最终会得到以下几行:
| type | total
a 15
b 8
c 5
问题是,我需要向每一行(已分组)添加最 最近 行的 action -所以应该是
| type | total | action
a 15 put
b 8 put2
c 5 get
如何实现?谢谢!
如果它支持 window 功能:
select
type,
lastaction,
SUM(count) as total
from (
SELECT *,
first_value(action) over (partition by type order by time desc) lastaction
FROM mydb.mytable
WHERE time between ago(1h) and now()
)
GROUP BY type, lastaction