[英]HOW TO RETURN THE MOST RECENT VALUE?
timestamp![]() |
id ![]() |
type![]() |
---|---|---|
2021-06-01T00:00:00 ![]() |
ID1 ![]() |
LOL![]() |
2021-06-01T00:00:01 ![]() |
ID2 ![]() |
DOTA![]() |
2021-06-01T00:00:02 ![]() |
ID2 ![]() |
DOTA![]() |
2020-06-02T00:00:00 ![]() |
ID5 ![]() |
COD![]() |
2020-06-02T00:00:01 ![]() |
ID7 ![]() |
VALO![]() |
2020-06-02T00:00:02 ![]() |
ID8 ![]() |
VALO![]() |
2020-06-02T00:00:03 ![]() |
ID3 ![]() |
DOTA![]() |
2020-06-03T00:00:00 ![]() |
ID1 ![]() |
DOTA![]() |
2020-06-03T00:00:01 ![]() |
ID1 ![]() |
DOTA![]() |
2020-06-03T00:00:02 ![]() |
ID2 ![]() |
DOTA![]() |
2020-06-03T00:00:03 ![]() |
ID3 ![]() |
DOTA![]() |
2020-06-03T00:00:04 ![]() |
ID4 ![]() |
LOL![]() |
I am trying to get a result for all DISTINCT IDS with the most recent type.我正在尝试获取所有具有最新类型的 DISTINCT IDS 的结果。
Using this query使用此查询
SELECT DISTINCT id, type
FROM table1
ORDER BY 1;
I got this result:我得到了这个结果:
id ![]() |
type![]() |
---|---|
ID1 ![]() |
DOTA![]() |
ID1 ![]() |
LOL![]() |
ID2 ![]() |
DOTA![]() |
ID3 ![]() |
DOTA![]() |
ID4 ![]() |
LOL![]() |
ID5 ![]() |
COD![]() |
ID7 ![]() |
VALO![]() |
ID8 ![]() |
VALO![]() |
I understand that the row 1 and row 2 have distinct values.我知道第 1 行和第 2 行具有不同的值。 I tried querying this to check if it will only return a value with the most recent date.
我尝试查询它以检查它是否只会返回最近日期的值。 It didn't.
它没有。
SELECT DISTINCT id, type, MAX(timestamp) date
FROM table1
GROUP BY 1, 2
ORDER BY 1;
id ![]() |
type![]() |
date![]() |
---|---|---|
ID1 ![]() |
DOTA![]() |
2020-06-03 00:00:01 ![]() |
ID1 ![]() |
LOL![]() |
2021-06-01 00:00:00 ![]() |
ID2 ![]() |
DOTA![]() |
2021-06-01 00:00:02 ![]() |
ID3 ![]() |
DOTA![]() |
2020-06-03 00:00:03 ![]() |
ID4 ![]() |
LOL![]() |
2020-06-03 00:00:04 ![]() |
ID5 ![]() |
COD![]() |
2020-06-02 00:00:00 ![]() |
ID7 ![]() |
VALO![]() |
2020-06-02 00:00:01 ![]() |
ID8 ![]() |
VALO![]() |
2020-06-02 00:00:02 ![]() |
Tried below query just to check.尝试以下查询只是为了检查。 Returned the same.
回来一样。
SELECT DISTINCT id, type, (SELECT MAX(timestamp) FROM table1 as b where b.timestamp = a.timestamp )
FROM table1 as a
GROUP BY 1, 2
ORDER BY 1;
Is my intended result possible?我的预期结果可能吗?
A simple method uses qualify
:一个简单的方法使用
qualify
:
SELECT a.*
FROM chinook.new_table as a
WHERE true
QUALIFY ROW_NUMBER() OVER (PARTITION BY id, type ORDER BY date DESC) = 1;
You can also express this easily using GROUP BY
:您也可以使用
GROUP BY
轻松表达这一点:
SELECT ARRAY_AGG(a ORDER BY date DESC LIMIT 1).*
FROM chinook.new_table a
GROUP BY id, type;
Consider below options考虑以下选项
select as value array_agg(t order by timestamp desc limit 1)[offset(0)]
from `project.dataset.table1` t
group by id
or或者
select *
from `project.dataset.table1` t
where true
qualify row_number() over(partition by id order by timestamp desc) = 1
both with below output两者都具有以下 output
This provides you groups:这为您提供了组:
SELECT DISTINCT id, type, MAX(timestamp) date
FROM chinook.new_table as a
GROUP BY 1, 2
ORDER BY 1;
However, seemingly you want a single group for each id
, so you will need to somehow aggregate type
as well:但是,似乎您希望每个
id
有一个组,因此您还需要以某种方式聚合type
:
SELECT DISTINCT id, GROUP_CONCAT(`type`), MAX(timestamp) date
FROM chinook.new_table as a
GROUP BY 1
ORDER BY 1;
The above will group types into comma-separated values.以上将类型分组为逗号分隔的值。
Just use GROUP BY只需使用 GROUP BY
SELECT id, type, MAX(date) AS date_max FROM table1 GROUP BY id,type ORDER BY id,type;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.