How to average of last days data in influxdb - influxdb

I want to calculate the average of unit_price from entries of the last known day from my influxdb database.
Below shows you the last two days data I have (14 entries per day)
I have 5 different days data in total.
> select * from "variable" order by time desc limit 28
name: variable
time area_code area_name unit_price
---- --------- --------- ----------
2021-05-11T23:00:00Z P Northern_Scotland 18.4695
2021-05-11T23:00:00Z N Southern_Scotland 17.598
2021-05-11T23:00:00Z M Yorkshire 16.968
2021-05-11T23:00:00Z L South_Western_England 18.6795
2021-05-11T23:00:00Z K Southern_Wales 18.081
2021-05-11T23:00:00Z J South_Eastern_England 18.501
2021-05-11T23:00:00Z H Southern_England 17.5875
2021-05-11T23:00:00Z G North_Western_England 17.4615
2021-05-11T23:00:00Z F North_Eastern_England 17.262
2021-05-11T23:00:00Z E West_Midlands 17.6085
2021-05-11T23:00:00Z D Merseyside_and_Northern_Wales 19.4355
2021-05-11T23:00:00Z C London 17.4405
2021-05-11T23:00:00Z B East_Midlands 17.3565
2021-05-11T23:00:00Z A Eastern_England 17.871
2020-11-01T00:00:00Z P Northern_Scotland 17.073
2020-11-01T00:00:00Z N Southern_Scotland 16.2225
2020-11-01T00:00:00Z M Yorkshire 15.6135
2020-11-01T00:00:00Z L South_Western_England 17.094
2020-11-01T00:00:00Z K Southern_Wales 16.527
2020-11-01T00:00:00Z J South_Eastern_England 16.8945
2020-11-01T00:00:00Z H Southern_England 16.128
2020-11-01T00:00:00Z G North_Western_England 16.0125
2020-11-01T00:00:00Z F North_Eastern_England 15.7395
2020-11-01T00:00:00Z E West_Midlands 16.086
2020-11-01T00:00:00Z D Merseyside_and_Northern_Wales 17.8605
2020-11-01T00:00:00Z C London 15.897
2020-11-01T00:00:00Z B East_Midlands 15.8445
2020-11-01T00:00:00Z A Eastern_England 16.2855
As you can see here, limit 14 average shows the same result as not using limit at all.
So this mean command is averaging 'all' the data, not any limited data.
select mean(unit_price) from "variable" order by time desc limit 14
name: variable
time mean
---- ----
1970-01-01T00:00:00Z 16.2924375
> select mean(unit_price) from "variable"
name: variable
time mean
---- ----
1970-01-01T00:00:00Z 16.2924375
>
I have tried nested selects, but can’t seem to find how to get an average of the final 14 entries (or from the final date with data)
Any help would be very much appreciated.

Think I may have solved with after playing some more with nested queries.
> select mean(unit_price) from "variable" group by time(1d) fill(none)
name: variable
time mean
---- ----
2019-04-12T00:00:00Z 15.572249999999997
2020-01-15T00:00:00Z 15.340499999999997
2020-11-01T00:00:00Z 16.377
2021-05-11T00:00:00Z 17.880000000000003
> select last("mean") from (select mean(unit_price) from "variable" group by time(1d) fill(none))
name: variable
time last
---- ----
2021-05-11T00:00:00Z 17.880000000000003
>

Related

Get multi column range but only where specific column is not repeated

