I am using mysql Ver 14.14 Distrib 5.7.21 on ubuntu 18.04.
I am having a table called companies and a table e_s_g_scores.
I would like to get the latest row from the table e_s_g_scores by the respective companies.symbol, whereas latest is the created_at field in e_s_g_scores.
Please find below my minimum example on db-fiddle:
When I am trying to do the following I get:
select
c.id as companies_id,
c.symbol,
c.name,
p.totalesg,
p.environmentscore,
p.socialscore,
p.governancescore,
p.ratingyear,
p.ratingmonth,
p.relatedcontroversy,
p.highestcontroversy,
p.peercount,
p.esgperformance,
p.peergroup,
p.created_at,
max(p.created_at)
from
companies c
join e_s_g_scores p on
p.companies_id = c.id
group by p.companies_id
order by p.created_at DESC;
I get the following error:
Query Error: Error: ER_WRONG_FIELD_WITH_GROUP: Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.p.totalEsg' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Any suggestions what I am doing wrong?
I appreciate your replies!