【问题标题】:Is there a way to insert data for certain values into one table from another?有没有办法将某些值的数据从另一个表插入到另一个表中?
【发布时间】:2019-05-20 20:01:09
【问题描述】:

我创建了一张表来汇总数据,并希望将其用于我的主表。我需要将多个表加入到这个主表中,但无法将 2017 年的数据放入其中。当我执行 LEFT JOIN 时,它会为 Wireless Rev 添加另一列。如何在“nans”所在的位置输入我的 2017 年数据以替换“nans”?表格示例:

Table1

State    Year    Wireline    Wireless
-------------------------------------
TENN.    2017    120         NAN
TEXAS    2017    255         NAN
TENN.    2018    182         55
TEXAS    2018    222         120

Table2

State    Year    Wireless
-------------------------------------
TENN.    2017    222
TEXAS    2017    431

我已经尝试了左连接,但它在现有无线旁边创建了另一列:

SELECT Table1.*,Table2.Wireless
FROM Table1    
LEFT JOIN Table2
ON Table1.State = Table2.State
AND Table1.Year = Table2.Year

这给了我:

State    Year    Wireline    Wireless    Wireless
--------------------------------------------------
TENN.    2017    120         NAN         222
TEXAS    2017    255         NAN         431  
TENN.    2018    182         55          0 
TEXAS    2018    222         120         0

我希望得到:

State    Year    Wireline    Wireless
-------------------------------------
TENN.    2017    120         222
TEXAS    2017    255         431
TENN.    2018    182         55
TEXAS    2018    222         120

【问题讨论】:

    标签: python sql join merge left-join


    【解决方案1】:

    也许是这样的?

    SELECT Table1.State,
           Table1.Year,
           Table1.Wireline,
           COALESCE(Table1.Wireless, Table2.Wireless) AS Wireless
    FROM Table1    
    LEFT JOIN Table2
    ON Table1.State = Table2.State
    AND Table1.Year = Table2.Year
    

    【讨论】:

    • 给我“ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Microsoft Access Driver] Undefined function 'COALESCE' in expression. (-3102) (SQLExecDirectW)")" I顺便说一句,我在 Python 中这样做
    • 我基本上只是想插入这两个值来代替那些 nans
    • 如果您正在使用访问权限,请尝试将“COALESCE”关键字替换为“Nz”,这是访问选项。 stackoverflow.com/questions/247858/…
    猜你喜欢
    • 1970-01-01
    • 2020-05-22
    • 2023-03-25
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 2014-07-29
    相关资源
    最近更新 更多