SQL Error

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:

	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:

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?

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 ?

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

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:

   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)

Thank you Robert!

That worked perfectly !!! :-)