Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2 DB - Column must be in Group By list

i am using H2-DB to access static databases...

i have a table which looks like:

COUNTRY     STATE       CITY         LAT     LNG     COUNTRYID      STATEID     CITYID 
"Germany"   "Berlin"    ""           1.23    1.23    1              1           0
"Germany"   "München"   ""           1.23    1.23    1              2           0
"USA"       "Alabama"   "Auburn"     1.23    1.23    2              1           1
"USA"       "Alabama"   "Birmingham" 1.23    1.23    2              1           2
"USA"       "Alaska"    "Anchorage"  1.23    1.23    2              2           1
"USA"       "Alaska"    "Cordova"    1.23    1.23    2              2           2

its a huge list with lots of countries, most of them just have Country and State (like Germany here, whereas State's are Cities), a few also have a City (like USA here)...

the problem is now, when i query

SELECT * FROM MyTable WHERE COUNTRY = 'Germany' group by STATE order by STATE

to get a sorted list of the states (or cities), i get an error saying

Field CITY must be in the GROUP BY list

if the row has a city, i need the whole row, otherwise i would only need the State column, but i can just know after having queried it, wether it uses the city column or not, so i have to query "*" instead of "STATE"

the query should be okay, or? On MySql it is properly working... so whats the problem here?

Found this if it helps: http://www.h2database.com/javadoc/org/h2/constant/ErrorCode.html#c90016

Metin

like image 678
metinkale38 Avatar asked Jul 23 '15 19:07

metinkale38


People also ask

Does GROUP BY need all columns?

If you specify the GROUP BY clause, columns referenced must be all the columns in the SELECT clause that do not contain an aggregate function. These columns can either be the column, an expression, or the ordinal number in the column list.

What does GROUP BY 2 mean in SQL?

Consider above queries: Group by 1 means to group by the first column and group by 1,2 means to group by the first and second column and group by 1,2,3 means to group by first second and third column.

How do I add a column to my H2 database?

Alter Table Add. Alter Table Add is a command used to add a new column to a table along with the respective data type. This command commits the transaction in this connection.

What is GROUP BY in mysql?

The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.


1 Answers

MySQL is broken in regards to this. It allows columns in the GROUP BY that are neither in the group by nor arguments to aggregation functions. In fact, the documentation warns against using this extension.

So you can do:

SELECT state
FROM DIYANET
WHERE COUNTRY = 'Germany'
GROUP BY STATE 
ORDER BY STATE;

Or something like this:

SELECT state, min(city), min(lat), . . .
FROM DIYANET
WHERE COUNTRY = 'Germany'
GROUP BY STATE 
ORDER BY STATE;

But SELECT * is not allowed and doesn't really make sense.

like image 153
Gordon Linoff Avatar answered Sep 28 '22 20:09

Gordon Linoff