EN VI

Sql - Sorting by selecting the order of values ClickHouse?

2024-03-11 15:00:08
How to Sql - 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

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login