How to find the MAX of a calculated value in a window? - sqlite

I have a simple database table with three columns: id, x, y. x and y are just the coordinates of points in a line. I want to using the SQLite Window function to partition the table using a sliding window of three rows, and then get the y value that is the furthest from the y value of the first coordinate (row) in the window.
An example:
| id | x | y |
|----|---|---|
| 1 | 1 | .5|
| 2 | 2 | .9|
| 3 | 3 | .7|
| 4 | 4 |1.1|
| 5 | 5 | 1 |
So the first partition would consist of:
| id | x | y |
|----|---|---|
| 1 | 1 | .5|
| 2 | 2 | .9|
| 3 | 3 | .7|
And the desired result would be:
| id | x | y | d |
|----|---|---|---|
| 1 | 1 | .5| .4|
| 2 | 2 | .9|
| 3 | 3 | .7|
Since the the window with id = 1 as the CURRENT ROW would have a maximum variation of .4; the maximum distance between the y value of the first row in the partition, .5, and .9, is .4.
The final expected result:
| id | x | y | d |
|----|---|---|---|
| 1 | 1 | .5| .4|
| 2 | 2 | .9| .2|
| 3 | 3 | .7| .4|
| 4 | 4 |1.1| .1|
| 5 | 5 | 1 | |
I've tried using a window function like: WINDOW win1 AS (ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING which gives me the correct window.
With the window defined, I tried doing something like:
SELECT
max(abs(y - first_value(y) OVER win1)) AS d
FROM t
WINDOW win1 AS (ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING)
But I get an error for misuse of first_value.
I think the problem I have is this is not the proper approach to calculate over each row of a partition, but I could not find another solution or approach that matches what I am trying to do here.

For each row of your table you define a window starting from the current row up to the next 2 rows.
In your code y is the value in the current row and first_value() is the 1st value of y of the current window which is also the value of y of the current row.
So even if your code was syntactically correct the difference you calculate would always return 0.
It's easier to solve your problem with LEAD() window function:
WITH cte AS (
SELECT *,
LEAD(y, 1) OVER () AS y1,
LEAD(y, 2) OVER () AS y2
FROM tablename
)
SELECT
id, x, y,
MAX(ABS(y - y1), COALESCE(ABS(y - y2), 0)) d
FROM cte
See the demo.
Results:
id x y d
1 1 0.5 0.4
2 2 0.9 0.2
3 3 0.7 0.4
4 4 1.1 0.1
5 5 1.0

Related

Combining aggregate functions in sqlite

Assuming the following table and using sqlite I have the following question:
Node |Loadcase | Fx | Cluster
---------------------------------
1 | 1 | 50 | A
2 | 1 | -40 | A
3 | 1 | 60 | B
4 | 1 | 80 | C
1 | 2 | 50 | A
2 | 2 | -50 | A
3 | 2 | 80 | B
4 | 2 | -100 | C
I am trying to write a query which fetches the maximum absolute value of Fx and the Load case for each Node 1-4.
An additional requirement is that Fx having the same Cluster shall be summed up before making this query .
In the example above I would expect the following results:
Node | Loadcase | MaxAbsClusteredFx
-----|-----------|-------------------
1 | 1 | 10
2* | |
3 | 2 | 80
4 | 2 | 100
N/A because summed up with node one. Both belonging to cluster A
Query:
For Node 1 I would execute a query similar to this
SELECT Loadcase,abs(Fx GROUP BY Cluster) FROM MyTable WHERE abs(Fx GROUP BY Cluster) = max(abs(Fx GROUP BY Cluster)) AND Node = 1
I keep getting " Error while executing query: near "Forces": syntax error " or alike.
Thankful for any help!

Fetch min and max values in the same row using group by

