【问题标题】:appending staging table to main, based on multiple columns, lots of nulls基于多列,大量空值将临时表附加到主表
【发布时间】:2014-06-23 17:28:24
【问题描述】:

将行从会计系统带入临时表。想要将临时表中的新记录附加到主表。没有主键,因为除了查看每一列之外,行中没有唯一标识符。很多空值,因为不是每一列都需要填写。

示例数据:

分期:

    预算行|日期|资金|金额|描述|采购订单号

    1 |20140623 |xyz |12.00 |甜甜圈 |{null}
    1 |{null} |xyz |3.00 |{null} |12345
    1 |20140623 |abc |4.00 |轮胎 |{null}
    2 |20140623 |xyz |12.00 |甜甜圈 |{null}
    1 |20140623 |xyz |12.00 |鲍勃甜甜圈 |{null}

主要:

    预算行|日期|资金|金额|描述|采购订单号

    1 |20140623 |xyz |12.00 |甜甜圈 |{null}
    1 |{null} |xyz |3.00 |{null} |12345
    1 |20140623 |abc |4.00 |轮胎 |{null}

我一直是这样的:

INSERT INTO Main SELECT budget_line ,date ,fund ,amount ,description ,PO_number FROM Staging WHERE NOT EXISTS ( SELECT budget_line ,date ,fund ,amount ,description ,PO_number FROM Main WHERE ((Staging.budget_line = Main.budget_line) OR (Staging.budget_line is null AND Main.budget_line is null)) AND ((Staging.date = Main.date) OR (Staging.date is null AND Main.date is null)) AND ((Staging.fund = Main.fund) OR (Staging.fund is null AND Main.fund is null)) AND ((Staging.amount = Main.amount) OR (Staging.amount is null AND Main.amount is null)) AND ((Staging.description = Main.description) OR (Staging.description is null AND Main.description is null)) AND ((Staging.PO_number = Main.PO_number) OR (Staging.PO_number is null AND Main.PO_number is null)) )

我得到了一些没有过来的东西,我不知道为什么。我有大约 28 个字段。有没有更简单的方法来做到这一点?

【问题讨论】:

    标签: sql ms-access


    【解决方案1】:

    你的 null 处理有问题..

    试试这个 - 它将所有字段连接到一个键列中,并测试:

    INSERT INTO Main
    SELECT budget_line
    ,date
    ,fund
    ,amount
    ,description
    ,PO_number
    FROM Staging
    WHERE NOT EXISTS (
       SELECT 1
       FROM Main
       WHERE Staging.budget_line & Staging.date & Staging.fund & Staging.amount & Staging.description & Staging.PO_number =
       Main.budget_line & Main.date & Main.fund & Main.amount & Main.description & Main.PO_number)
    

    【讨论】:

    • 我会在有机会的时候进行测试,但它应该可以工作。我想到了这一点,但认为空值会弄乱 concat,所以我没有使用它。谢谢!
    猜你喜欢
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多