Can anyone suggest a reason why my query does not work? I have attempted with .data and .dataArray. I have experimented with bizarre punctuation. I have experimented with differing formats of data. There is only an empty array produced.
Not to be Captain Obvious here, but does the array in fact contain a record where firstname is tom and not TOM or Tom (or Thomas)...you get my point, but can you share the data in the array? Or if not, what happens if you remove the where clause?
The query is working then. The firstname field doesn't have a tom - it has Tim, Michael, and Jonathan. If you change you where clause to where firstname = 'Tim' it should return a single record.
I think you may want to transform the initial data you are querying on because each key is an array - so as to have a better access to the field itself...using a map function...
Unfortunately 'select * from {{ contact_query.data.map(i=>i.lastname) }}' results in 'TypeError: contact_query.data.map is not a function' (lastname does not have an underscore divider).
This is doubly frustrating because my efforts so far are based on another community post...
No worries, I did attempt that for about 90 mins until I had a brainwave and implemented my inelegant workaround. For all practical purposes my problem is solved. It would have been nice to use the facilities as intended but I'm happy to stage a tactical retreat from this skirmish in the interests of continuing the wider campaign (i.e. hitting deadlines and getting paid).
if you're curious I believe the problem is with how the data you're working on is organized for 'where' to work. SQL queries are returned in columnar format, which isn't what WHERE is expecting.
what were given:
a single RECORD (we're using SQL so it's being consumed as a Record not an object) contact_query.data with 21+ properties all of type array with identical lengths and presumably identical sorting (ie: index 1 of lastname matches index 1 of firstname and so on)
what we're trying to do:
use the SQL 'WHERE' clause to select all items related to a lastname
what you're currently trying:
select * from {{ contact_query.data }} where lastname='Smith'
broken down:
select * from (1 Record) where lastname='Smith'
select ALL from (1 Record) where lastname='Smith'
select ALL from (1 Record) where ['Smith','Lastname1', 'Lastname2'] = 'Smith'
and that comparison is why it doesn't return anything. and if it did, it wouldn't have a Record to use Select * on, it would have a string or an index.
Solution ( use formatDataAsArray):
first we need to generate a separate object/record for each 'index'. so using index 0 from every property, we make obj0 and from using index1 we make another and so on. our goal is to go from:
//this is columnar format
{
lastname:['ln0', 'ln1', 'ln2'],
firstname:['fn0', 'fn1', fn2']
}
Retool provides 2 types of functions to help w SQL: formatDataAsArray() and formatDataAsObject(). for WHERE we need an array, so we use {{ formatDataAsArray(contact_query.data) }}
(TLDR;)
change
select * from {{ contact_query.data }}
where lastname='Smith'
to
select * from {{ formatDataAsArray(contact_query.data) }}
where UPPER(lastname) = UPPER('Smith')
note: I added upper so that the search is not case sensitive. otherwise a lastname of 'smith' while searching for 'Smith' won't be found.