The first thing is my English is basic. Sorry.
Second thing, and the most important here: I can't find the way to do a simple query. My table is like this:
------------------------------------------
id_det_iti | id_iti | orden_iti| id_ciudad
--------------------------------------------
1 | 1 | 1 | 374
2 | 1 | 2 | 25
3 | 1 | 3 | 241
4 | 2 | 1 | 34
5 | 2 | 2 | 22
6 | 2 | 3 | 352
7 | 2 | 4 | 17
--------------------------------------------
Then, I wanna get results like this:
------------------------------------------
id_iti | min | id_ciudad | max | id_ciudad
------------------------------------------
1 | 1 | 374 | 3 | 241
2 | 1 | 34 | 4 | 17
------------------------------------------
I need to show the max and the min value in the same row group by id_iti.
I have tried to use full join, but I'm working with sqlite, and that's not an option. I spend a long day trying with different options but I can't found the solution. I hope you guys can help me.
Thanks in advance!
Edit:
SELECT a.id_iti, c.id_ciudad, d.id_ciudad
FROM detalle_itinerario as a,
(SELECT MAX(orden_iti),id_ciudad, id_iti FROM detalle_itinerario) AS c
INNER JOIN
(SELECT MIN(orden_iti),id_ciudad, id_iti FROM detalle_itinerario) AS d
ON c.id_iti=d.id_iti
GROUP BY a.id_iti;
That's only one of my attempts, but I get just values of the first coincidence.
First, use a simple query to get the min/max values for each group:
SELECT id_iti,
MIN(orden_iti) AS min,
MAX(orden_iti) AS max,
FROM detalle_itinerario
GROUP BY id_iti;
You can the use these values to join back to the original table:
SELECT a.id_iti,
a.min,
a2.id_ciudad,
a.max,
a3.id_ciudad
FROM (SELECT id_iti,
MIN(orden_iti) AS min,
MAX(orden_iti) AS max
FROM detalle_itinerario
GROUP BY id_iti) AS a
JOIN detalle_itinerario AS a2 ON a.id_iti = a2.id_iti AND a.min = a2.orden_iti
JOIN detalle_itinerario AS a3 ON a.id_iti = a3.id_iti AND a.max = a3.orden_iti;

Julia Gadfly can't scale axis when Scale.x_log10

I am new to Julia and try to get a simple x-y plot through Gadfly Pkg.
I am trying to plot x-axis in log scale and set min and max value in the same time.
plot(layer(rdsPmos, x="A", y="B", Geom.line), Scale.x_log10(minvalue= 10),
Theme(default_point_size = 1.5px))
This won't get any error message. The outcome plot has a log scale x-axis but the minvalue seems not work.
I also try to write lise this:
plot(layer(rdsPmos, x="A", y="B", Geom.line), Scale.x_log10, Scale.x_continuous(minvalue= 10), Theme(default_point_size = 1.5px))
And the result is the minvalue work but the logscale fail.
My tests shows that minvalue and maxvalue options works in the way that none of the data missed from view-port, (true for x_continuous or x_log10), so if one wants a narrower view-port, one way is to apply filter on data:
julia> df = DataFrame(A = 1:10, B = 2:2:20)
10x2 DataFrames.DataFrame
| Row | A | B |
|-----|----|----|
| 1 | 1 | 2 |
| 2 | 2 | 4 |
| 3 | 3 | 6 |
| 4 | 4 | 8 |
| 5 | 5 | 10 |
| 6 | 6 | 12 |
| 7 | 7 | 14 |
| 8 | 8 | 16 |
| 9 | 9 | 18 |
| 10 | 10 | 20 |
minvalue is not working, and it's nothing with Scale type:
julia> plot(layer(df, x="A", y="B" ,Geom.line), Scale.x_log10(minvalue=5), Theme(default_point_size = 1.5px))
julia> plot(layer(df, x="A", y="B" ,Geom.line), Scale.x_continuous(
minvalue=5), Theme(default_point_size = 1.5px))
minvalue is working on filtered data
julia> plot(layer(df[df[:A].>5,:], x="A", y="B" ,Geom.line), Scale.x_log10(minvalue=5), Theme(default_point_size = 1.5px))

How to add certain elements of column in Matrix in R?

N* [1]| [2] | [3]
1* | 3 | 20 | 3 |
2* | 2 | 10 | 3 |
3* | 3 | 25 | 3 |
4* | 1 | 15 | 3 |
5* | 3 | 30 | 3 |
Can you help me to get a sum of second column, but only sum of elements that has 3 in the first row. For example in that matrix it is 20+25+30=75. In a fastest way (it's actually big matrix).
P.S. I tried something like this with(Train, sum(Column2[,"Date"] == i))
As you can see I need sum Of Colomn2 where date has certain meaning (from 1 to 12)
We can create a logical index with the first column and use that to subset the second column and get the sum
sum(m1[m1[,1]==3,2])
EDIT: Based on #Richard Scriven's comment.

Calculate Grid Position

I'm trying to figure out a way to calculate the positions in the grid like I have below. I know the row, column, totalColumns, totalRows. For example, given column = 2, row = 0, totalColumns = 4, totalRows = 3, the position is B (11)
Cols
+ + + + +
| 0 | 1 | 2 | 3 |
+--+---|---|---|---|---
0 | 9 | A | B | C |
+--+---|---|---|---|--- Rows
1 | 5 | 6 | 7 | 8 |
+--+---|---|---|---|---
2 | 1 | 2 | 3 | 4 |
+--+---|---|---|---|---
ah,, well, i guess you have better thinks to do than school ;))
hex(tr*tc-r*tc-tc+c+1)

Resources