简体   繁体   English

连接两个表。 使用连接列作为 Group By 然后 select 基于时间戳的最新行

[英]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 1个 2020-03-03 09:07:09.058000 2020-03-03 09:07:09.058000 available可用的
2 2个 2020-03-03 09:51:03.852000 2020-03-03 09:51:03.852000 faulted故障
1 1个 2022-10-06 16:32:14.130000 2022-10-06 16:32:14.130000 charging收费
3 3个 2022-10-06 16:28:26.228000 2022-10-06 16:28:26.228000 available可用的
4 4个 2022-10-06 16:28:03.195000 2022-10-06 16:28:03.195000 charging收费

connector连接器

connector连接器 box_id box_id connector_id connector_id
1 1个 Alpha Α 0 0
2 2个 Alpha Α 1 1个
3 3个 Beta测试版 0 0
4 4个 Beta测试版 1 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 box_id
1 1个 2020-03-03 09:07:09.058000 2020-03-03 09:07:09.058000 available可用的 Alpha Α
2 2个 2020-03-03 09:51:03.852000 2020-03-03 09:51:03.852000 faulted故障 Alpha Α
1 1个 2022-10-06 16:32:14.130000 2022-10-06 16:32:14.130000 charging收费 Alpha Α
3 3个 2022-10-06 16:28:26.228000 2022-10-06 16:28:26.228000 available可用的 Beta测试版
4 4个 2022-10-06 16:28:03.195000 2022-10-06 16:28:03.195000 charging收费 Beta测试版

With the desired result:有了想要的结果:

box_id 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 ;

https://dbfiddle.uk/1LRcbbma https://dbfiddle.uk/1LRcbbma

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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