# Ratio of two values in Influx DB

Hello everybody,

I am quite new to this whole Influx DB / Flux topic and need to build a Grafana dashboard for a university project.

My database has a column _value where all the sensor values are saved and a column tag2 which is used as a remark to know what type of value it is. My sensor detects vehicles and sorts them into speed classes depending on how big the vehicle is. therefore I have 3 Speed classes (Truck, Car, Bike). It also includes the info if the car is moving from left to right (li > re) or right to left (re > li).

What I want to do now is calculate a sum of all the vehicles moving from left to right and a sum of all the vehicles moving from right to left, and then calculate the ratio between those two sums.

Unfortunately it won’t work for me with the code down below and I am frustrated because I just can’t find the right solution.

Would be awesome if someone could help me out here…

import “date”

today = date.truncate(t: now(), unit: 1d)

test2 = from(bucket: “Group3_Verkehrszähler”)
|> range(start: today, stop: now())
|> filter(fn: (r) => r[“_measurement”] == “KurzeSteige”)
|> filter(fn: (r) => r[“_field”] == “value”)
|> filter(fn: (r) => r[“tag2”] =~ /.*Objektanzahl.li > re./)
|> last()
|> map(fn: (r) => ({ test: r[“_value”] }))
|> keep(columns: [“test”])

test = from(bucket: “Group3_Verkehrszähler”)
|> range(start: today, stop: now())
|> filter(fn: (r) => r[“_measurement”] == “KurzeSteige”)
|> filter(fn: (r) => r[“_field”] == “value”)
|> filter(fn: (r) => r[“tag2”] =~ /.*Objektanzahl.re > li./)
|> last()
|> map(fn: (r) => ({ test: r[“_value”] }))
|> keep(columns: [“test”])

ergebnis = test / test2

ergebnis |> yield(name: “result2”)