Influxdb - Subtracting value from previous row, group by time - influxdb

Is it possible to get individual data from cumulative?
Output of the following query is
SELECT mean("value") FROM "statsd_value" WHERE "type_instance" = 'counts' AND time > now() - 5m GROUP BY time(10s) fill(none)
TimeStamp Value
1463393810 0
1463393820 10
1463393830 23
1463393840 34
1463393850 67
1463393860 90
1463393870 104
Basically, the above data is cumulative data, I want to get individual data from that similar to this
TimeStamp Value
1463393820 10
1463393830 13
1463393840 11
1463393850 33
1463393860 23
1463393870 14
Is it possible to form query to get data in this way?

InfluxQL provides a difference function that will give you the functionality that you're looking for.
The query would look like this:
SELECT difference(mean("value")) FROM "statsd_value" WHERE "type_instance" = 'counts' AND time > now() - 5m GROUP BY time(10s) fill(none)
TimeStamp Value
1463393820 10
1463393830 13
1463393840 11
1463393850 33
1463393860 23
1463393870 14

Related

List unique dates and add line at the beginning of a new month

I have long (multiple thousand lines and growing) list of data in Sheets which have a date and additional columns with data. Here's a simplified example of this list (=TAB1):
Date Number Product-ID
02.09.2021 123 1
02.09.2021 2 1
01.09.2021 15 1
01.09.2021 675 2
01.09.2021 45 2
01.09.2021 52 1
31.08.2021 2 1
31.08.2021 78 1
31.08.2021 44 1
31.08.2021 964 2
30.08.2021 1 2
29.08.2021 ...
...
Three remarks:
The date is formatted to European standard DD.MM.YYYY
There definitely is more than one line per day per product (could be a big number depending on the day)
(for the formulas below) In the European standard Sheets uses ; instead of , as in =IF(A;B;C)
In a different tab (=TAB2), I want to add up all the numbers for a unique date for Product-ID 1. So far I've done it like this:
Date Sum (if Product-ID=1)
=UNIQUE('TAB1'!A2:A) =ARRAYFORMULA(SUMIF('TAB1'!A:A&'TAB1'!C:C;A2:A&"1";'TAB1'!B:B))
02.09.2021 125
01.09.2021 67
31.08.2021 124
30.08.2021 1
29.08.2021 ...
...
This works fine so far. Here's what I want to do now:
For every month (here: August and September 2021) I need an additional line above the current date (in this case: above 02.09.2021) AND above a completed month to sum over the whole month for column B. Here's how it should look like:
Date Sum (if Product-ID=1)
September 2021 192
02.09.2021 125
01.09.2021 67
August 2021 125
31.08.2021 124
30.08.2021 1
29.08.2021 ...
Of course, the line for the next day (03.09.2021) should be added above 02.09.2021 and below the sum for the month when it's automatically added to TAB1 on the next day.
I tried to play around with s.th. like =IF(DAY(UNIQUE('TAB1'!A2:A))=1;...;...) but didn't get far.
Is there anyone with an idea how to realize s.th. like this?
You want to learn about QUERY().
in cell A1 of an empty tab.
=QUERY('TAB1'!A2:C,"select A,SUM(B) where C = 1 group by A")
it makes a very big difference whether your product ids are text or numbers. the above was written as if they are numbers, but you might have just been simplifying. If they are text you would write it like this:
=QUERY('TAB1'!A2:C,"select A,SUM(B) where C = '1XYZ' group by A")
note the single quotes.
if the IDs are a MIX of text and letters then you need to force them all to text values in the original data by highlighting the IDs column and choosing Format>Number>Plain Text from the menu bar.
UPDATE:
I understand the requirements better now for intermixing a cumulative month total into the output. This may work.
=ARRAYFORMULA({QUERY({EOMONTH('TAB1'!A2:A,0),'TAB1'!B2:C},"select 'Total',Col1,SUM(Col2) where Col3 = 1 group by 'Total',Col1 label 'Total''',SUM(Col2)''",0);QUERY('TAB1'!A2:C,"select '',A,SUM(B) where C = 1 group by '',A label '''',SUM(B)''",0)},"order by Col2,Col1",0))

InfluxDB get list of changes

I would like to get a result such as the following:
name from_value to_value at
tag A 10 15 2019-02-11 16:00
tag B 1 2 2019-02-11 16:00
tag A 15 20 2019-02-11 16:05
tag B 2 3 2019-02-11 16:05
tag A 20 25 2019-02-11 16:10
tag B 3 4 2019-02-11 16:10
basically a column "from_value" (previous value current point) and a column "to_value" (current value current point).
To select only the current point value I do:
SELECT value FROM data WHERE "name"='tag A'
What if I wanted to select also the previous value?
SELECT prev(value) AS "from_value", value AS "to_value" FROM data WHERE "name"='tag A'
Can I do something like the above or I need to always save the previous value every time for every new point?
With group by time you can use last() and difference() functions to get value changes per time interval.
SELECT LAST(value)-DIFFERENCE(LAST(value)) as FromValue, LAST(value) as ToValue
FROM demo where time > 1549983975150000000
GROUP BY time(10ms),tagA FILL(none)
name: demo
tags: tagA=1
time FromValue ToValue
---- --------- -------
1549984410470000000 10
1549984421820000000 10 15
1549984431180000000 15 17
1549984436350000000 17 10
1549984753810000000 10 10
SELECT * FROM demo
name: demo
time tagA value
---- ---- -----
1549984410475859753 1 10
1549984421827992234 1 15
1549984431180379398 1 17
1549984436356232522 1 10
1549984753817094214 1 10

query result in set of interval ranges in postgresql(rails)

