# Script to Sum Multiple Table Columns

**URL:** <https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [November 27, 2023, 4:39pm UTC](https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383 "2023-11-27T16:39:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Markee](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Mike\_Markee](https://forum.inductiveautomation.com/u/Mike_Markee)\
**Post date:** [November 27, 2023, 4:39pm UTC](https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383/1 "2023-11-27T16:39:33Z")

</div>

Long time reader, first time caller.

I am looking to create a script that sums all the values in the "quantity" column for 10 datasets.  
The number of rows may change based on how many different products we create that hour.

Hour Values are datasets, column 1 are strings, column 2 are quantities.

columnname = 'COUNT(WO)'  
print columnname  
df = system.tag.readBlocking('[default]353/KPI/First Shift/Hour 1 Tank Counts')[0].value  
Hour2 = system.tag.readBlocking('[default]353/KPI/First Shift/Hour 2 Tank Counts')[0].value  
Hour3 = system.tag.readBlocking('[default]353/KPI/First Shift/Hour 3 Tank Counts')[0].value  
Hour4 = system.tag.readBlocking('[default]353/KPI/First Shift/Hour 4 Tank Counts')[0].value  
Hour5 = system.tag.readBlocking('[default]353/KPI/First Shift/Hour 5 Tank Counts')[0].value  
print df

total =system.math.sum(Hour3.getColumnIndex('COUNT(WO)'))

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 27, 2023, 4:50pm UTC](https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383/2 "2023-11-27T16:50:56Z")

</div>

What do you want to **do** with the result? And how often do you want to compute this?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 27, 2023, 4:51pm UTC](https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383/3 "2023-11-27T16:51:49Z")

</div>

Additionally, does it have to be a script? Expressions make this task pretty easy if your list of input dataset tags is fixed (and with one of Phil's modules, technically even that restriction can be lifted).

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [November 27, 2023, 4:56pm UTC](https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383/4 "2023-11-27T16:56:30Z")

</div>

As mentioned above, there are different ways to go about this, although I went ahead and cleaned your script up a little for example.

```auto
columnName = 'COUNT(WO)'
print columnName
tagPaths = ['[default]353/KPI/First Shift/Hour %s Tank Counts' % i for i in xrange(1,6)]
# returns qualified values
qValues = system.tag.readBlocking(tagPaths)
total = 0

for qVal in qValues:
	ds = qVal.value
	l = ds.getColumnAsList(ds.getColumnIndex(columnName))
    # if the list has null values, this won't work
	colSum = system.math.sum(l)
	print colSum
	total += colSum

print total
# without looping
df = qValues[0].value
Hour2 = qValues[1].value
Hour3 = qValues[2].value
Hour4 = qValues[3].value
Hour5 = qValues[4].value
print df

```

---

<div class="post-metadata">

**Author:** ![Mike\_Markee](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Mike\_Markee](https://forum.inductiveautomation.com/u/Mike_Markee)\
**Post date:** [November 27, 2023, 4:57pm UTC](https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383/5 "2023-11-27T16:57:52Z")

</div>

This is going to be set up in a GateWay event. I am putting it in a timer scrip to be called every 10seconds. Traditionally I would do this on the SQL side, but I do not have access to pull from this table and need to use the data I already have to quantify total.  
I will be taking the end sum and doing a system.tag.writeblocking(Dest,Value) in the end.

Python is new to me...

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [November 27, 2023, 4:58pm UTC](https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383/6 "2023-11-27T16:58:57Z")

</div>

Expression is probably the way to go here.

---

<div class="post-metadata">

**Author:** ![Mike\_Markee](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Mike\_Markee](https://forum.inductiveautomation.com/u/Mike_Markee)\
**Post date:** [November 27, 2023, 4:59pm UTC](https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383/7 "2023-11-27T16:59:47Z")

</div>

Can you show me an example of an expression that would sum dynamic rows in a table?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 27, 2023, 5:03pm UTC](https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383/8 "2023-11-27T17:03:46Z")

</div>

The expression is trivial:

```auto
sum(
	sum({[default]353/KPI/First Shift/Hour 1 Tank Counts}, "Count(WO)"),
	sum({[default]353/KPI/First Shift/Hour 2 Tank Counts}, "Count(WO)"),
	sum({[default]353/KPI/First Shift/Hour 3 Tank Counts}, "Count(WO)"),
	sum({[default]353/KPI/First Shift/Hour 4 Tank Counts}, "Count(WO)"),
	sum({[default]353/KPI/First Shift/Hour 5 Tank Counts}, "Count(WO)")
)

```

The expression language's `sum` has builtin capability to extract a particular column from a dataset - [sum - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/sum)

You could put this in an additional expression tag, running on a 10 second rate.

I also started writing out a script to do it as well, though I haven't tested this:

```python
columnname = 'COUNT(WO)'
basePath = '[default]353/KPI/First Shift/Hour {} Tank Counts' 
values = [qv.value for qv in system.tag.readBlocking([basePath.format(i + 1) for i in xrange(5)])]

return sum(value for value in column for column in ds.getColumnAsList(ds.getColumnIndex(columnname)) for ds in values)

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [November 27, 2023, 5:07pm UTC](https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383/9 "2023-11-27T17:07:07Z")

</div>

> [@paul-griffith](#):
>
> ```auto
> return sum(value for value in column for column in ds.getColumnAsList(ds.getColumnIndex(columnname)) for ds in values)
> 
> ```

```python
sum(sum(ds.getColumnAsList(ds.getColumnIndex(columnname))) for ds in values)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 27, 2023, 5:20pm UTC](https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383/10 "2023-11-27T17:20:57Z")

</div>

Worth pointing out that the expression can be dramatically easier on the eyes if you place the expression tag in the same folder as the counts, like so:

```auto
sum(
	sum({[.]Hour 1 Tank Counts}, "Count(WO)"),
	sum({[.]Hour 2 Tank Counts}, "Count(WO)"),
	sum({[.]Hour 3 Tank Counts}, "Count(WO)"),
	sum({[.]Hour 4 Tank Counts}, "Count(WO)"),
	sum({[.]Hour 5 Tank Counts}, "Count(WO)")
)

```

Which then also makes it possible to place a copy of the tag into the `Second Shift` folder, and have it work without changes.

---

<div class="post-metadata">

**Author:** ![Mike\_Markee](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Mike\_Markee](https://forum.inductiveautomation.com/u/Mike_Markee)\
**Post date:** [November 27, 2023, 6:03pm UTC](https://forum.inductiveautomation.com/t/script-to-sum-multiple-table-columns/82383/11 "2023-11-27T18:03:14Z")

</div>

Thank you! I appreciate the quick feedback! Works great
