The result column names you define in your SELECT
clause don't exist in the GROUP BY
clause. SQL's syntax is confusing because the last step of the query is the first thing you write. You should interpret it as
FROM `C:\a.xlsx`.[Sheet1$]
GROUP BY region,
[Sheet1$].[Cost Center],
[Sheet1$].[Cost Element],
[Sheet1$].[Document Date]
SELECT 'B' As "region",
[Sheet1$].[Cost Center] As "cost center",
[Sheet1$].[Cost Element] AS "cost element",
[Sheet1$].[Document Date] AS "document date",
sum([Sheet1$].[Val/COArea Crcy]) as "val"
Here, it is obvious that region
does not yet exist.
FROM `C:\a.xlsx`.[Sheet1$]
GROUP BY
[Sheet1$].[Cost Center],
[Sheet1$].[Cost Element],
[Sheet1$].[Document Date]
SELECT 'B' As "region",
[Sheet1$].[Cost Center] As "cost center",
[Sheet1$].[Cost Element] AS "cost element",
[Sheet1$].[Document Date] AS "document date",
sum([Sheet1$].[Val/COArea Crcy]) as "val"
Here, it is also obvious that 'B'
(a constant) can be determined without any aggregate functions, because you don't use any columns that aren't part of your GROUP BY
clause. Not using any columns at all is merely a special case of not using any columns that aren't part of your GROUP BY
clause. :)
What you've done to resolve the problem is exactly what you should have done.