# MS SQL Queries Adding Table Columns

**URL:** <https://forum.inductiveautomation.com/t/ms-sql-queries-adding-table-columns/23758>\
**Category:** Ignition\
**Created:** [April 1, 2019, 7:08pm UTC](https://forum.inductiveautomation.com/t/ms-sql-queries-adding-table-columns/23758 "2019-04-01T19:08:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EEdmondson](https://avatars.discourse-cdn.com/v4/letter/e/41988e/32.png) [@EEdmondson](https://forum.inductiveautomation.com/u/EEdmondson)\
**Post date:** [April 1, 2019, 7:08pm UTC](https://forum.inductiveautomation.com/t/ms-sql-queries-adding-table-columns/23758/1 "2019-04-01T19:08:56Z")

</div>

Hi all,

I’m not very Jython savvy. I have data that I am pulling from a dataset and I want to add columns to the table after I have pulled that data. I want to do this because I want to calculate some things from the data and display the results of those calculations in a new column. I have tried the ALTER TABLE function and it does not appear to work. I have also tried to add into the SELECT statement as:  
COUNT(CASE WHEN (EXAMPLE)=’ ’ then null else 1 end) Passed.  
However, when I try to run this it gives me an error that it doesn’t recognize the CASE statement.

This is a copy/paste of my current code.

SELECT Infeed\_Pick\_Time, HMC\_Place\_Position, Reject\_Place\_Time, Telesis\_Pick\_Time, Outfeed\_Place\_Time  
COUNT(CASE when Telesis\_Pick\_Time=’’ then null else 1 end) Passed  
FROM Table  
WHERE Infeed\_Pick\_Time \>= DATEADD(day,-7, GETDATE())

I want to add a column/rows for the new data that I calculate but it won’t let 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:** [April 1, 2019, 8:11pm UTC](https://forum.inductiveautomation.com/t/ms-sql-queries-adding-table-columns/23758/2 "2019-04-01T20:11:01Z")

</div>

Not sure about the query, but I can link you to a post where I did something similar.

> [@Compact Table Scripting and Add IF statement to columnData.append](http://forum.inductiveautomation.com/t/compact-table-scripting-and-add-if-statement-to-columndata-append/18358/7):
>
> Looks much nicer than my rep rep repetitiveness ha ha. I got this code to work and initialize all Booleans to 0. Your code threw an error…TypeError: can’t put element of type class java.lang.Booleanin column of type class java.lang.Integer po = event.source.value data = system.db.runNamedQuery("BOM/PoDetails",{"po":po}) fields = ['5x5', '4x8', 'RB', 'HW'] for field in fields: columnData = [] for row in range(data.getRowCount()): ##description = data.getValueAt(row, "Description")## This…

---

<div class="post-metadata">

**Author:** ![Stevenson.Yuan](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Stevenson.Yuan](https://forum.inductiveautomation.com/u/Stevenson.Yuan)\
**Post date:** [April 1, 2019, 8:16pm UTC](https://forum.inductiveautomation.com/t/ms-sql-queries-adding-table-columns/23758/3 "2019-04-01T20:16:22Z")

</div>

The syntax needs to be fixed:

```auto
select CASE Telesis_Pick_Time when '' then null else 1 end as Passed from table

```

if you’re trying to do count, you’ll need to include a group by at the end on how it should count the data.
