# SQL Error

**URL:** <https://forum.servoy.com/t/sql-error/14342>\
**Category:** Classic Servoy\
**Created:** [July 23, 2011, 3:02pm UTC](https://forum.servoy.com/t/sql-error/14342 "2011-07-23T15:02:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgerz](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@rgerz](https://forum.servoy.com/u/rgerz)\
**Post date:** [July 23, 2011, 3:02pm UTC](https://forum.servoy.com/t/sql-error/14342/1 "2011-07-23T15:02:12Z")

</div>

I am trying to get average of a group of records in a dataset, but getting an error when I run getDataSetByQuery:

The sql code is:

```auto
	var mySql = 'SELECT AVG(tbl_data_entry.mean) AS mean_avg, ' +
	            'tbl_data_entry.test_code, ' +
	            'tbl_data_entry.control_type, ' +
	            'tbl_data_entry.control_level, ' +
	            'tbl_data_entry.instrument_mfg_name, ' +
	            'tbl_data_entry.instrument_model ' +
	            'FROM tbl_data_entry ' +
	            'WHERE ' +
	            'tbl_data_entry.test_code="' + test_code + '" AND ' +
	            'tbl_data_entry.control_type="' + control_type + '" AND ' +
	            'tbl_data_entry.control_level="' + control_level + '" AND ' +
	            'tbl_data_entry.instrument_mfg_name="' + instrument_mfg_name + '" AND ' +
	            'tbl_data_entry.instrument_model="' + instrument_model + '"' +
	            'GROUP BY ' +
	            'tbl_data_entry.test_code, ' +
	            'tbl_data_entry.control_type, ' +
	            'tbl_data_entry.control_level, ' +
	            'tbl_data_entry.instrument_mfg_name, ' +
	            'tbl_data_entry.instrument_model '

	var myDataSet = databaseManager.getDataSetByQuery('medlabqcpro_qc',mySql,null,500)

```

I am getting this error:

```auto
ERROR: column "APTT" does not exist
Wrapped java.lang.RuntimeException: com.servoy.j2db.dataprocessing.DataException: ERROR: column "APTT" does not exist (C:\Users\rgerz\servoy_workspace51\app_quick_advisor\forms\data_entry_utility_entry_container.js#55)
	at C:\Users\rgerz\servoy_workspace51\app_quick_advisor\forms\data_entry_utility_entry_container.js:55 (calcTrueValue)

```

There is a value “APTT” in the test\_code column

Database is Postgres and test\_code is a stored calculation field

Any ideas?

---

<div class="post-metadata">

**Author:** ![ROCLASI](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/roclasi/32/4371_2.png) [@ROCLASI](https://forum.servoy.com/u/ROCLASI)\
**Post date:** [July 23, 2011, 3:13pm UTC](https://forum.servoy.com/t/sql-error/14342/2 "2011-07-23T15:13:28Z")

</div>

Sounds like you have a single quote somewhere missing (or too many).  
Perhaps one of the variables you concatenate in the query have a single quote in their value.  
Can you do an output of that mySql variable ?

---

<div class="post-metadata">

**Author:** ![rgerz](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@rgerz](https://forum.servoy.com/u/rgerz)\
**Post date:** [July 23, 2011, 3:16pm UTC](https://forum.servoy.com/t/sql-error/14342/3 "2011-07-23T15:16:09Z")

</div>

```auto
MySQL: SELECT AVG(tbl_data_entry.mean) AS mean_avg, tbl_data_entry.test_code, tbl_data_entry.control_type, tbl_data_entry.control_level, tbl_data_entry.instrument_mfg_name, tbl_data_entry.instrument_model FROM qc.tbl_data_entry WHERE tbl_data_entry.test_code="APTT" AND tbl_data_entry.control_type="ROUTINE" AND tbl_data_entry.control_level="1" AND tbl_data_entry.instrument_mfg_name="Organon Teknika" AND tbl_data_entry.instrument_model="COAG-A-MATE RA4" GROUP BY tbl_data_entry.test_code, tbl_data_entry.control_type, tbl_data_entry.control_level, tbl_data_entry.instrument_mfg_name, tbl_data_entry.instrument_model

```

---

<div class="post-metadata">

**Author:** ![ROCLASI](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/roclasi/32/4371_2.png) [@ROCLASI](https://forum.servoy.com/u/ROCLASI)\
**Post date:** [July 23, 2011, 3:18pm UTC](https://forum.servoy.com/t/sql-error/14342/4 "2011-07-23T15:18:34Z")

</div>

Oh wait,  
You are using double quotes to quote values ? Double quotes are used to quote database objects. Single quotes are used to quote a string.  
Also it’s better to not concatenate a SQL string like that but to use prepared statements like so:

```auto
   var mySql = 'SELECT AVG(tbl_data_entry.mean) AS mean_avg, ' +
               'tbl_data_entry.test_code, ' +
               'tbl_data_entry.control_type, ' +
               'tbl_data_entry.control_level, ' +
               'tbl_data_entry.instrument_mfg_name, ' +
               'tbl_data_entry.instrument_model ' +
               'FROM tbl_data_entry ' +
               'WHERE ' +
               'tbl_data_entry.test_code=? AND ' +
               'tbl_data_entry.control_type=? AND ' +
               'tbl_data_entry.control_level=? AND ' +
               'tbl_data_entry.instrument_mfg_name=? AND ' +
               'tbl_data_entry.instrument_model=?' +
               'GROUP BY ' +
               'tbl_data_entry.test_code, ' +
               'tbl_data_entry.control_type, ' +
               'tbl_data_entry.control_level, ' +
               'tbl_data_entry.instrument_mfg_name, ' +
               'tbl_data_entry.instrument_model '

   var myDataSet = databaseManager.getDataSetByQuery('medlabqcpro_qc',mySql,[test_code,control_type,instrument_mfg_name,instrument_model],500)

```

---

<div class="post-metadata">

**Author:** ![rgerz](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@rgerz](https://forum.servoy.com/u/rgerz)\
**Post date:** [July 23, 2011, 3:28pm UTC](https://forum.servoy.com/t/sql-error/14342/5 "2011-07-23T15:28:18Z")

</div>

Thank you Robert!

That worked perfectly !!! ![:-)]( "Smile")
