variable fuellen

kiki's profile image kiki posted 1 year ago in Running SQL scripts Permalink

Hello i am learning about declaring and using variables in HeidiSQL running the following script in do get the errormassage "invalid use of group function" how can i fill a variable to a sum function

USE `pv-anlage`;
SET @myvar= SUM(fusionsolardaten.netzbezug_kwh);
SELECT @myvar;

thx for helping

ansgar's profile image ansgar posted 1 year ago Permalink

You have to use that SUM() function in a SELECT context:

USE `pv-anlage`;
SELECT @myvar:=SUM(netzbezug_kwh) FROM fusionsolardaten;
SELECT @myvar;

Normally you do not need a variable to get that, as the SELECT query also tells you the same:

USE `pv-anlage`;
SELECT SUM(netzbezug_kwh) FROM fusionsolardaten;

Please login to leave a reply, or register at first.