Discussion Board for collaboration on QlikView Scripting.
I have some problem with the GROUP BY clause when loading a script.
sum(Amount) AS Sales
WHERE Week > 10;
SQL SELECT *
FROM DatabaseName.dbo.TableName GROUP BY Week, Status ORDER BY Week;
My error message is that the first column I select (*) is not grouped by or aggregated, as I don't want to load it into my table. On the other hand, if I do...
SELECT Week, Status, Amount
... I won't accomplish this either.
Thanks for your help in advance!
Go to Solution.
Try like this
Select Week, Status, sum(Amount) As Sales
FROM DatabaseName.dbo.TableNameGROUP BY Week, Status;
This is strictly an SQL problem. You can't select any fields in a GROUP BY query unless they are either part of the GROUP BY list, or aggregated in some function or another (e.g. sum, count, etc).
In your case, you'd probably want to use SELECT Week, Status, sum(Amount).
Alternatively, you can load the full data (without a GROUP BY clause) and group it in QV's load statement, but you'll still have to aggregate Amount.
Thanks for your quick answer.
OK, I have managed to accomplish this in an original SQL database manager.
Is it correct that the problem is that I load and select different things in the script I provided? In "classic SQL" I just have to write...
FROM DatabaseName.dbo.TableNameGROUP BY Week, Status
... and I get the result set I want to get.
Can you please give me a code example of a possible solution?
Thank you very much!
You have misplaced the group by wrongly. Your script should be like this
WHERE Week > 10 group by Week, Status;
FROM DatabaseName.dbo.TableName ORDER BY Week
Thanks a lot. It works.
In other words, it's preferred to make the aggregations in the select part, while using GROUP BY...