So, I have a sheet named "Calendar" and another sheet called "Stats".
Here's a sample of the "Calendar" sheet:
F
G
H
I
J
K
2023-01-27
Fri
11:30 PM
Family
Family Activity 1
YYY
2023-01-27
Fri
11:45 PM
Family
Family Activity 1
YYY
2023-01-28
Sat
12:00 AM
Family
Family Activity 1
YYY
2023-01-28
Sat
12:15 AM
Family
Family Activity 1
XY
2023-01-28
Sat
12:30 AM
Fun
Fun Activity 1
ABC
2023-01-28
Sat
12:45 AM
Fun
Fun Activity 1
ABC
2023-01-28
Sat
1:00 AM
Obligations
Obligations 1
AAA
2023-01-28
Sat
1:15 AM
Fun
Fun Activity 2
ZZZ
2023-01-28
Sat
1:30 AM
Fun
Fun Activity 2
ZZZ
2023-01-28
Sat
1:45 AM
Family
Family Activity 2
MMM
2023-01-28
Sat
2:00 AM
Family
Family Activity 2
MMM
Now, on the "Stats" sheet there's a date in cell B16. For this example, it's 2023-01-28.
What I want is that I can get the columns H, I, J, and K from "Calendar" where F equals the date specified in cell B16 of the "Stats" sheet.
The tricky part, where I'm having issues, is to only show the rows where the previous row isn't identical, resp. where I, J, and K aren't the exact same as the previous row, like this:
H
I
J
K
12:00 AM
Family
Family Activity 1
YYY
12:15 AM
Family
Family Activity 1
XY
12:30 AM
Fun
Fun Activity 1
ABC
1:00 AM
Obligations
Obligations 1
AAA
1:15 AM
Fun
Fun Activity 2
ZZZ
1:45 AM
Family
Family Activity 2
MMM
I'm not sure if it's comprehensive, if it isn't please let me know so I can clarify.
What I got so far is the following formula:
=QUERY(A:K,"select H,I,J,K where F = date '2023-01-28'")
This only works if I execute it in the "Calendar" sheet and the date isn't dependent of cell B16 of the "Stats" sheet. However, ideally I'd like place the formula into the "Stats" sheet.
you can try:
=FILTER(Calendar!H2:K,Calendar!F2:F=B16,{"";LAMBDA(z,MAKEARRAY(COUNTA(z),1,LAMBDA(r,c,IF(INDEX(z,r)=INDEX(z,r-1),1,))))(INDEX(Calendar!F3:F&Calendar!I3:I&Calendar!J3:J&Calendar!K3:K))}<>1)
If you can, you may add an auxiliary column in your raw data sheet. I'll say it's L, with this formula in L2:
=MAP(F2:F,I2:I,J2:J,K2:K,LAMBDA(fx,ix,jx,kx,IF(OR(fx<>OFFSET(fx,-1,0),ix<>OFFSET(ix,-1,0),jx<>OFFSET(jx,-1,0),kx<>OFFSET(kx,-1,0)),1,0)))
It checks if F,I,J and K are equal, and returns 1 or 0. Then you can do a QUERY like this:
=QUERY(A:L,"select H,I,J,K WHERE L = 1 AND F = date '"&TEXT(B16,"YYYY-MM-DD")&"'")
If you can't add the column you may do it like this joining all this in one formula:
=QUERY({Calendar!F:K,"";MAP(Calendar!F2:F,Calendar!I2:I,Calendar!J2:J,Calendar!K2:K,LAMBDA(fx,ix,jx,kx,IF(OR(fx<>OFFSET(fx,-1,0),ix<>OFFSET(ix,-1,0),jx<>OFFSET(jx,-1,0),kx<>OFFSET(kx,-1,0)),1,0)))},"select Col3,Col4,Col5,Col6 WHERE Col7 = 1 AND Col1 = date '"&TEXT(Stats!B16,"YYYY-MM-DD")&"'")
date is just a number. try:
=QUERY(Calendar!A:K, "select H,I,J,K where F = "&B16*1, )

Add two numbers up to one constant

