I have a query to influxdb, something like:
SELECT last("Shop1.balance")+last("Shop2.balance")+last("Shop2.balance") + last("Shop2.balance") FROM "balances" WHERE $timeFilter GROUP BY time($__interval) fill(previous)
where i am receiving with graphana a total balance for all shops . It works fine until I add a new shop with a new data: a new shop balance can be null for some interval in history and whole calculated value will be null. I can not reorganize my database but may be I can change my query to receive date where null interval will be treated as zero in a total sum.
This works as you've asked:
SELECT last("Shop1.balance") + last("Shop2.balance") + last("Shop3.balance)
FROM "balances"
WHERE $timeFilter
GROUP BY time($__interval) fill(0)
Demo
select last(balance1) as last_balance_1, last(balance2) as last_balance_2
from balance
where time > '2018-03-07T10:44:14.0000000Z' and time < '2018-03-07T10:44:52.0000000Z'
group by time(7s)
Result:
name: balance
time last_balance_1 last_balance_2
---- -------------- --------------
2018-03-07T10:44:13Z 1 2
2018-03-07T10:44:20Z 2 3
2018-03-07T10:44:27Z 4
2018-03-07T10:44:34Z 5 6
2018-03-07T10:44:41Z 7
2018-03-07T10:44:48Z 8 9
select last(balance1) as last_balance_1, last(balance2) as last_balance_2
from balance
where time > '2018-03-07T10:44:14.0000000Z' and time < '2018-03-07T10:44:52.0000000Z'
group by time(7s) fill(0)
Result:
name: balance
time last_balance_1 last_balance_2
---- -------------- --------------
2018-03-07T10:44:13Z 1 2
2018-03-07T10:44:20Z 2 3
2018-03-07T10:44:27Z 4 0
2018-03-07T10:44:34Z 5 6
2018-03-07T10:44:41Z 7 0
2018-03-07T10:44:48Z 8 9
Related
Let's say, for simplicity sake, I have the following table:
id amount p_id date
------------------------------------------------
1 5 1 2020-01-01T01:00:00
2 10 1 2020-01-01T01:10:00
3 15 2 2020-01-01T01:20:00
4 10 3 2020-01-01T03:30:00
5 10 4 2020-01-01T03:50:00
6 20 1 2020-01-01T03:40:00
Here's a sample response I want:
{
"2020-01-01T01:00:00": 25, -- this is from adding records with ids: 2 and 3
"2020-01-01T03:00:00": 55 -- this is from adding records with ids: 3,4,5 and 6
}
I want to get the total (sum(amount)) of all unique p_id's grouped by the hour.
The row chosen per p_id is the one with the latest date. So for example, the first value in the response above doesn't include id 1 because the record with id 2 has the same p_id and the date on that row is later.
The one tricky thing is I want to include the summation of all the amount per p_id if their date is before the hour presented. So for example, in the second value of the response (with key "2020-01-01T03:00:00"), even though id 3 has a timestamp in a different hour, it's the latest for that p_id 2 and therefore gets included in the sum for "2020-01-01T03:00:00". But the row with id 6 overrides id 2 with the same p_id 1.
In other words: always take the latest amount for each p_id so far, and compute the sum for every distinct hour found in the table.
Create a CTE that includes row_number() over (partition by p_id, date_trunc('hour',"date") order by "date" desc) as pid_hr_seq
Then write your query against that CTE with where pid_hr_seq = 1.
I want to query the sum per minute from the result obtained from another query that calculates the difference between subsequent values.
select sum(ph1), sum(ph2), sum(ph2) from (select
non_negative_difference(day_chan1) as ph1,
non_negative_difference(day_chan2) as ph2,
non_negative_difference(day_chan3) as ph3
from electricity)
group by time(1m) tz('Europe/Dublin')
For example if I get the following from the suqbquery
time ph1 ph2 ph3
---- --- --- ---
2017-04-02T14:40:38Z 0 0 2
2017-04-02T14:41:38Z 1 1 1
2017-04-02T14:41:39Z 0 0 2
2017-04-02T14:42:38Z 1 1 1
2017-04-02T14:42:39Z 0 1 2
I want to sum them up into
time ph1 ph2 ph3
---- --- --- ---
2017-04-02T14:40:00Z 0 0 2
2017-04-02T14:41:00Z 1 1 3
2017-04-02T14:42:00Z 1 2 3
but what I get from the query is aggregate function required inside the call to non_negative_difference but if I do the sub query on its own, it returns the results
I was also looking a long time for this and I finally found the solution:
select sum(ph1), sum(ph2), sum(ph2) from (select
This is right. Now we want to add an aggregate function inside the non_negative_difference call (as the error also indicates). I assume you want to sum everything.
non_negative_difference(sum(day_chan1)) as ph1,
non_negative_difference(sum(day_chan2)) as ph2,
non_negative_difference(sum(day_chan3)) as ph3
from electricity
Now if we don't add the following line the group by function of the inside query will also be 1m. We don't want this since if a value is missing the way influx calculates sum this will result in a very large differnce. So we group this subquery by the smallest interval you have (e.g. 1s)
group by time(1s))
Finally you can group the outer query by the interval you would like the values to be added together.
group by time(1m) tz('Europe/Dublin')
I am trying to implement a "time spent on platform" metric, grouped by user and day.
My test data has 15 events for each of two users, and those 15 events are split among three days. However, the five events for a particular user/day combo all happen at exactly the same moment, so for the purposes of my "time spent" calculation they should only be counted as a single "time unit". I'm defining a "time unit" as a minute that contains at least event for a user.
Here is my attempt so far:
SELECT SUM(x) FROM (SELECT COUNT(score_value) as x FROM user_scores GROUP BY time(1m),user_id) GROUP BY time(1d),user_id
name: user_scores
tags: user_id=123
time sum
---- ---
1518134400000000000 5
1518220800000000000 5
1518307200000000000 5
1518393600000000000
name: user_scores
tags: user_id=456
time sum
---- ---
1518134400000000000 5
1518220800000000000 5
1518307200000000000 5
I can see how this is the expected result set, but it is not the data I'm looking for. Since each of the five events for a single user/day combo happen at exactly the same minute, the sum values in the results should all be 1.
So, I need a way to channge SELECT COUNT(score_value) as x FROM user_scores GROUP BY time(1m),user_id into something that returns 0 or 1 depending on if there are any events occuring in that minute
I figured it out, what works is as follows:
SELECT COUNT(x) FROM (SELECT COUNT(score_value) as x FROM user_scores GROUP BY time(1m),user_id) WHERE x > 0 GROUP BY time(1d),user_id
Basically I changed the outer SELECT SUM(x) to SELECT COUNT(x) and added the where x > 0.
Is there anyway that i can get the count of total number of results / points / records in a group by query result?
> SELECT COUNT("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' GROUP BY time(12m)
name: h2o_feet
--------------
time count
2015-08-18T00:00:00Z 2
2015-08-18T00:12:00Z 2
2015-08-18T00:24:00Z 2
I expect the count as 3 in this case. Even though I can calculate the number of results using the time period and interval (12m) here, I would like to know whether it is possible to do so with a query to database.
You can use Multiple Aggregates in single query.
Using your example add a select count(*) from (<inner query>):
> SELECT COUNT(*) FROM (SELECT COUNT("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' GROUP BY time(12m))
name: h2o_feet
--------------
time count_count
1970-01-01T00:00:00Z 3
However if you had a situation in which the grouping by returns empty rows, they will not be counted.
For example, counting over the below table will result in a count of 2 rather than 3:
name: h2o_feet
--------------
time count
2015-08-18T00:00:00Z 2
2015-08-18T00:12:00Z
2015-08-18T00:24:00Z 2
To include empty rows in your count you will need to add fill(1) to your query like this:
> SELECT COUNT(*) FROM (SELECT COUNT("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' GROUP BY time(12m) fill(1))
You will need to do some manual work. Run it directly,
$ influx -execute "select * from measurement_name" -database="db_name" | wc -l
This will return 4 more than the actual values.
Here is an example,
luvpreet#DHARI-Inspiron-3542:~/www$ influx -execute "select * from yprices" -database="vehicles" | wc -l
5
luvpreet#DHARI-Inspiron-3542:~/www$ influx -execute "select * from yprices" -database="vehicles"
name: yprices
time price
---- -----
1493626629063286219 2
luvpreet#DHARI-Inspiron-3542:~/www$
So, I think now you know why subtract 4 from the value.
If I have a table with field date_completed. I would like to calculate the difference between this date and the time now in months
(date_completed.year * 12) + date_completed.month - (Date.today.year * 12) + Date.today.month
and then group the results based on the number of months e.g.
No Months => Count
1 => 10
2 => 5
3 => 6
etc.
Is it possible to calculate the difference between two dates in a database and then group and count the results?
A Postgre-SQL specific SQL query will be something like below - it makes use of extract and age method of PostgreSQL(Refer Documentation)
select extract(year from age(date_completed)) * 12 +
extract(month from age(date_completed)) as months,
count(*) from learn group by months;
Sample output:
"months"|"count"
2| 2
3| 1
4| 1
I made use of a table learn above, you need to change it to table name you have.