I have only seen the following issue when working with SELECT statement containing a join.
There is a GROUP BY at the end of the query.
I have to put all of the columns that I want returned into the Group By or else I get an error message saying something such as you are missing columns. I add all of the column names into the GROUP BY and then the error goes away.
This is pseudo code:
SELECT A, B, C, Count(*) AS MY_COUNT
JOIN SECOND TABLE on MYFRIEND = YOURFRIEND
WHERE SOMETHING = 'THIS_IS_COOL'
AND SOMETHING_ELSE = 'YES'
GROUP BY A,B <---Since I did not include C it gives an error saying that C does not exist near the top next to the SELECT Statement.
When the SQL is less complex then I can get away with only using A or A,B.
When in Florida, Don't Tampa with the code. I made this up.
For Group BY, any column returned that's not an aggregation (sum, count, etc...) needs to be in the group by statement. Why this is important is those are all the buckets they are group into first before aggregated.
in your statement,
SELECT A, B, C, SUM(D) WHERE .... GROUP BY A,B
When you grouped (A,B), then the results are grouped into buckets organized by (A,B), without considering C values. Then it tries to summarize it into 1 entry for every (A,B) group. When you add 'C' into the mix, the C value is already lost so it cannot select C. You could try something like MIN(C) or MAX(C) since they are also considered aggregation functions.