I have a constant number X. I also have two numbers that add up to it. How can I make it so that if I change one number, the other number automatically changes so that it still adds up to X.
I have tried to take subtract the one number from X and add it to the other number, but instead I got two numbers in the thousands.
Assuming your constant value is 10, you can set this in a cell and make all your other calculations based on it.
For example, you can have cell C2 containing your constant, in this example, 10
Then in C4 you can have the number which you change, and the value of C5 will be equal to the value of the constant minus the value in C4.
You can then finally do your sum wherever you want, adding up the values of C4 and C5.
Here's an example Spreadsheet:
Untitiled spreadsheet ☆
File Edit View Insert Format Data Tools Extensions Help Last edit was 2 minutes ago
↶ ↷ 🖶 ⮷ | 100%⯆ | $ % .0 .00 123⯆ | Default(Ro... ⯆ | 10 ⯆ | B | I | S | A |⯐|☰
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
1
2
Contsant:
10
3
4
Number 1:
3
5
Number 2:
=(C2 - C4)
6
7
Sum:
=(C4 + C5)
8

Total sum across wide range of specific cells

Okay, so suppose we have the data set up like so:
Area 1 10 sq m
Wall 1 5 sq m
Wall 2 6 sq m
Wall 3 5 sq m
Wall 4 6 sq m
Area 2 21 sq m
Wall 1 7 sq m
Wall 2 4 sq m
Window 1 2 sq m
Wall 3 7 sq m
Window 2 2 sq m
Wall 4 4 sq m
And in another sheet, the data is to be presented like so:
Total interior wall area __ sq m
Area 1 __ sq m
Area 2 __ sq m
Total floor area __ sq m
Area 1 __ sq m
Area 2 __ sq m
How would I do this automatically?
What I was thinking is:
Look for cells with a specific tag (like "wall") in them
Get the number value to the right, under the "AREA" column
Get the sum of all those values
My problem with this algorithm is that I do not know how to execute it, not knowing the specific functions needed. Or, maybe there are other ways to do this with specific functions that I do not know of. Thank you very much to those who would answer, this is my first question on this website.
=ARRAYFORMULA({SUM(FILTER(D1:D, REGEXMATCH(LOWER(B1:B), "wall")));
QUERY({IF(A1:A<>"", A1:A, IF(B1:B="",,VLOOKUP(ROW(A1:A),
IF(A1:A<>"", {ROW(A1:A), A1:A}), 2, 1))), B1:B, D1:D},
"select sum(Col3)
where lower(Col2) contains 'wall'
group by Col1
label sum(Col3)''", 0)})

Relationship between m and n in filter of image processing

I have a filter/kernel like
| 1 1 1|
H = 1/m | 1 n 1|
| 1 1 1|
I want to know what is the relationship between m and n in this filter and how this relationship
effect the image using convolution.
There doesn't have to be any relationship between n and m, but if you want the convolution to be normalized, you need the sum of the kernel to be 1. In that case
m = 8 + n
The wiki page on kernels also explains that
Normalization ensures that the pixel values in the output image are of
the same relative magnitude as those in the input image.
Otherwise if m < 8 + n they will be brighter, or if m > 8 + n they will be dimmer.
NOTE
As pointed out by BЈовић, changing n changes the action of the filter significantly (see comments on this question).

KNN Decision Boundary

I have two classes:
x={-3,-2,1} //represented by *
y={0,5,6,7} //represented by x
If k=3, how do you determine the decision boundary?
* * x * x x x
| | | | | | | | | | | | |
-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
Supposedly the correct answer is 1.5, between 1 and 2. How does that work?
The KNN algorithm classifies new observations by looking at the K nearest neighbors, looking at their labels, and assigning the majority (most popular) label to the new observation.
For KNN with K=3, anything < 1.5 will be classified as * and anything > 1.5 will be classified as x.
You can see this by trying out a few examples. Suppose you need to classify a value of 1. The three nearest neighbors are the * at 1, the x at 0, and the * at -2. Since there are two *'s and one x, 1 will be classified as *.
Now suppose you want to classify 2. Here, the three nearest neighbors are the x at 0, the * at 1, and the x at 5. So 2 would get classified as x.
The KNN process implicitly defines a decision boundary. The best way to determine it that I'm aware of is to try a bunch of examples and look for the transition boundary where observation classifications change from one class to another class. In your example this would look like this:
-5 -> *
-4 -> *
-3 -> *
-2 -> *
-1 -> *
0 -> *
1 -> *
2 -> x
3 -> x
4 -> x
You can see this in your example - the decision boundary is somewhere between 1 and 2. Hence the 1.5 answer.

Resources