diff --git a/mobile_config/src/mobile_radio_tracker.rs b/mobile_config/src/mobile_radio_tracker.rs index dcbfb2c27..881503bb0 100644 --- a/mobile_config/src/mobile_radio_tracker.rs +++ b/mobile_config/src/mobile_radio_tracker.rs @@ -208,32 +208,24 @@ pub async fn get_tracked_radios( fn get_all_mobile_radios(metadata: &Pool) -> impl Stream + '_ { sqlx::query_as::<_, MobileRadio>( r#" - WITH unique_entity_keys AS ( - SELECT - kta.entity_key as entity_key, - kta.asset as asset, - MAX(mhi.refreshed_at) AS refreshed_at - FROM key_to_assets kta - INNER JOIN mobile_hotspot_infos mhi - ON kta.asset = mhi.asset - WHERE kta.entity_key IS NOT NULL - AND mhi.refreshed_at IS NOT NULL - GROUP BY kta.entity_key, kta.asset - ) - SELECT - uek.entity_key, - mhi.refreshed_at, - mhi.location::bigint, - mhi.is_full_hotspot::int, - mhi.num_location_asserts, - mhi.is_active::int, - mhi.dc_onboarding_fee_paid::bigint, - mhi.device_type::text, - mhi.deployment_info::text - FROM unique_entity_keys uek + DISTINCT ON (kta.entity_key, mhi.asset) + kta.entity_key, + mhi.asset, + mhi.refreshed_at, + mhi.location::bigint, + mhi.is_full_hotspot::int, + mhi.num_location_asserts, + mhi.is_active::int, + mhi.dc_onboarding_fee_paid::bigint, + mhi.device_type::text, + mhi.deployment_info::text + FROM key_to_assets kta INNER JOIN mobile_hotspot_infos mhi ON - uek.asset = mhi.asset and mhi.refreshed_at = uek.refreshed_at + kta.asset = mhi.asset + WHERE kta.entity_key IS NOT NULL + AND mhi.refreshed_at IS NOT NULL + ORDER BY kta.entity_key, mhi.asset, refreshed_at DESC "#, ) .fetch(metadata)