How do I graph of change from previous value in grafana amazon-timestream? - grafana

My current query is
SELECT bin(time, 5m) AS bin, sum(measure_value::double) as bytesdropped FROM $__database.$__table
where measure_name='messurename' and $__timeFilter
group by bin(time, 5m) order by bin
unfortunately, the bytesdropped value is cumulative, for example a table of the data:
bin
bytesdropped cum
t0
0
t1
100
t2
100
t3
110
t4
110
t5
115
t6
115
when no bytes dropped, the the bytes dropped value does not report as zero, but instead reports as the previous value, IE t2 after t1 means no bytes dropped at t2 (100 bytes had dropped at t1)
how do I modify (or re-write completely) my query such that I only show the change between the current time and the last time. IE:
bin
bytesdropped
t0
0
t1
100
t2
0
t3
10
t4
0
t5
15
t6
0

Related

Unable to calculate compound interest in PostgreSQL

I have a table table1 which contains the details of any depositor like
Depositor
Deposit_Amount
Deposit_Date
Maturity_Date
Tenure
Rate
A
25000
2021-08-10
2022-08-10
12
10%
I have another table table2 which contains the interest due date as:
Interest_Due_Date
2021-09-30
2021-12-31
2022-03-31
2022-06-30
2022-08-10
My Code is:
with recursive recur (n, start_bal, days,principle,interest, end_bal) as
(
select sno,deposit_amount,rate,days,deposit_amount * (((rate::decimal(18,2))/100)/365)*days as interest, deposit_amount+(deposit_amount * (((rate::decimal(18,2))/100)/365)*days) as end_bal from (
SELECT
sno, COALESCE(DATE_PART('day', deposit_date::TIMESTAMP - lag(deposit_date::TIMESTAMP) over
(ORDER BY sno ASC rows BETWEEN UNBOUNDED PRECEDING AND CURRENT row)),0) AS
days, deposit_date, deposit_amount, rate
FROM
( SELECT
ROW_NUMBER () OVER (ORDER BY deposit_date) AS sno,
deposit_date,
deposit_amount,
rate
FROM
( SELECT
t1.deposit_date, t1.deposit_amount, t1.rate from table1 t1
UNION ALL
SELECT
t2.Interest_Due_Date AS idate, 0 as depo_amount, 0 as rate
FROM
table2 t2
ORDER BY
deposit_date) dep) calc) b where sno = 1 union all select b.sno, b.end_bal,b.days,b.prin_bal,(coalesce(a.end_bal,0)) * (((b.rate)/100)/365)*b.days as interest_NEW,
coalesce(a.end_bal,0)+ ((a.end_bal) * (((calc.rate)/100)/365)*calc.days) as end_bal_NEW
from b, recur as a
where calc.sno = a.n+1 ) select * from recur
"Every time when i try to execute the query its showing an error 'relation 'b' does not exist"
...
The result table should be
Deposit Amount
Date
Days
Interest
Total Amount
25000
2021-08-10
0
0
25000
0
2021-09-30
51
349.32
25349.32
0
2021-12-31
92
638.94
25988.26
0
2022-03-31
90
640.81
26629.06
0
2022-06-30
91
663.90
27292.97
0
2022-08-10
41
306.58
27599.54

Calculating a simple rate for "windows" within data

Battery % time charging
90 t1 yes
91 t2 yes
95 t3 no
89 t4 no
87 t5 no
80 t6 no
78 t7 yes
85 t8 yes
50 t9 no
40 t10 no
38 t11 no
20 t12 yes
I want to calculate battery depletion rate as :
change in battery / time taken
This should be calculated for ALL the windows when charging is 'no' (sandwiched in between 2 "yes"), and then the average of those rates should be taken.
So, for this dataset it should be:
95 - 80 / t6 - t3 = rate 1
50 - 38 / t11 - t9 = rate 2
average rate = ( rate 1 + rate 2 ) / 2
Please note there can be more than 2 windows of no's in the data
Here is my current code -
select ((max(battery_Percentage) - min (battery_Percentage)) / NULLIF(Extract(epoch FROM (max(time) - min(time))/3600),0)) as rate_of_battery_decline
from table
where
table.charging = 'no'
but this is not taking into account windows of no's in between the yes's as I want. Please help.
You have to separate the runs between the charging = 'yes' blocks:
with discharge_intervals as (
select battery_pct, tstamp,
sum((charging = 'yes')::int) over (order by tstamp) as ival_number,
charging = 'no' as keep
from cstats
), interval_rates as (
select ival_number,
(max(battery_pct) - min(battery_pct))
/ extract(epoch from max(tstamp) - min(tstamp)) as ival_rate
from discharge_intervals
where keep
group by ival_number
)
select avg(ival_rate)
from interval_rates;
Fiddle Here

"partition by" giving incorrect value

