I am getting the following error, upon running the attached code.
Msg 8120, Level 16, State 1, Line 1
Column 'HATB2.CustomerID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
My T-SQL troubleshooting skills are not good, so that's why I'm asking.
I agree that this is a long select statement. But, I sort of know what the problem is and I know how to point you to a "summary" of this.
If you do a "find" on the following clauses in the attached select statement, you will see that this long select statement is actually three sets of select statements for three separate databases (TRXDE, TRXIN, and TRXUK):
--TRXIN BEGINS HERE:
--TRXUK BEGINS HERE:
Again, doing a search on the two clauses that I mention above will help you see the "separation" among the three databases.
Anyway, the error is referring to the first line of the long select statement which ultimately refers to the very end of the long select statement.
Somehow, it does not like the reference to HATB2. This reference is in three spots throughout this long select statement. That's probably the issue.
Really, it's got to be just some simple T-SQL syntax change with ORDER BY and GROUP BY. But, I'm not skilled enough to see what the fix is.
If I run the three "sets" of select statements individually, the correct data is returned. It's combining them in this select statement that is generating this error.
Perhaps, I need to add another select clause at the beginning or change HATB2 to HATB3, HATB4, etc.
I hope that makes sense.
Any help is much appreciated!