2

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.

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but this doesn't work properly. arrayJoin does not accept more than one argument.
you can try to indexOf() instead of arrayJoin(). Edit the answer
0

Try this:

WITH [5, 3, 7, 8, 11, 14, 54] AS o
SELECT name 
FROM VALUES 
(
  'name UInt32'
, 3, 5, 7, 8, 11, 14, 54, 3, 5 --, ...
)
ORDER BY indexOf(o, name)

The result is:

|name|
|----|
|5   |
|5   |
|3   |
|3   |
|7   |
|8   |
|11  |
|14  |
|54  |

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.