I have the following SQL query:
select * from my_table
then the returned results looks like:
my_id field_1 field_2 ...
1 ... ...
1 ... ...
1 ... ...
2
3
3
I only want to keep one record per my_id
, perhaps taking the record with minimum value in field_2
Therefore, I am expecting the following query to fail becauseI haven't put a desired aggregation function after select
:
select * from my_table group by my_id order by my_id
However, the query went through and returned table have no duplicated my_id
. Therefore, I am wondering is there a default method SQL is using if I didn't specify an aggregation function before group by
?
I am expecting the following query to fail because I haven't put a desired aggregation ..
Unfortunately, some SQL Implementations allow GROUP BY without an aggregate function 1 .
In this case the result is "not defined / implementation defined" - see the specific implementation documentation to see if it provides any guarantees. That is, while it is still guaranteed that the my_id
column is unique, values from any row could be returned for other output fields .
(Obviously, if my_id
were a key / candidate key - but it is not in this case - then this doesn't make a difference as only one row could be selected..)
For increased compatibility and predictable results, use an aggregate function for every field which is not covered by the GROUP BY - even if the particular SQL/RDBMS does not enforce or "require" the use aggregates.
1 While the original query is "accepted" in MySQL ( depending on ONLY_FULL_GROUP_BY ), both PostgreSQL and SQL Server would have rejected the original query "as expected".
You can use a correlated subquery:
select t.*
from my_table t
where t.field2 = (select min(t2.field2)
from my_table t2
where t2.my_id = t.my_id
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.