Kapacitor: Wildcards in Kapacitor / where() not filtering correctly / sum(), mean() sending "0"

In a stream node I want to execute sum() and mean() actions, depending on the result of a where() node.
I have different, dynamically filled fields per measurement. So I am looking for a way to iterate through the fields, keep their original name and write it into the output.

with this .tick script:

dbrp "telegraf"."raw"

var data = stream
    |from()
    	.database('telegraf')
    	.retentionPolicy('raw')
        .measurement('')
        .groupBy(*)
    |window()
        .period(1m)
        .every(1m)
    
data
	|where(lambda: "cq" == 'a')
    |sum('') 
    |influxDBOut()
        .database('test_ds')
        .retentionPolicy('autogen')
        .measurement('')
        .precision('s')
    

data
	|where(lambda: "cq" == 'b')
    |mean('') 
    |influxDBOut()
        .database('test_ds')
        .retentionPolicy('autogen')
        .measurement('')
        .precision('s')

I get this output from kapacitor show :

DOT:
digraph st4 {
graph [throughput="0.00 points/s"];

stream0 [avg_exec_time_ns="0s" errors="0" working_cardinality="0" ];
stream0 -> from1 [processed="121612"];

from1 [avg_exec_time_ns="12.959µs" errors="0" working_cardinality="0" ];
from1 -> window2 [processed="121612"];

window2 [avg_exec_time_ns="1.131µs" errors="0" working_cardinality="23378" ];
window2 -> where6 [processed="15636"];
window2 -> where3 [processed="15636"];

where6 [avg_exec_time_ns="34.049µs" errors="7996" working_cardinality="15636" ];
where6 -> mean7 [processed="15636"];

mean7 [avg_exec_time_ns="13.119µs" errors="10482" working_cardinality="15636" ];
mean7 -> influxdb_out8 [processed="0"];

influxdb_out8 [avg_exec_time_ns="0s" errors="0" points_written="0" working_cardinality="0" write_errors="0" ];

where3 [avg_exec_time_ns="8.816µs" errors="7996" working_cardinality="15636" ];
where3 -> sum4 [processed="15636"];

sum4 [avg_exec_time_ns="67.111µs" errors="28558" working_cardinality="15636" ];
sum4 -> influxdb_out5 [processed="15636"];

influxdb_out5 [avg_exec_time_ns="19.417µs" errors="0" points_written="15636" working_cardinality="0" write_errors="0" ];
}

Although I get errors in mean7 and sum4 I can see all my measurements in test_ds with the same name they have in the telegraf database.

PROBLEM 1: all fields have the name sum, even the ones that have a “cq” = ‘b’ value.
PROBLEM 2: the value of the field sum is 0 for every point. I can confirm, that “data” contains the correct values which are over 0.

It seems like the where() node doesn’t filter correctly on the tags and the sum() node plainly sends 0.

I don’t know if the syntax I used in the .tick script is actually efficient. Any help on solving the problems and making the .tick as efficient as possible is highly appreciated.