# Sum columns individually

**URL:** <https://community.influxdata.com/t/sum-columns-individually/27265>\
**Category:** InfluxDB 2\
**Tags:** influxdb, grafana, flux\
**Created:** [November 6, 2022, 12:50am UTC](https://community.influxdata.com/t/sum-columns-individually/27265 "2022-11-06T00:50:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PradoEagle](https://avatars.discourse-cdn.com/v4/letter/p/977dab/32.png) [@PradoEagle](https://community.influxdata.com/u/PradoEagle)\
**Post date:** [November 6, 2022, 12:50am UTC](https://community.influxdata.com/t/sum-columns-individually/27265/1 "2022-11-06T00:50:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [November 9, 2022, 7:57pm UTC](https://community.influxdata.com/t/sum-columns-individually/27265/2 "2022-11-09T19:57:15Z")

</div>

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

```auto
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:

```auto
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 + / ") 

```

> **[union() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/union/)**
>
> union() merges two or more input streams into a single output stream.

---

<div class="post-metadata">

**Author:** ![PradoEagle](https://avatars.discourse-cdn.com/v4/letter/p/977dab/32.png) [@PradoEagle](https://community.influxdata.com/u/PradoEagle)\
**Post date:** [November 10, 2022, 10:54am UTC](https://community.influxdata.com/t/sum-columns-individually/27265/3 "2022-11-10T10:54:36Z")

</div>

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%.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/1/166c74747936420cead74a8397c3e0d7d1644c48.png)  
How to get the sum data on the same line?

---

<div class="post-metadata">

**Author:** ![PradoEagle](https://avatars.discourse-cdn.com/v4/letter/p/977dab/32.png) [@PradoEagle](https://community.influxdata.com/u/PradoEagle)\
**Post date:** [November 13, 2022, 12:42am UTC](https://community.influxdata.com/t/sum-columns-individually/27265/4 "2022-11-13T00:42:22Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/2/29c39a979d7777d49c8228f163d622d0dbc2663e.png)  
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?

---

<div class="post-metadata">

**Author:** ![grant1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/grant1/32/15107_2.png) [@grant1](https://community.influxdata.com/u/grant1)\
**Post date:** [November 13, 2022, 12:33pm UTC](https://community.influxdata.com/t/sum-columns-individually/27265/5 "2022-11-13T12:33:21Z")

</div>

@PradoEagle

Maybe this after your `tail` function…

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

```

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

Then in Grafana, try these settings:

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/1/15efad7bd703c9291e74d0d0011d28f632c55e60.png)

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

---

<div class="post-metadata">

**Author:** ![PradoEagle](https://avatars.discourse-cdn.com/v4/letter/p/977dab/32.png) [@PradoEagle](https://community.influxdata.com/u/PradoEagle)\
**Post date:** [November 14, 2022, 12:03pm UTC](https://community.influxdata.com/t/sum-columns-individually/27265/6 "2022-11-14T12:03:18Z")

</div>

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.
