在python中使用sqlite3的每组最近匹配?

时间:2015-09-23 13:16:54

标签: python sqlite

我非常珍惜如何找到最接近的匹配,但后来我意识到sql-server ROW_NUMBERPARTITION BY未在python脚本的sqlite3中实现。

以下是我要做的事情:

我的表格与此类似:

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

在我的SQL查询中,我试图找到CalibrationValueY的平均值,其中CalibrationValueX是一个特定值:

SELECT avg(CalibrationValueY), MotorType, Motor FROM MotorTable
WHERE CalibrationValueX = 1.23333
GROUP BY MotorType

这不会返回任何内容,因为没有CalibrationValueX值恰好等于1.23333。

我能够为每个MotorTable单独找到最接近的匹配:

SELECT CalibrationValueY, FileSize, MotorType, Motor FROM MotorTable
where FileType = 'text' order by abs(FileSize - 1.23333) LIMIT 1

但是,我不能让它与group by语句一起工作。 我怎么能这样做,如果我按MotorType分组,我正在搜索CalibrationValueX = 1.23333,我会得到这个:

A     Car       1.2343            2.33343
B     Boat      1.2455            2.55434

正如我意识到的那样,使用row_count和partition by的解决方案在sqlite3中不起作用。

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

或者......我可以为每个MotorType执行工作代码X类型......类似于SELECT DISTINCT MotorType FROM MotorTable并在循环中运行其他查询。但是,我担心速度。

1 个答案:

答案 0 :(得分:1)

  1. 获取所有类型的电机:

    SELECT DISTINCT MotorType
    FROM MotorTable;
    
  2. 为每种电机类型获取最接近校准值的电机:

    SELECT MotorType,
           (SELECT Motor
            FROM MotorTable AS T2
            WHERE T2.MotorType = Types.MotorType
            ORDER BY abs(CalibrationValueX - 1.2345)
            LIMIT 1
           ) AS Motor
    FROM (SELECT DISTINCT MotorType
          FROM MotorTable) AS Types;
    
  3. 使用以下值过滤原始表:

    SELECT Motor,
           MotorType,
           CalibrationValueX,
           CalibrationValueY
    FROM MotorTable
    JOIN (SELECT MotorType,
                 (SELECT Motor
                  FROM MotorTable AS T2
                  WHERE T2.MotorType = Types.MotorType
                  ORDER BY abs(CalibrationValueX - 1.2345)
                  LIMIT 1
                 ) AS Motor
          FROM (SELECT DISTINCT MotorType
                FROM MotorTable) AS Types)
    USING (MotorType, Motor);