Hello,
I got many support from @scott to be able to build my final query.
That’s my first one, it’s a good opportunity to get minimum know-how.
I see in my calculation that my float number has more than 2 decimals.
I see many tricks to be able to keep only 2 decimals but my syntax looks not good.
Can you confirm to me what should be the formula to be able to get only 2 decimals
below exemple formula of my query as an exemple
|> map(fn: (r) => ({ r with BRHCJB_Conso: if exists r.BBRHCJB_Max then (r.BBRHCJB_Max - r.BBRHCJB_Min) / 1000.0 else 0.0}))
See my full Query if it can help somebody to get cumulative Tip & tricks provided by @scott
data = () =>
from(bucket: "DB_Tele")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(
fn: (r) =>
r["_measurement"] == "BBRHCJB" or r["_measurement"] == "BBRHCJR" or r["_measurement"] == "BBRHCJW"
or
r["_measurement"] == "BBRHPJB" or r["_measurement"] == "BBRHPJR" or r["_measurement"] == "BBRHPJW"
or
r["_measurement"] == "HCHC" or r["_measurement"] == "HCHP",
)
|> filter(fn: (r) => r["_field"] == "Compteur")
max = data()
|> aggregateWindow(every: 1d, fn: max, createEmpty: false)
//|> fill(value: 0.0)
|> pivot(rowKey: ["_time"], columnKey: ["_measurement"], valueColumn: "_value")
|> rename(
fn: (column) => {
columnRegex = /HCHC|HCHP|BBRHCJB|BBRHCJW|BBRHCJR|BBRHPJB|BBRHPJW|BBRHPJR/
columnName = if column =~ columnRegex then "${column}_Max" else column
return columnName
},
)
min = data()
|> aggregateWindow(every: 1d, fn: min, createEmpty: false)
//|> fill(value: 0.0)
|> pivot(rowKey: ["_time"], columnKey: ["_measurement"], valueColumn: "_value")
|> rename(
fn: (column) => {
columnRegex = /HCHC|HCHP|BBRHCJB|BBRHCJW|BBRHCJR|BBRHPJB|BBRHPJW|BBRHPJR/
columnName = if column =~ columnRegex then "${column}_Min" else column
return columnName
},
)
join(tables: {min: min, max: max}, on: ["_time"])
|> drop(columns: ["_field_max","_field_min","_start_max","_start_min","_stop_max","_stop_min"])
|> map(fn: (r) => ({ r with BRHCJB_Conso: if exists r.BBRHCJB_Max then (r.BBRHCJB_Max - r.BBRHCJB_Min) / 1000.0 else 0.0}))
|> map(fn: (r) => ({ r with BRHPJB_Conso: if exists r.BBRHPJB_Max then (r.BBRHPJB_Max - r.BBRHPJB_Min) / 1000.0 else 0.0}))
|> map(fn: (r) => ({ r with BRHCJW_Conso: if exists r.BBRHCJW_Max then (r.BBRHCJW_Max - r.BBRHCJW_Min) / 1000.0 else 0.0}))
|> map(fn: (r) => ({ r with BRHPJW_Conso: if exists r.BBRHPJW_Max then (r.BBRHPJW_Max - r.BBRHPJW_Min) / 1000.0 else 0.0}))
|> map(fn: (r) => ({ r with BRHCJR_Conso: if exists r.BBRHCJR_Max then (r.BBRHCJR_Max - r.BBRHCJR_Min) / 1000.0 else 0.0}))
|> map(fn: (r) => ({ r with BRHPJR_Conso: if exists r.BBRHPJR_Max then (r.BBRHPJR_Max - r.BBRHPJR_Min) / 1000.0 else 0.0}))
|> map(fn: (r) => ({ r with HCHC_Conso: if exists r.HCHC_Max then (r.HCHC_Max - r.HCHC_Min) / 1000.0 else 0.0}))
|> map(fn: (r) => ({ r with HCHP_Conso: if exists r.HCHP_Max then (r.HCHP_Max - r.HCHP_Min) / 1000.0 else 0.0}))
|> map(fn: (r) => ({ r with STAT_TODAY_HP: r.BRHPJB_Conso + r.BRHPJW_Conso + r.BRHPJR_Conso + r.HCHP_Conso}))
|> map(fn: (r) => ({ r with STAT_TODAY_HC: r.BRHCJB_Conso + r.BRHCJW_Conso + r.BRHCJR_Conso + r.HCHC_Conso}))
|> map(fn: (r) => ({ r with STAT_TODAY: r.STAT_TODAY_HP + r.STAT_TODAY_HC}))
|> timeShift(duration: -1s, columns: ["_time"])
|> truncateTimeColumn(unit: 1d)
|> keep(fn: (column) => column =~ /_Conso|STAT_TODAY|_time/)