I have a timestamp column for which i have to calculate the time difference and divide it into certain set of intervals
for time difference in hours i have written this query
result = ActiveRecord::Base.connection.exec_query("SELECT id,(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - image_retouch_items.created_at)/3600)::INTEGER AS latency FROM image_retouch_items WHERE status= 0;");
The result of my query is
"id" "latency"
104 5928
106 5917
158 5751
162 5736
95 5940
85 5950
How to get result as set of intervals(hours),like for row for which time difference lie between the range of 0-24 hr increment the count .
i.e.
interval count
0-24 2
24-48 3
48-72 0
How to get that in single query

Sliding window aggregate Big Query 15 minute aggregation

I have a table like this
Row time viewCount
1 00:00:00 31
2 00:00:01 44
3 00:00:02 78
4 00:00:03 71
5 00:00:04 72
6 00:00:05 73
7 00:00:06 64
8 00:00:07 70
I would like to aggregate this into
Row time viewCount
1 00:00:00 31
2 00:15:00 445
3 00:30:00 700
4 00:45:00 500
5 01:00:04 121
6 01:15:00 475
.
.
.
Please help. Thanks in advance
Supposing that you actually have a TIMESTAMP column, you can use an approach like this:
#standardSQL
SELECT
TIMESTAMP_SECONDS(
UNIX_SECONDS(timestamp) -
MOD(UNIX_SECONDS(timestamp), 15 * 60)
) AS time,
SUM(viewCount) AS viewCount
FROM `project.dataset.table`
GROUP BY time;
It relies on conversion to and from Unix seconds in order to compute the 15 minute intervals. Note that it will not produce a row with a zero count for an empty 15 minute interval unlike Mikhail's solution, however (it's not clear if this is important to you).
Below is for BigQuery Standard SQL
Note: you provided simplified example of your data and below follows it - so instead of each 15 minutes aggregation, it uses each 2 sec aggregation. This is for you to be able to easy test / play with it. It is easily can be adjusted to 15 minutes by changing SECOND to MINUTE in 3 places and 2 to 15 in 3 places. Also this example uses TIME data type for time field as it is in your example so it is limited to just 24 hour period - most likely in your real data you have DATETIME or TIMESTAMP. In this case you will also need to replace all TIME_* functions with respective DATETIME_* or TIMESTAMP_* functions
So, finally - the query is:
#standardSQL
WITH `project.dataset.table` AS (
SELECT TIME '00:00:00' time, 31 viewCount UNION ALL
SELECT TIME '00:00:01', 44 UNION ALL
SELECT TIME '00:00:02', 78 UNION ALL
SELECT TIME '00:00:03', 71 UNION ALL
SELECT TIME '00:00:04', 72 UNION ALL
SELECT TIME '00:00:05', 73 UNION ALL
SELECT TIME '00:00:06', 64 UNION ALL
SELECT TIME '00:00:07', 70
),
period AS (
SELECT MIN(time) min_time, MAX(time) max_time, TIME_DIFF(MAX(time), MIN(time), SECOND) diff
FROM `project.dataset.table`
),
checkpoints AS (
SELECT TIME_ADD(min_time, INTERVAL step SECOND) start_time, TIME_ADD(min_time, INTERVAL step + 2 SECOND) end_time
FROM period, UNNEST(GENERATE_ARRAY(0, diff + 2, 2)) step
)
SELECT start_time time, SUM(viewCount) viewCount
FROM checkpoints c
JOIN `project.dataset.table` t
ON t.time >= c.start_time AND t.time < c.end_time
GROUP BY start_time
ORDER BY start_time, time
and result is:
Row time viewCount
1 00:00:00 75
2 00:00:02 149
3 00:00:04 145
4 00:00:06 134

Writing SQL query with a date range using postgresql and ruby on rails

I am having problem writing a sql statement with the following tables.
I have two tables one is meter another one is meter_info.
meter table has below columns
id, name, created_at
and meter_info has bellow columns
id, voltage, meter_id, created_at
data is being saved continuously to meter_info table.
I want to write a query that will take a date range and give me exactly one meter info within each date for each of the meter.
so let's say I have three meters in my meter table.
id name created_at
1 meter-a 2017-10-10
2 meter-b 2017-10-11
and in my meter table i have alot of data
id voltage created_at meter_id
1 15 2017-10-10 1
2 16 2017-10-10 1
3 14 2017-10-10 2
4 15 2017-10-10 2
5 13 2017-10-11 1
6 11 2017-10-11 1
7 13 2017-10-11 2
8 12 2017-10-11 2
Now I want to write a query that will take a date range parameter and out put like bellow(data range is 2017-10-10to 2017-10-11)
created_at meter_id voltage
2017-10-10 1 16
2017-10-10 2 15
2017-10-11 1 11
2017-10-11 2 12
so I want the last record of each meter within the date.
I don't know how to write the sql query.
I am using Postgresql and Ruby on Rails.
Thanks a ton in advance.
You must use DISTINCT ON clause.
SELECT DISTINCT ON (created_at, meter_id) created_at, meter_id, voltage
FROM meter_info
WHERE created_at BETWEEN '2017-10-10' AND '2017-10-11'
ORDER BY created_at, meter_id, id DESC
;
If you want to use rails active record you can do it this way:
start_date = Date.new 2017, 10, 10
end_date = Date.new 2017, 10, 11
MeterInfo.select("DISTINCT ON (created_at::date, meter_id) * ").where(created_at: start_date..end_date).order("created_at::date, meter_id, id DESC")
Note that I'm assuming your 'created_at' column is a datetime and thus need to be converted to a date for grouping using postgresql ::date.

Resources