WITH ranked_dsl AS (
    SELECT 
        dsl.*, 
        product.name AS product_name,
        ROW_NUMBER() OVER (PARTITION BY product.name, dsl.code ORDER BY dsl.event_time) AS row_num
    FROM 
        "device-status-log" AS dsl
    LEFT JOIN device ON device.uuid = dsl.device_id
    LEFT JOIN product ON product.uuid = device.product_device_uuid
)
SELECT 
    product_name,
    ranked_dsl.*
FROM 
    ranked_dsl
WHERE ranked_dsl.row_num = 1;


SELECT * FROM SPACE LIMIT 100
--test test 