【问题标题】:Distribute table data to another table using SQL query使用 SQL 查询将表数据分发到另一个表
【发布时间】:2021-10-14 07:38:20
【问题描述】:

有关如何使用 SQL Server Query 将 Default Table 分配到 Transaction Table 的帮助,如果另一侧不存在月份?请参考下面的示例表

默认表

Month Value
Jan 0
Feb 0
Mar 0
Apr 0
May 0

交易表

Month Sales
Jan 10
Feb 0
Apr 20

我想实现以下结果。

Month Sales
Jan 10
Feb 0
Mar 0
Apr 20
May 0

【问题讨论】:

    标签: sql-server-2012


    【解决方案1】:

    您可以使用INSERT...WHERE NOT EXISTS

    INSERT INTO Transaction (Month, Sales)
    SELECT c.Month, d.Sales
    FROM [Default] d
    WHERE NOT EXISTS (SELECT 1
        FROM Transaction t
        WHERE t.Month = d.Month)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-14
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 2013-08-02
      • 2014-01-28
      相关资源
      最近更新 更多