[英]Join two tables. Use a joined column as Group By to then select the latest row based on timestamp
I have two tables:我有两张桌子:
connector_status连接器状态
connector![]() |
status_timestamp![]() |
status![]() |
---|---|---|
1 ![]() |
2020-03-03 09:07:09.058000 ![]() |
available![]() |
2 ![]() |
2020-03-03 09:51:03.852000 ![]() |
faulted![]() |
1 ![]() |
2022-10-06 16:32:14.130000 ![]() |
charging![]() |
3 ![]() |
2022-10-06 16:28:26.228000 ![]() |
available![]() |
4 ![]() |
2022-10-06 16:28:03.195000 ![]() |
charging![]() |
connector连接器
connector![]() |
box_id ![]() |
connector_id ![]() |
---|---|---|
1 ![]() |
Alpha ![]() |
0 ![]() |
2 ![]() |
Alpha ![]() |
1 ![]() |
3 ![]() |
Beta![]() |
0 ![]() |
4 ![]() |
Beta![]() |
1 ![]() |
My connector_status table has multiple rows for each connector, but I only want the most recent row, based on the box_id我的 connector_status 表中的每个连接器都有多行,但我只想要基于 box_id 的最新行
I would like to join the tables based on box_id but using the latest timestamp from the 2x connectors.我想加入基于 box_id 的表,但使用来自 2x 连接器的最新时间戳。 This would select status charging based on the above table
这将 select status 根据上表计费
The joined table would look a bit like this:联接表看起来有点像这样:
connector![]() |
status_timestamp![]() |
status![]() |
box_id ![]() |
---|---|---|---|
1 ![]() |
2020-03-03 09:07:09.058000 ![]() |
available![]() |
Alpha ![]() |
2 ![]() |
2020-03-03 09:51:03.852000 ![]() |
faulted![]() |
Alpha ![]() |
1 ![]() |
2022-10-06 16:32:14.130000 ![]() |
charging![]() |
Alpha ![]() |
3 ![]() |
2022-10-06 16:28:26.228000 ![]() |
available![]() |
Beta![]() |
4 ![]() |
2022-10-06 16:28:03.195000 ![]() |
charging![]() |
Beta![]() |
With the desired result:有了想要的结果:
box_id ![]() |
status![]() |
---|---|
Alpha ![]() |
Charging![]() |
Beta![]() |
Available![]() |
I have the following code我有以下代码
SELECT IF(connector_status.status = 'Charging','Charging', IF(connector_status.status ='Available','Not Occupied', IF(connector_status.status = 'Faulted','Faulted','Occupied'))) AS group_status, connector.connector_id, connector.box_id, status_timestamp FROM connector_status JOIN connector ON connector_status.connector = connector.connector GROUP BY connector.box_id ORDER BY connector.box_id
I don't know how to do the join on box_id to get the max timestamp though.我不知道如何在 box_id 上进行连接以获得最大时间戳。
What is confusing me is if i firstly get the latest timestamp from the connector_status table and then try and join by box_id, how can i be sure that it will take the latest connector timestanmp for that box_id令我困惑的是,如果我首先从 connector_status 表中获取最新的时间戳,然后尝试通过 box_id 加入,我怎么能确定它将采用该 box_id 的最新连接器时间戳
You could use row_number to get the desired result:您可以使用 row_number 来获得所需的结果:
select box_id,
status
from ( select status,
box_id,
row_number() over( partition by tbl.box_id order by tbl.status_timestamp desc ) as rn
from ( select status_timestamp,
status,
box_id
from connector_status cs
inner join connector c on c.connector=cs.connector
) as tbl
) as x where rn=1 ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.