I have a table called Policy like:
Name Version Id
Policy1 1.0.0 1
Policy2 2.0.0 2
Policy1 1.1.1 3
Policy2 2.0.1 4
I tried to extract the following:
Name Version Id
Policy1 1.1.1 3
Policy2 2.0.1 4
based on one value per name and the highest version in the table.
I tried the following:
ActiveRecord::Base.connection.exec_query('(SELECT MAX(A.version) AS rev, A.name FROM policies A GROUP BY A.name) ')
If I add id
to the mix like:
ActiveRecord::Base.connection.exec_query('(SELECT MAX(A.version) AS rev, A.name, A.id FROM policies A GROUP BY A.name) ')
I get the error:
ActiveRecord::StatementInvalid: Mysql2::Error: Expression #3 of SELECT
list is not in GROUP BY clause and contains nonaggregated column
'pikto_dev.A.id' which is not functionally dependent on columns in
GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by: (SELECT MAX(A.version) AS rev, A.name,
A.id FROM policies A GROUP BY A.name)`
I also tried things like:
Policy.select(:id, "MAX(version)", :name).group(:name)