Query Help - Column must appear in the GROUP BY clause
I need some help with analytic functions as I cannot figure out why the below is wrong.
SELECT
a.HoldingCompanyID,
a.HoldingCompanyName,
a.Metric,
a.Frequency,
SUM(a.Spend) AS SpendTotal,
SUM(a.Spend) OVER (PARTITION BY a.Metric, a.Frequency)
FROM temp.rt_test a
GROUP BY 1,2,3,4;
[Code: 2640, SQL State: 42803] [Vertica]VJDBC ERROR: Column "a.Spend" must appear in the GROUP BY clause or be used in an aggregate function
If I take out " SUM(a.Spend) AS SpendTotal” and remove the GROUP BY then it works. Looking at documentation I believe GROUP BY is not used in analytic functions? But when I put back " SUM(a.Spend) AS SpendTotal” then I get the same error.
a.HoldingCompanyID,
a.HoldingCompanyName,
a.Metric,
a.Frequency,
SUM(a.Spend) AS SpendTotal,
SUM(a.Spend) OVER (PARTITION BY a.Metric, a.Frequency)
FROM temp.rt_test a
[Code: 2640, SQL State: 42803] [Vertica]VJDBC ERROR: Column "a.HoldingCompanyID" must appear in the GROUP BY clause or be used in an aggregate function
This works…
SELECT
a.HoldingCompanyID,
a.HoldingCompanyName,
a.Metric,
a.Frequency,
SUM(a.Spend) OVER (PARTITION BY a.Metric, a.Frequency)
FROM temp.rt_test a
Answers
Hi,
I am not exactly sure what you are trying to accomplish, but all columns NOT listed in the GROUP BY clause need to have an aggregate function.
Example: