# Math with query results

**URL:** <https://community.influxdata.com/t/math-with-query-results/19514>\
**Category:** Welcome & Getting Started\
**Tags:** query, flux\
**Created:** [April 21, 2021, 8:18am UTC](https://community.influxdata.com/t/math-with-query-results/19514 "2021-04-21T08:18:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![strittmm](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/strittmm/32/7159_2.png) [@strittmm](https://community.influxdata.com/u/strittmm)\
**Post date:** [April 21, 2021, 8:18am UTC](https://community.influxdata.com/t/math-with-query-results/19514/1 "2021-04-21T08:18:20Z")

</div>

Hello I am trying to do some math with my query results using Flux. Everything works fine until I try to calculate something with the results. I would like to divide divide `operational` by `nfaults`:

```auto
// Get all the data from the bucket filtered for FAULT_LASER
data = from(bucket: "plcview_4/autogen")
  |> range(start: 2021-01-29T00:00:00.000Z, stop: now()) // regular time range filter
  |> filter(fn: (r) => r._measurement == "FAULT_LASER") // filter for the measurement
  |> elapsed(unit: 1ms, timeColumn: "_time", columnName: "duration") // calculate time difference between rows
  |> yield(name: "data")
// returns data tables for every unique set of tags (workplace and equipmentNumber)

// Filter for all "No-Fault-Values" and sum their durations
operational = data
  |> filter(fn: (r) => r._value == 0) // filter for all rows where FAULT_LASER = 0 --> No Faults
  |> group() // group all data tables together
  |> sum(column: "duration") // sum all the durations from all data tables 
  |> yield(name: "operational")

// Count the number of faults
nfaults = data
  |> filter(fn: (r) => r._value == 1)	// filter for all rows where FAULT_LASER = 1 --> Faults
  |> group() // group all data tables together
  |> count() // count the number of records
  |> yield(name: "nfaults")
  
// Calculate MTBF
mtbf = operational / nfaults

```

Why do my calculations not work?

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [April 23, 2021, 6:46pm UTC](https://community.influxdata.com/t/math-with-query-results/19514/2 "2021-04-23T18:46:01Z")

</div>

Hello @strittmm,  
operational and nfaults are table streams, not individual values so you can’t simply divide them.  
You would need to perform a join and then a map().

> **[join() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/join/)**
>
> join() merges two streams of tables into a single output stream based on columns with equal values. Null values are not considered equal when comparing column values. The resulting schema is the union of the input schemas. The resulting group key is...

> **[map() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/map/)**
>
> map() iterates over and applies a function to input rows.

```auto
join(tables: {nfaults: nfaults, operational: operational}, on: ["_time"], method: "inner")
|> map(fn: (r) => ({ _value: r._value_operational / r._value_nfaults }))

```

---

<div class="post-metadata">

**Author:** ![strittmm](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/strittmm/32/7159_2.png) [@strittmm](https://community.influxdata.com/u/strittmm)\
**Post date:** [April 27, 2021, 6:57am UTC](https://community.influxdata.com/t/math-with-query-results/19514/3 "2021-04-27T06:57:20Z")

</div>

@Anaisdg thanks, but for some reason this did not work for me…  
Instead I did this and it works just how I intended:

```auto
mtbf = data
  |> reduce(
  	fn: (r, accumulator) => ({ 
    	operational: r.duration + accumulator.operational,
        nFaults: accumulator.nFaults + 1
    }),
    identity: { operational: 0, nFaults: 0 }
  )
  |> map(fn: (r) => ({ _value: r.operational/ r.nFaults }))

```
