Search code examples
sqlselectdistinctclickhouse

select all from table, without duplicates by 1 column


I have table (clickhouse) as history:

id product_name count date
123 name 1 10 2022-10-19
123 name 1 10 2022-10-20
123 rename 1 10 2022-10-23
123 rename 1 12 2022-11-19
222 name 2 50 2022-10-23
222 name 2 50 2022-10-24
222 name 2 52 2022-10-25

id (not unique) it is product_name id and product name can change. Date it is record into table.

I need select all from table where product_name or count was changed.

id product_name count date
123 name 1 10 2022-10-19
123 rename 1 10 2022-10-23
123 rename 1 12 2022-11-19
222 name 2 50 2022-10-23
222 name 2 52 2022-10-25

I don't have a lot of SQL experience.

Please help!


Solution

  • I believe you can do:

    SELECT * 
    FROM your_table
    LIMIT 1 BY id, product_name, count
    

    That will get you only a single row for each unique combination of (id, product_name, count)

    https://clickhouse.com/docs/en/sql-reference/statements/select/limit-by/