I'm joining data from 2 tables to get the last transaction date of a customer in my database. This is a simplified example of what I'm trying to do:
SELECT c.CustomerID
c.CustomerFirstName
c.CustomerLastName
MAX(t.TransactionDate)
FROM [db].[customertable] C
LEFT JOIN [TransactionTable] T on c.CustomerID = t.CustomerID
GROUP BY c.CustomerID
ORDER BY t.TransactionDate
However when executing this code I get this error message:
'Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause'
I've spent some time trying to trouble shoot this and found that putting MAX() in front of the customer first name and last name works, gets rid of the error message and the output seems to be what I want.
Though I'm wondering if this is bad practice? I'm grouping by CustomerID and this is a unique reference - there will only be one first name and one last name belonging to each CustomerID so therefore am I right in saying putting the max in front of the first name / last name wont have any impact other than to make my query work?
Other solutions I found online suggested sub queries but I'm still learning SQL so not sure how to do that.
Thanks