Skip to content

Instantly share code, notes, and snippets.

@learngcpwithmahesh
Last active February 3, 2022 08:09
Show Gist options
  • Save learngcpwithmahesh/5dc7e1b94107aed7d731d7285666777c to your computer and use it in GitHub Desktop.
Save learngcpwithmahesh/5dc7e1b94107aed7d731d7285666777c to your computer and use it in GitHub Desktop.
WITH device_details AS (
SELECT 'deviceA' AS unique_id, '{"data":"deviceA"}' AS data, '2022-02-03 11:00:00' as event_time
UNION ALL select 'deviceA', '{"data":"deviceA"}', '2022-02-03 11:00:00' --duplicate
UNION ALL select 'deviceB', '{"data":"deviceB"}', '2022-02-02 10:00:00'
UNION ALL select 'deviceA', '{"data":"deviceA"}', '2022-02-02 10:00:00'
UNION ALL select 'deviceB', '{"data":"deviceB"}', '2022-02-02 10:00:00' --duplicate
UNION ALL select 'deviceC', '{"data":"deviceC"}', '2022-02-04 10:00:00'
UNION ALL select 'deviceB', '{"data":"deviceB"}', '2022-02-02 10:00:00' --duplicate
),
remove_duplicates as (
SELECT
unique_id
, data
, event_time
, ROW_NUMBER() OVER(PARTITION BY unique_id, event_time order by event_time) AS row_details
FROM device_details
)
SELECT * FROM remove_duplicates WHERE row_details = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment