@knalla66 Flux in InfluxDB 1.x is read-only, so you aren’t able to write data back to InfluxDB with Flux (in 1.x). @FixTestRepeat is correct. Flux has to pull the entire queried dataset into memory to perform the pivot and the join, both of which are very expensive operations. With the nature of your data and the operation you’re trying to perform, I don’t know that the query will be able to complete on large datasets.
If you were using InfluxDB Cloud or InfluxDB 2.0 OSS, I’d suggest creating a Flux task that performs this operation on new data at regular intervals and stores the transformed data. I don’t know that there is a feasible solution with InfluxDB 1.8.1.