Sum columns individually

I have a table with 3 columns and I am trying to sum individually the 3 columns. Summing 1 no problem.
Data is this
Grid_Supply Battery_Supply Solar_Supply
0 54.2 47.2
0 54.2 47.2
0 36.7 31.9
0 36.7 31.9
0 48.3 44.3
Extract from query
|> keep(columns: [“Solar_Supply”, “Grid_Supply”, “Battery_Supply”])
If I try
|> sum(column: “Solar_Supply”)
I get the sum of solar supply as expected.
If I try this
|> sum(column: [“Solar_Supply”, “Grid_Supply”, “Battery_Supply”])
I get the this error
invalid: error @17:18-17:67: expected string but found [string] (array) (argument column)
I am trying to get 3 summed numbers so I can create a pie chart in Grafana.
I am sure it is easy but I can’t see any examples from multiple searches

Hello @PradoEagle,
Thanks for your question and welcome!
Your flux script would look something like this:

data = from...
...
|> keep(columns: [“Solar_Supply”, “Grid_Supply”, “Battery_Supply”])


sum_solar_supply = data |> sum(column: “Solar_Supply”) |> yield(name: "sum solar") 

sum_grid_supply = data |> sum(column: “Grid_Supply”) |> yield(name: "grid solar") 

sum_battery_supply = data |> sum(column: “Battery_Supply”) |> yield(name: "battery solar") 

Please let me know if that helps.

You might optionally want to union after too:

union(tables: [sum_solar_supply , sum_grid_supply, sum_battery_supply ])
|> yield(name: "everything together and get rid of the other yield statemetns by commenting out with // or cmd + / ") 
1 Like

Thanks for the reply. Got most if it to work except the final step to display the pie chart. The data is on 3 lines after the union. The pie chart only picks up the first value (11066) and makes is 100%.


How to get the sum data on the same line?

I have been able to align the values to 1 row using sort, fill and tail.
union(tables: [sum_solar_supply, sum_grid_supply, sum_battery_supply])
|> yield(name:“result”)
|> sort(columns: [“Solar_Supply”])
|> fill(column: “Battery_Supply”, usePrevious: true)
|> fill(column: “Grid_Supply”, usePrevious: true)
|> tail(n: 1)
This however produces 2 tables and I don’t know why or how to delete the the original


I have tried to remove the table and columns with various combinations of keep. drop and filter but I am only able to remove parts of the resultant table which is the one I want to keep.
Any ideas with this?

@PradoEagle

Maybe this after your tail function…

|> group()
|> yield(name: "piechart")

This last group() function merges all tables into a single table.

Then in Grafana, try these settings:

(maybe choose Numeric fields if the above does not look as expected)

Thanks for the reply but the final yield statement does not join the tables back together. The whole thing is very strange in not being able to work with a table that I can clearly see is there.
I can filter out the data I don’t want with Grafana but not with flux.