I asked preciously how to find closest match, but I later I realized that sql-server ROW_NUMBER and PARTITION BY is not implemented in sqlite3 in python script.
Here is what I am trying to do:
I have table that looks similar to this:
Motor MotorType CalibrationValueX CalibrationValueY
A Car 1.2343 2.33343
B Boat 1.2455 2.55434
B1 Boat 1.4554 2.11211
C Car 1.4323 4.56555
D Car 1.533 4.6666
..... 500 entries
In my SQL query, I am trying to find average of CalibrationValueY where CalibrationValueX is a certain value:
SELECT avg(CalibrationValueY), MotorType, Motor FROM MotorTable
WHERE CalibrationValueX = 1.23333
GROUP BY MotorType
This will not return anything, since there is not a CalibrationValueX value that equals exactly 1.23333.
I am able to find closest match separately for each MotorTable with:
SELECT CalibrationValueY, FileSize, MotorType, Motor FROM MotorTable
where FileType = 'text' order by abs(FileSize - 1.23333) LIMIT 1
However, I can't get it to work with a group by statement. How can I do it so that if I am grouping by MotorType and I am searching CalibrationValueX = 1.23333, I would get this:
A Car 1.2343 2.33343
B Boat 1.2455 2.55434
The solution with row_count and partition by does not work in sqlite3, as I realized.
with cte as (
SELECT MotorType, CalibrationValueX, CalibrationValueY,
ROW_NUMBER() over (partition by MotorType order by abs(CalibrationValueX - 1.23333)) rn
from historyCR
)
SELECT *
from cte
where rn = 1
Alternatively... I could just execute working code X types per each MotorType... Something like SELECT DISTINCT MotorType FROM MotorTable and run other query in a loop. However, I am concerned about speed.
Aucun commentaire:
Enregistrer un commentaire