battery_pct tstamp charging phone_id
90 t1 yes 12
91 t2 yes 22
95 t3 no 22
89 t4 no 22
87 t5 no 22
80 t6 no 22
78 t7 yes 22
85 t8 yes 4
50 t9 no 4
40 t10 no 4
38 t11 no 4
20 t12 yes 4
I want to calculate battery depletion rate as : change in battery / time taken
This should be calculated for ALL the windows when charging is 'no' (sandwiched in between 2 "yes"), and then the average of those rates should be taken.
So, for this dataset it should be:
95 - 80 / t6 - t3 = rate for phone_id 22
50 - 38 / t11 - t9 = rate for phone_id 4
average rate = ( rate 1 + rate 2 ) / 2
Please note there can be more than one windows of no's for each phone_id in the data.
I have to find average rate across ALL phone id's. i.e. one value for average rate which encompasses all phones.
Here is my current code, it does not give any error, but is returning a value that is NOT plausible -
with discharge_intervals as (
select battery_pct, tstamp,
sum((charging = 'yes')::int) over (partition by phone_id order by tstamp) as ival_number,
charging = 'no' as keep
from dataset
), interval_rates as (
select ival_number,
(max(battery_pct) - min(battery_pct))
/ extract(epoch from max(tstamp) - min(tstamp)) as ival_rate
from discharge_intervals
where keep
group by ival_number
)
select avg(ival_rate)
from interval_rates;
Your interval_rates are calculated without grouping by phone, but should be. The ival_numbers are partitioned by phone_id, but that just means multiple phones will create rows with the same ival_number. You'll want to use
with discharge_intervals as (
select battery_pct, tstamp, phone_id,
-- ^^^^^^^^^
sum((charging = 'yes')::int) over (partition by phone_id order by tstamp) as ival_number,
charging = 'no' as keep
from dataset
), interval_rates as (
select (max(battery_pct) - min(battery_pct))
/ extract(epoch from max(tstamp) - min(tstamp)) as ival_rate
from discharge_intervals
where keep
group by phone_id, ival_number
-- ^^^^^^^^^
)
select avg(ival_rate)
from interval_rates;

SAS : row difference

I have to calculate date difference between first date at time = 0 and the dates after. I also have one variable = factor which has 2 categories : one ; two.
For example, here is one date :
A B TIME
10/11/2016 one T0
17/11/2016 two T0
05/01/2017 one T1
28/02/2017 two T1
06/07/2017 one T2
05/09/2017 two T2
I would like to calculate the difference between T0 and the dates for B="one" and B="two" in order to obtain :
DIFF
0
0
56
103
238
292
Calculating the diff as follows :
56 = T1-T0 for "one" = 05/01/2017 - 10/11/2016
103 = T1-T0 for "two" = 28/02/2017 - 17/11/2016
238 = T2-T0 for "one" = 06/07/2017 - 10/11/2016
292 = T2-T0 for "two" = 05/07/2017 - 17/11/2016
Could you help me do it in SAS?
Thanks a lot.
One way is to pull out the TIME='T0' records and merge them back with the other records.
First let's convert your table into a dataset.
data have ;
input b $ Time $ date :yymmdd.;
format date yymmdd10.;
cards;
one T0 2016-11-10
two T0 2016-11-17
one T1 2017-01-05
two T1 2017-02-28
one T2 2017-07-06
two T2 2017-09-05
;
Now let's re-order it so that we can merge by the grouping variable, B.
proc sort ;
by b time ;
run;
Here is a way to merge the data with itself.
data want ;
merge have(where=(time ne 'T0'))
have(keep=time b date rename=(time=time0 date=date0) where=(time0='T0'))
;
by b ;
diff = date - date0;
drop time0;
run;
Results:
Obs b Time date date0 diff
1 one T1 2017-01-05 2016-11-10 56
2 one T2 2017-07-06 2016-11-10 238
3 two T1 2017-02-28 2016-11-17 103
4 two T2 2017-09-05 2016-11-17 292
There are of course several ways to do this. Below are two alternatives. The first selects the first A for each B and merges this with the original data in a SQL-step. The second uses a DATA-step and by groups. The first A within each B is saved as firsttime, and retained so it can be used to calculate the difference.
data test;
input A ddmmyy10. #12 B $3.;
format A ddmmyy10.;
datalines;
10/11/2016 one
17/11/2016 two
05/01/2017 one
28/02/2017 two
06/07/2017 one
05/09/2017 two
;
/* Alt 1*/
proc sql;
create table test2 as
select t1.*, t1.A-t2.A as time
from test as t1 left join (select B, min(A) as A from test group by 1) as t2
on t1.B=t2.B
order by A;
/* Alt 2*/
proc sort data=test;
by B A;
run;
data test3;
set test;
by B;
retain firsttime;
if first.B then firsttime=A;
time=A-firsttime;
drop firsttime;
run;

Append columns to empty table - Q/KDB+

I'm pulling data from a source that returns tick data for stocks (timespan + float prices).
I need to build 1 table that has the tick data for each stock, while inserting new timespan index values for each one. Example:
AAPL:
t0 101.20
t3 102.10
GOOG:
t1 850.50
t2 860.10
Table:
AAPL GOOG
t0 101.20 NA
t1 NA 850.50
t2 NA 860.10
t3 102.10 NA
There would be many symbols, so I can't just manually type AAPL, GOOG etc.
While it would be possible to set up a table like you have described it would not be advisable. You would be better to set up a column to record each stock, sym in this case:
t sym price
-------------------------------------------
2018.02.05D14:11:09.241245000 AAPL 101.7808
2018.02.05D14:11:09.241246000 GOOG 103.0177
2018.02.05D14:11:09.241246000 AAPL 107.8503
2018.02.05D14:11:09.241247000 GOOG 105.3471