Answer the question
In order to leave comments, you need to log in
How to build a query in sql?
Task: Display values grouped by group, where price should be the maximum. In addition, this price can be the same for group.
Please help, because 10 hours of searching all over the Internet did not lead to anything good. The database is clickhouse, but that doesn't seem to make a difference.
Example:
id | group | price
--------------------
1 | group1 | 1000
2 | group1 | 1000
3 | group3 | 500
4 | group2 | 600
5 | group2 | 400
Required:
id | group | price
--------------------
1 | group1 | 1000
3 | group3 | 500
4 | group2 | 600
There was an option:
SELECT t1.* FROM table t1, (SELECT group, MAX(price) price FROM table GROUP BY group) t2
WHERE t1.group=t2.group AND t1.price=t2.price
Answer the question
In order to leave comments, you need to log in
In the case of clickhouse, the request worked correctly:
SELECT argMax(id, price), argMax(group, price), max(price ) FROM table GROUP BY group
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question