Select columns by min value - group by

When I read this post: SQL: group by the minimum value in one field when selecting individual rows

The following solution made no sense to me, and I wonder how this might work:

SELECT id, min(record_date), other_cols 
FROM mytable
GROUP BY id

This does NOT work in my database, and for some reason it seems illogical to me (although it works in the example SQL script). My DBMS complains that

The column must be a group column: other_cols

I use MaxDB - is it a specific DBMS problem?

+4
source share
3 answers

It works in some DBMSs, such as MySQL . MySQL docs :

MySQL GROUP BY, , GROUP BY. , MySQL. , . , , , GROUP BY, .

SQL Server GROUP BY. , :

"ColName" , , GROUP BY.

. MSDN.

+3

, RDBMS , , SQL Server , (MIN, MAX, AVERAGE ..) , Group By Group By.

MySql, , , , , other_cols .

, (, SQL Server).

+2

SQL. :

SELECT mytable.id, mytable_id_with_min_date.min_record_date, other_cols 
FROM mytable JOIN
  (
    SELECT id, min(record_date) min_record_date
    FROM mytable
    GROUP BY id
  ) mytable_id_with_min_date ON (mytable_id_with_min_date.id = mytable.id AND mytable.record_date = mytable_id_with_min_date.min_record_date)

, , other_cols?

0

Source: https://habr.com/ru/post/1542297/


All Articles