# Total for Columns

**URL:** <https://forum.inductiveautomation.com/t/total-for-columns/35733>\
**Category:** Ignition\
**Created:** [June 17, 2020, 12:47am UTC](https://forum.inductiveautomation.com/t/total-for-columns/35733 "2020-06-17T00:47:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![malejandroreyes](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@malejandroreyes](https://forum.inductiveautomation.com/u/malejandroreyes)\
**Post date:** [June 17, 2020, 12:47am UTC](https://forum.inductiveautomation.com/t/total-for-columns/35733/1 "2020-06-17T00:47:11Z")

</div>

I have this to create headers and count from a SQL Column.

```auto
SELECT 
	Description, [DIC] as DIC, [DOC] as DOC, [CMF] as CMF,
	DIC + DOC + CMF as Total
FROM
(
	SELECT
		Description,
		TagName,
		Area
	FROM
		v_AlarmHistory
	WHERE
		(EventStamp > :FechaInicio)
	    and
	  	(EventStamp < :FechaFin)
		and
		Area <> 'Events'
		and
		AlarmState LIKE :Alarm
)t
PIVOT(
	COUNT(TagName)
	FOR Area IN(
	[DIC],
	[DOC],
	[CMF],
	[Total])

) AS pvt
ORDER BY Total DESC

```

But I want totalize all columns created after PIVOT instruction  
The result after the PIVOT instruction is this

 ![capture 3](https://us1.discourse-cdn.com/inductiveautomation/original/2X/b/bdc74349a296108d380aed0838b2c1258e2b2b74.png)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 17, 2020, 1:31am UTC](https://forum.inductiveautomation.com/t/total-for-columns/35733/2 "2020-06-17T01:31:56Z")

</div>

Good to see you got your PIVOT working 🙂  
It’s always good, especially with SQL questions, to give the table you have (which you’ve done) and also the table or resulting dataset that you’d like to see so that we’re not left guessing or assuming what you mean.

Are you wanting to summate the Totals column to give one total? E.g. 279+134+107+… = 123456

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [June 17, 2020, 2:36pm UTC](https://forum.inductiveautomation.com/t/total-for-columns/35733/3 "2020-06-17T14:36:51Z")

</div>

When you paste in code, be sure to add three backticks (`) before and after your code to preserve the formatting. I took the liberty of editing your post to make it more readable.

---

<div class="post-metadata">

**Author:** ![malejandroreyes](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@malejandroreyes](https://forum.inductiveautomation.com/u/malejandroreyes)\
**Post date:** [June 17, 2020, 2:59pm UTC](https://forum.inductiveautomation.com/t/total-for-columns/35733/4 "2020-06-17T14:59:59Z")

</div>

I want to summate all the columns and get total of each one.

---

<div class="post-metadata">

**Author:** ![kgamble](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgamble/32/8019_2.png) [@kgamble](https://forum.inductiveautomation.com/u/kgamble)\
**Post date:** [June 17, 2020, 3:14pm UTC](https://forum.inductiveautomation.com/t/total-for-columns/35733/5 "2020-06-17T15:14:53Z")

</div>

Try adding an aggregation to your `DIC, DOC, and CMF` columns, but grouping by (with Rollup) on your description.

It wont actually group anything further together because all of your descriptions are unique after the pivot, but will add an extra row that shows the totals and a null description

```auto
SELECT 
	Description, SUM([DIC]) as DIC, SUM([DOC]) as DOC, SUM([CMF]) as CMF,
	SUM(DIC + DOC + CMF) as Total
FROM
(
	SELECT
		Description,
		TagName,
		Area
	FROM
		v_AlarmHistory
	WHERE
		(EventStamp > :FechaInicio)
	    and
	  	(EventStamp < :FechaFin)
		and
		Area <> 'Events'
		and
		AlarmState LIKE :Alarm
)t
PIVOT(
	COUNT(TagName)
	FOR Area IN(
	[DIC],
	[DOC],
	[CMF],
	[Total])

) AS pvt
GROUP BY ROLLUP (Description)
ORDER BY Total DESC

```

If you want the total row to show “Totals” or something for the description, then add an Isnull around your selection of the Description column like this

```auto
ISNULL(Description, 'Totals') Description

```

---

<div class="post-metadata">

**Author:** ![malejandroreyes](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@malejandroreyes](https://forum.inductiveautomation.com/u/malejandroreyes)\
**Post date:** [June 17, 2020, 7:24pm UTC](https://forum.inductiveautomation.com/t/total-for-columns/35733/6 "2020-06-17T19:24:59Z")

</div>

Thank you, it works!
