Question : GROUP BY must contain non Outer reference

I am trying to get an average exchange rate for each month, but I seem to be getting an error

Msg 164, Level 15, State 1, Line 2
Each GROUP BY expression must contain at least one column that is not an outer reference.

Can you please let me know what's wrong with this query and how I can fix it?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
DECLARE @Xrate TABLE (xrProperty VARCHAR(2), xrDateTime DATETIME, xrXrate MONEY)
INSERT INTO @Xrate (xrProperty,xrXrate,xrDateTime)
SELECT	 'CU'
		,AVG(crr.ExchangeValue)
		,DATEPART(YYYY,crr.ExchangeDate) + '-' + DATEPART(MM,crr.ExchangeDate) + '-01 00:00:00 000'
FROM dbo.CurrencyRates crr
WHERE crr.CurrencyFromId = (SELECT cur.id FROM dbo.Currencies cur WHERE cur.CODE = 'EUR')
AND crr.CurrencyToId = (SELECT cur.id FROM dbo.Currencies cur WHERE cur.CODE = 'USD')
AND crr.ExchangeDate > '2005-01-01'
GROUP BY 'CU',DATEPART(YYYY,crr.ExchangeDate) + '-' + DATEPART(MM,crr.ExchangeDate) + '-01 00:00:00 000'
 
SELECT * FROM @Xrate
Open in New Window Select All

Answer : GROUP BY must contain non Outer reference

I believe that problem is because of the constant you're copying down from your select list.  Change it to this:

GROUP BY DATEPART(YYYY,crr.ExchangeDate) + '-' + DATEPART(MM,crr.ExchangeDate) + '-01 00:00:00 000'
 
 
Random Solutions  
 
programming4us programming4us