简体   繁体   English

如何返回最近的值?

[英]HOW TO RETURN THE MOST RECENT VALUE?

timestamp时间戳 id ID type类型
2021-06-01T00:00:00 2021-06-01T00:00:00 ID1 ID1 LOL哈哈
2021-06-01T00:00:01 2021-06-01T00:00:01 ID2 ID2 DOTA刀塔
2021-06-01T00:00:02 2021-06-01T00:00:02 ID2 ID2 DOTA刀塔
2020-06-02T00:00:00 2020-06-02T00:00:00 ID5 ID5 COD鳕鱼
2020-06-02T00:00:01 2020-06-02T00:00:01 ID7 ID7 VALO瓦洛
2020-06-02T00:00:02 2020-06-02T00:00:02 ID8 ID8 VALO瓦洛
2020-06-02T00:00:03 2020-06-02T00:00:03 ID3 ID3 DOTA刀塔
2020-06-03T00:00:00 2020-06-03T00:00:00 ID1 ID1 DOTA刀塔
2020-06-03T00:00:01 2020-06-03T00:00:01 ID1 ID1 DOTA刀塔
2020-06-03T00:00:02 2020-06-03T00:00:02 ID2 ID2 DOTA刀塔
2020-06-03T00:00:03 2020-06-03T00:00:03 ID3 ID3 DOTA刀塔
2020-06-03T00:00:04 2020-06-03T00:00:04 ID4 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 ID type类型
ID1 ID1 DOTA刀塔
ID1 ID1 LOL哈哈
ID2 ID2 DOTA刀塔
ID3 ID3 DOTA刀塔
ID4 ID4 LOL哈哈
ID5 ID5 COD鳕鱼
ID7 ID7 VALO瓦洛
ID8 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 ID type类型 date日期
ID1 ID1 DOTA刀塔 2020-06-03 00:00:01 2020-06-03 00:00:01
ID1 ID1 LOL哈哈 2021-06-01 00:00:00 2021-06-01 00:00:00
ID2 ID2 DOTA刀塔 2021-06-01 00:00:02 2021-06-01 00:00:02
ID3 ID3 DOTA刀塔 2020-06-03 00:00:03 2020-06-03 00:00:03
ID4 ID4 LOL哈哈 2020-06-03 00:00:04 2020-06-03 00:00:04
ID5 ID5 COD鳕鱼 2020-06-02 00:00:00 2020-06-02 00:00:00
ID7 ID7 VALO瓦洛 2020-06-02 00:00:01 2020-06-02 00:00:01
ID8 ID8 VALO瓦洛 2020-06-02 00:00:02 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM