Jon von der Heyden
MrExcel MVP, Moderator
- Joined
- Apr 6, 2004
- Messages
- 10,912
- Office Version
- 365
- Platform
- Windows
Hi All
I have a table for collecting results for different KPI's. When it comes to consolidating / aggregating the table into summaries, some KPI's should be summed (e.g. volume of corporate cards) and some KPI's should be averaged (e.g. average spend on corporate card). Therefore I have a field in the table that indicates which function to use for each record.
I would like to create a crosstab query, but I believe in order to have the average and sum results aggregate correctly I first need to select the 'Sum' records and then select the 'Average' records. Once the aggregate functions have been applied then I should be able to UNION ALL the results, right?
I come up with this:
I get 'Syntax error (missing operator) in query expression 'KPI_TBL.SECTOR UNION ALL....''
Am I asking it to do too much? The intention here is actually to use the SQL in ADO from excel and return a recordset. If needs be I could run two queries and join the recordsets, but if what I am asking to be done is possible then I would rather get it right.
Thanks.
I have a table for collecting results for different KPI's. When it comes to consolidating / aggregating the table into summaries, some KPI's should be summed (e.g. volume of corporate cards) and some KPI's should be averaged (e.g. average spend on corporate card). Therefore I have a field in the table that indicates which function to use for each record.
I would like to create a crosstab query, but I believe in order to have the average and sum results aggregate correctly I first need to select the 'Sum' records and then select the 'Average' records. Once the aggregate functions have been applied then I should be able to UNION ALL the results, right?
I come up with this:
Code:
TRANSFORM Sum(KPI_TBL.RESULT) AS SumOfRESULT
SELECT KPI_TBL.MAJOR_COMMODITY, KPI_TBL.KPI_NAME, KPI_TBL.KPI_DESCRIPTION, Sum(KPI_TBL.RESULT) AS TOTAL
FROM KPI_TBL
WHERE (((KPI_TBL.Period) = #5/1/2011#) And ((KPI_TBL.RECORD_TYPE) = 0) And ((KPI_TBL.COUNTRY) = "GB") And ((KPI_TBL.Function) = "Sum"))
GROUP BY KPI_TBL.MAJOR_COMMODITY, KPI_TBL.KPI_NAME, KPI_TBL.KPI_DESCRIPTION
ORDER BY KPI_TBL.MAJOR_COMMODITY, KPI_TBL.KPI_NAME
PIVOT KPI_TBL.SECTOR
UNION ALL
TRANSFORM Avg(KPI_TBL.RESULT) AS AvgOfRESULT
SELECT KPI_TBL.MAJOR_COMMODITY, KPI_TBL.KPI_NAME, KPI_TBL.KPI_DESCRIPTION, Avg(KPI_TBL.RESULT) AS TOTAL
FROM KPI_TBL
WHERE (((KPI_TBL.Period) = #5/1/2011#) And ((KPI_TBL.RECORD_TYPE) = 0) And ((KPI_TBL.COUNTRY) = "GB") And ((KPI_TBL.Function) = "Average"))
GROUP BY KPI_TBL.MAJOR_COMMODITY, KPI_TBL.KPI_NAME, KPI_TBL.KPI_DESCRIPTION
ORDER BY KPI_TBL.MAJOR_COMMODITY, KPI_TBL.KPI_NAME
PIVOT KPI_TBL.SECTOR;
I get 'Syntax error (missing operator) in query expression 'KPI_TBL.SECTOR UNION ALL....''
Am I asking it to do too much? The intention here is actually to use the SQL in ADO from excel and return a recordset. If needs be I could run two queries and join the recordsets, but if what I am asking to be done is possible then I would rather get it right.
Thanks.