How do I write a code in Hue SQL for slope or decay rate? - hue

I have the next table t1 with the next values:
table t1
What I am looking for is to generate a slope(or decay rate) by id, for reading_pct on every other 10 days (from the 1st day to the last day).
I was thinking I can use this code for slope:
SELECT sum((x - x_bar) * (y - y_bar)) / sum((x - x_bar) * (x - x_bar)) as slope
FROM
(SELECT datediff(to_date(reading_dt), id_dt) AS x,
avg(datediff(to_date(reading dt), id_dt)) over() AS x_bar,
reading_pct AS y,
avg(reading_pct) over() AS y_bar
FROM t1;
But I cannot get it to work for every other 10 days on reading_dt, for the same id, and I cannot get it to work as a subsequent query in a query (either as having or ...where...)
I really need help here.

Related

Google Sheet Query Function - Combine 2 Steps together in Query

Pl. refer the Sheet shared below -
https://docs.google.com/spreadsheets/d/1r0_oHLTcT9tmTJPgKVgkGwGWhooBM8uluwmWcC92eOk/edit?usp=sharing
Rank for diff. factors is calculated in Rank Sheet in Table A. Rank!ColL calculates combine score (D+ G + J).
PF Sheet Range - Z:AE have Step wise calculation and results expected.
1 - Objective is to get results of used Query(Rank!C8:L220,"Select C,L where D > F12 & L > 0.5 order by L desc",1) (ref. cell PF Sheet!AE17).
Query is referencing RANK Sheet's Cols.
2 - Possibility of merging Combine Score calculation as base data in Query.
Do revert is more clarity is reqd.
I reviewed the Sheet, and I can make some suggestions.
Since you are calling 2 columns with the Query, add 1 column to the right of AE, and that one will be the new AF.
After that, modify the formula from:
=query(Rank!C8:L220,"Select C,L where D > 0.25 & L > 0.5 order by L desc",1)
to:
=query(Rank!C8:L220,"Select C,L where D > 0.25 and L > 0.5 order by L desc",1)
The reason it was not working is the use of & instead of and as mentioned here.
To make reference to a specific Cell instead of a value. You can use "&F12&", Like this:
Reference
QUERY function

InfluxDB: Is flux the only way to add simple calculations as a column in a query?

I'm trying a query like so:
SELECT COUNT("value"), F("value"),G("value") FROM "someTable" WHERE time >= t1 AND time < t2 GROUP BY (aggregateWindow),*
F = sum of squares, and this wouldn't be too hard if I could do something like the following SUM("value"*"value"), but apparently that doesn't work in Influx (or maybe I'm using the syntax wrong).
G = time stamp of aggregate in unix epoch + aggregateWindow. So for example, if aggregateWindow == 1s, then I would want the following output (assuming there's only one point in that aggregateWindow whose value is value):
time value F G
---- ----- -- -----------------
1600272300000000000 1 1 1600272301000000000
1600272301000000000 2 4 1600272302000000000
1600272302000000000 3 9 1600272303000000000
1600272303000000000 4 16 1600272304000000000
1600272304000000000 5 25 1600272305000000000
I know you can implement sum of squares via flux as described here, but I'm worried about the performance of Flux vs regular Influx queries as mentioned here. So basically I'm asking, is flux the only and most efficient way of making a query like this?
Simple:
SELECT
COUNT("value"),
"value"*"value" AS F,
POW("value", 2) AS FwithPOWfunction
FROM "someTable"
WHERE
time >= t1 AND time < t2
GROUP BY (aggregateWindow)
You can't create new time column, but you can apply offset to time grouping GROUP BY time(time_interval,[<offset_interval])
Doc is your good friend to get more details and learn correct syntax:
https://docs.influxdata.com/influxdb/v1.8/query_language/explore-data/#group-by-time-intervals-and-fill
https://docs.influxdata.com/influxdb/v1.8/query_language/functions/#pow

How to get the sum of a column up to a certain value?

I have a google sheet that I am using to try and calculate leveling and experience points. Column A has the level and Column B has the exp needed to reach the next level. i.e. To get to Level 3 you need 600 exp.
A B
1 200
2 400
3 600
...
99 19800
In column I2 I have an integer for an amount of exp (e.g. 2000), in column J2 I want to figure out what level someone would be at if they started from 0.
Put this in column J and ddrag down as required. Rounddown(I2,-2) rounds I2 down to the nearest 100. Index match finds a match in column B and returns the value in column A of the matched row.
=index(A2:A100,match(ROUNDDOWN(I2,-2),B2:B100,0))
Using a helper column (for example Z): put =sum(B$1:B1) in cell Z1 and drag down. This will compute the sums required for each level. In J2, use the formula
=vlookup(I2, {B:B, Z:Z}, 2) + 1
which looks up I2 in column B, and returns the nearest match that is less than or equal to the search key. It adds 1 to find the level that would be reached, because your table has this kind of an offset to you: the entry against level N is about achieving level N+1.
You may want to put 0 0 on top of the table, to correctly handle the amounts under 200. Or treat them with a separate if condition.
Using algebra
In your specific scenario, the point amount required for level N can be computed as
200*(1+2+3+...+N-1) = 200*(N-1)*N/2 = 100*(N-1/2)^2 - 25
So, given x amount of points, we can find N directly with algebra:
N = floor(sqrt((x+25)/100)+1/2)
which means that the formula
=floor(sqrt((I2 + 25) / 100) + 1/2)
will have the desired effect in cell J2, without the need for an extra column and vlookup.
However, the second approach only works for this specific point values.

Sort or Filter Column, display in ranges

I have a log to track my running distance/times/etc. I am trying to come up with a way to display my top 10 or so times (pace/mile) at various distances (0-2.9, 3-4.9, 5-6.9, etc). I've tried to use "Sort" and I'm able to sort by distance, but I want to be able to only select the distances within those groups, then sort based on pace. Ideally, it would display Date / Distance / Pace.
I'm not quite sure what terms I'm supposed to be looking for, so it makes it hard to find.
Here's a link to my sheet: https://docs.google.com/spreadsheets/d/1d0aOsaarKaoSJNupzyk62oDh_o1yWosoi8TldCcKs0Y/edit?usp=sharing
Basically, in the "Tests" sheet, I want to display "Best Pace 3-5 Miles" and have it sort my top 10(or whatever) runs using the "Mileage" sheet column C for distance, Column F for pace, and Column B for date.
Thanks!
Try this expression.
=sort(filter(Mileage!A10:E, (Mileage!C10:C >= 3) * (Mileage!C10:C <= 5) * (Mileage!E10:E <> 0)), 5, false)
Here is the documentation for filter
https://support.google.com/docs/answer/3093197?hl=en
and for sort https://support.google.com/docs/answer/3093150?hl=en
(Mileage!C10:C >= 3) * (Mileage!C10:C <= 5) * (Mileage!E10:E <> 0) means the value in column C must be >= 3 and <= 5 and the value in E must be different from 0.
sort(range, 5, false) means: sort the 5th column in the given range in descending order.

Custom average function on a pivot table in Google Sheets

I have a spreadsheet that I'm starting to use for personal money analysis.
My main sheet is called "transactions" and has headers of Category, Description, Date and Amount (it's basically a check register).
I've created a pivot report off of that sheet that contains sum, min and max of Amount by Category.
I would like to make a custom average function on that pivot report but not sure how to go about it.
What I would like to see is the average amount of negative transactions between positive ones.
My positive transactions are my paychecks and the negative transactions are any spending I do.
An example might help in what I'm trying to do here...
Let's say for category "Food" I have the following transactions (in this order)...
-20
-25
-30
100
-30
-35
-40
I'd like my average to be calculated like this...
( ( (-20 + -25 + -30) / 3 ) + ( (-30 + -35 + -40) / 3 ) ) / 2
Anyone have the slightest idea on how I can enhance my pivot report to do this?
You do it with something like:
=ARRAYFORMULA(AVERAGE(IF(Sheet1!D2:D8<0,Sheet1!D2:D8, 0)))
where column D is the amount of your example and Sheet1 contains the "transactions" of your example.
If you want to fill it for the pivot table (having the category as another criterion) you can check the answer at: https://stackoverflow.com/a/9165254/179529
=SUM(ARRAYFORMULA(((Transactions!$A2:$A)=$A2) * ((Transactions!$D2:$D)>0) * (Transactions!$D2:$D) ))
/
SUM(ARRAYFORMULA(((Transactions!$A2:$A)=$A2) * ((Transactions!$D2:$D)>0) * (1) ))
where $A2 is the cell where you have the category name in the pivot table (The $ will allow you to copy the formula to other columns in you want it per month or other second criterion.
If you want to SUM the element in column D only if they great than 0, you need to have ((Transactions!$D2:$D)>0) as the second argument and (Transactions!$D2:$D) as the 3rd argument (otherwise you will count the cells instead of SUM them).
Since AVERAGE will take blank cells as well, I've used SUM/COUNT instead. Note that COUNT is actually SUM with the 3rd argument as 1.
Also note that if you want to ignore a header line you need to define your columns with Transactions!$D2:$D, to start from the 2nd row.

Resources