I'm getting a syntax error in SQL Server Management Studio (SQL Server 2008) for the following query:
SELECT
MIN(EffectiveOn) AS EffectiveOn,
Address1,
Address2,
Address3,
RTRIM(LTRIM(COALESCE(Address4, '') + ' ' + COALESCE(Address5, ''))) Address4,
PostCode,
PolicyId
FROM
NameAddressCoverHoldingTable
GROUP BY
Address1,
Address2,
Address3,
RTRIM(LTRIM(COALESCE(Address4, '') + ' ' + COALESCE(Address5, ''))) Address4,
PostCode,
PolicyId
ORDER BY
PolicyId,
MIN(EffectiveOn);
Error message: Msg 102, Level 15, State 1, Line 15 Incorrect syntax near 'Address4'
I can't see what is wrong - this works:
SELECT
MIN(EffectiveOn) AS EffectiveOn,
Address1,
Address2,
Address3,
--RTRIM(LTRIM(COALESCE(Address4, '') + ' ' + COALESCE(Address5, ''))) Address4,
PostCode,
PolicyId
FROM
NameAddressCoverHoldingTable
GROUP BY
Address1,
Address2,
Address3,
--RTRIM(LTRIM(COALESCE(Address4, '') + ' ' + COALESCE(Address5, ''))) Address4,
PostCode,
PolicyId
ORDER BY
PolicyId,
MIN(EffectiveOn);
What is the syntax error?