Trying to complete this multi-query assignment but cannot figure out why I keep getting this error code (Error Code: 1055. Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'ebe.Co.ContactName' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by). This is my code below
USE EBE;
SELECT C.ClientName, CO.ContactName, CO.ContactPhone, CO.ContactEmail,
COUNT(E.EventCode) AS NumberOfEvents
FROM Client AS C
INNER JOIN Contact AS CO ON C.ClientID = CO.ClientID
INNER JOIN Events AS E ON C.ClientID = E.ClientID
GROUP BY E.ClientID
ORDER BY C.ClientName;
CodePudding user response:
All the columns that are being selected except for the aggregated column need to be in the GROUP BY
SELECT C.ClientName, CO.ContactName, CO.ContactPhone, CO.ContactEmail,
COUNT(E.EventCode) AS NumberOfEvents
FROM Client AS C
INNER JOIN Contact AS CO ON C.ClientID = CO.ClientID
INNER JOIN Events AS E ON C.ClientID = E.ClientID
GROUP BY C.ClientName, CO.ContactName, CO.ContactPhone, CO.ContactEmail
ORDER BY C.ClientName
CodePudding user response:
Another way of writing your query to remove the need for group by
would be to use a correlated subquery:
select C.ClientName,
CO.ContactName,
CO.ContactPhone,
CO.ContactEmail,
(select Count(*) from Events E where E.ClientID = C.ClientID) NumberOfEvents
from Client C
join Contact CO on CO.ClientID = c.ClientID
order by C.ClientName;