Skip to content

Instantly share code, notes, and snippets.

@redsquare
Created September 4, 2024 07:15
Show Gist options
  • Save redsquare/0dc86285e72adcf7bf05370eef0808a5 to your computer and use it in GitHub Desktop.
Save redsquare/0dc86285e72adcf7bf05370eef0808a5 to your computer and use it in GitHub Desktop.
merge maps take latest clickhouse
Handle the map as an array and then via ARRAY JOIN bring it into multiple rows, so you can use more complex SQL features. with argMax you could then get the latest value per key.
Then you need to regroup it again:
SELECT source, integration_id, transaction_id, min(min_event_time), groupArray((field_key, field_value))::Map(String, String)
FROM (
SELECT source, integration_id, transaction_id, min(event_time) AS min_event_time, field.1 AS field_key, argMax(field.2, event_time) AS field_value
FROM events
ARRAY JOIN fields AS field
GROUP BY source, integration_id, transaction_id, field.1
)
GROUP BY source, integration_id, transaction_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment