Search code examples
sqldatabaseclickhouse

Sorting by selecting the order of values ClickHouse


MySQL has a sorting function that allows you to select the order of values ORDER BY FIELD() ClickHouse does not have this function. I have a similar request.: SELECT name FROM table WHERE id IN(5,3,7,8,11,14,54)

I need the result to be returned in the order specified inside IN(). I can't find information anywhere on how to do this.


Solution

  • You can try to use indexOf keyword.

    SELECT name
    FROM table
    WHERE id IN (5, 3, 7, 8, 11, 14, 54)
    ORDER BY indexOf([5, 3, 7, 8, 11, 14, 54], id);
    

    This gives you the result in the order specified in the IN() clause