Hi guys got a SQL stmt in python with an output of values from measurements. Each Measure has a unique id and timestamp. My output at the moment looks like this. Pythoncode for the Output:
arrayvals = []
for row in result:
arrayvals.append({
'request_ts': row[0],
'identifier': row[1],
'ts': row[2],
'value': row[5],
})
return (arrayvals)
OutPut:
{
"result_array": [
{
"identifier": "1",
"request_ts": "Mon, 22 Feb 2021 08:43:03 GMT",
"ts": "Wed, 17 Feb 2021 12:12:00 GMT",
"value": 17.1
},
{
"identifier": "1",
"request_ts": "Mon, 22 Feb 2021 08:43:03 GMT",
"ts": "Wed, 17 Feb 2021 12:12:00 GMT",
"value": 18.0
},
....
What i want is for every id only 1 output with the values in a list. I have tried to relize this with fetchall() but now it get all outputs with all values like this: Pyhton code:
result2 = result.fetchall()
for row in result:
arrayvals.append({
'request_ts': row[0],
'identifier': row[1],
'ts': row[2],
'value': [i[5] for i in result2],
})
Output:
"result_array": [
{
"identifier": "01",
"request_ts": "Mon, 22 Feb 2021 08:46:48 GMT",
"ts": "Wed, 17 Feb 2021 12:12:00 GMT",
"value": [
17.1,
18.0,
2005.0,
17000.0,
1234.0
]
}
]
But i want it like this:
"result_array": [
{
"identifier": "01",
"request_ts": "Mon, 22 Feb 2021 08:46:48 GMT",
"ts": "Wed, 17 Feb 2021 12:12:00 GMT",
"value": [
17.1,
18.0
]
}
{
"identifier": "02",
"request_ts": "Mon, 22 Feb 2021 08:46:48 GMT",
"ts": "Wed, 17 Feb 2021 12:12:00 GMT",
"value": [
2005.0,
17000.0,
1234.0
]
}
]
Is it possible to "combine" the id´s, to generate the last output? Nearly forgot my SQL looks like this:
SELECT
NOW() AS request_ts,
I.name AS identifier,
AI.ts,
AD.array_info_id,
AD.index,
AD.value AS measures
FROM
machine M
INNER JOIN identifier I
ON
I.machine_id = M.id
INNER JOIN PDA_ArrayInfo AI
ON
I.id = AI.identifier_id
INNER JOIN PDA_ArrayData AD
ON
AD.array_info_id = AI.id
WHERE
M.serial = :machine_serial_arg
From the answer of Masklinn:
for request_ts, identifier, ts, array_id, index, measures in result2.fetchall():
vals = vals_dict.get(array_id)
if vals is None:
# add record to index
values = vals_dict[array_id] = {
'request_ts': request_ts,
'identifier': identifier,
'ts': ts,
'value': [measures],
}
# adds the same record to the relust list
arrayvals.append(values)
else:
# look up the record in the index
vals['value'].append(measures)
return(values)
changed 2 things: 'value': [measures], vals['value'].append(measures)
now its working Thank you all