roee zi
roee zi

Reputation: 131

Clickhouse topK query on several columns

In ClickHouse, is there any way use the topK query on more than the column ,

for example:

select topK(10)(AGE,COUNTRY) ...

meaning I want the top10 combinations of AGE+COUNTRY, I only found a workaround using concat on fields and topK on them, wondered if there is any other way.

Upvotes: 1

Views: 1555

Answers (1)

vladimir
vladimir

Reputation: 15226

You can pass array (or tuple) of columns to topK:

SELECT topK(10)([Age, Country])
FROM table

Or use the straightforward calculation (it is much slower but provides the exact result):

SELECT
    Age,
    Country
FROM table
GROUP BY
    Age,
    Country
ORDER BY count() DESC
LIMIT 10

Upvotes: 3

Related Questions