Skip to main content
added 265 characters in body
Source Link

but this is hardly a useful result? Another possibility is to randomly pick one row from each group, like:

     2014    30  -- 10+20 in group 1
     2015    40  -- 15+25 in group 2

The results are unpredictable and you may get different result for the same data/query.

SQL92 requires that all columns in the SELECT CLAUSE is part of the GROUP BY CLAUSE, so if we want:

Since there is only one PARAMETERNO per YEAR, the result is the same.

MySQL historically allowed you to violate the SQL92 as well as the SQL99 GROUP BY rule, unless you specified ONLY_FULL_GROUP_BY in @@sql_mode. This may lead to un-deterministic results, and has caused much confusion in forums such as this one over the years. I personally always set @@sql_mode to ONLY_FULL_GROUP_BY when using older MySQL version than the latest (which I havent tried).

but this is hardly a useful result?

SQL92 requires that all columns in the SELECT CLAUSE is part of the GROUP BY CLAUSE, so if we want:

Since there is only one PARAMETERNO per YEAR, the result is the same.

but this is hardly a useful result? Another possibility is to randomly pick one row from each group, like:

     2014    30  -- 10+20 in group 1
     2015    40  -- 15+25 in group 2

The results are unpredictable and you may get different result for the same data/query.

SQL92 requires that all columns in the SELECT CLAUSE is part of the GROUP BY CLAUSE, so if we want:

Since there is only one PARAMETERNO per YEAR, the result is the same.

MySQL historically allowed you to violate the SQL92 as well as the SQL99 GROUP BY rule, unless you specified ONLY_FULL_GROUP_BY in @@sql_mode. This may lead to un-deterministic results, and has caused much confusion in forums such as this one over the years. I personally always set @@sql_mode to ONLY_FULL_GROUP_BY when using older MySQL version than the latest (which I havent tried).

added 265 characters in body
Source Link

would be valid. AFAIK know recent versions of PostgreSQL and MySQL are the only ones that implements the SQL99 version of GROUP BY.

A trick for those DBMS that don't implement the SQL99 version is to apply an aggregate to those columns:

SELECT YEAR, MAX(PARAMETERNO), SUM(MARK)
FROM T
GROUP BY YEAR 

Since there is only one PARAMETERNO per YEAR, the result is the same.

would be valid. AFAIK know recent versions of PostgreSQL and MySQL are the only ones that implements the SQL99 version of GROUP BY

would be valid. AFAIK know recent versions of PostgreSQL and MySQL are the only ones that implements the SQL99 version of GROUP BY.

A trick for those DBMS that don't implement the SQL99 version is to apply an aggregate to those columns:

SELECT YEAR, MAX(PARAMETERNO), SUM(MARK)
FROM T
GROUP BY YEAR 

Since there is only one PARAMETERNO per YEAR, the result is the same.

added 150 characters in body
Source Link

It is not clear to me what you are asking but I believe that GROUP BY is probably one of the most misunderstood concepts in SQL, so I'll add this answer anyhow. It may or may not help with the understanding of the concept GROUP BY. AssumeAssume we have a table like:

GROUP BY is probably one of the most misunderstood concepts in SQL. Assume we have a table like:

It is not clear to me what you are asking but I believe that GROUP BY is one of the most misunderstood concepts in SQL, so I'll add this answer anyhow. It may or may not help with the understanding of the concept GROUP BY. Assume we have a table like:

Source Link
Loading