SQL查询,基于条件查找表更新值

时间:2019-12-11 19:39:38

标签: sql sqlite

我有一个查找表,其中每个位置每个化合物的最小值如下。

最小表

location    A       B       C       D       E

compound1   5.0     2.0     nan     0.5     nan
compound2   3.2     nan     0.16    0.4     0.3
compound3   8.0     nan     nan     0.2     nan

我有一个 data_table ,该化合物具有相同的化合物和位置(重复次数更多)。我想更新此 data_table 中的值,以便将 min_table 中的值以下的任何值替换为 min_table 的值。 compound1,compound2,compound3 在每个表的索引中,尽管 data_table 具有其他索引值(例如 compound4,compound5 )。

location    compound    A       A       B    B      B       C    C      C       D       D    E

0           compound1   0.36    0.5     5    9.8    7.2     9    4.5    7.8     0.89    14   2
1           compound1_flag  NaN U       U    J      J       J    NaN    NaN     NaN     NaN  U
2           compound2   0.4     0.3     2.2  3.2    1800    3200 6.1    0.16    0.3     0.16 5.6
3           compound2_flag  U   NaN     NaN     U   NaN     NaN  NaN    U     U     U    NaN
4           compound3   0.2     0.1     8   3300    280     700  1000   0.47    6.1     0.59 200
5           compound3_flag  U   NaN     U   NaN     NaN     NaN  NaN    J       NaN     NaN   NaN

以下尝试似乎无效。

UPDATE data_table
SET (compound1, compound2, compound3) = 
    (SELECT compound1, compound2, compound3 
    FROM min_table 
    WHERE data_table.location = min_table.location 
    AND data_table.index < min_table.index)

WHERE
    EXISTS (
       SELECT *
       FROM min_table
       WHERE min_table.index = data_table.index
   )

以防万一,我在熊猫中以SQL查询的形式运行此代码。我认为问题在于SQL代码。

import pandasql as ps

sqlcode = [sql query above]
newdf = ps.sqldf(sqlcode,locals())
  

PandaSQLException:(sqlite3.OperationalError)在“索引”附近:语法错误

0 个答案:

没有答案