A
A
Alexander Ulo2019-07-06 00:56:32
SQL
Alexander Ulo, 2019-07-06 00:56:32

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

(PS did not find sql syntax highlighting)
But in my case group1 has 2 maximum values, and both are displayed. I need a strict grouping by group, and what id gets into the selection (1 or 2) does not matter.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ulo, 2019-07-06
@Salexer

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 question

Ask a Question

731 491 924 answers to any question