【问题标题】:Making a query to merge two tables upon criteria, with giving priority to the rows from the second table进行查询以根据条件合并两个表,优先考虑第二个表中的行
【发布时间】:2021-02-07 12:49:49
【问题描述】:

我有两个 MS Access 表。

table1
ID  Country     Dat         Val
1   Australia   01.10.2021  10
2   Canada      01.10.2021  100
3   Japan       01.10.2021  1000
4   Australia   02.10.2021  20
5   Canada      02.10.2021  200
6   Japan       02.10.2021  2000

table2
ID  Country     Dat         Val
1   Canada      01.10.2021  50000
2   Greece      01.10.2021  50100
3   Canada      02.10.2021  60000
4   Greece      02.10.2021  60100

此 SQL 代码允许我合并两个表,它提供 10 行。

   SELECT table2.Dat,
          table2.Country,
          table2.Val
     FROM table2
LEFT JOIN table1
       ON (table1.Dat = table2.Dat)
      AND (table1.Country = table2.Country)

UNION ALL

   SELECT table1.Dat,
          table1.Country,
          table1.Val
     FROM table1
LEFT JOIN table2
       ON (table1.Dat = table2.Dat)
      AND (table1.Country = table2.Country)

 ORDER BY table2.Dat,
          table2.Country;

它会根据“日期”和“国家”标准给出重复项。

问题

我想知道,如果我想以某种方式合并两个表,如果 table1 和 table2 具有相同的“日期”和“国家”,那么代码应该是什么样子,然后会选择 table2 行,而不是两者?

换句话说,如何获取 table1 和 table2 中不匹配条件“日期”和“国家”的所有唯一行,并给出 table2 的优先级 何时 两个表中的行匹配 条件“日期”和“国家”?

(“日期+国家/地区”捆绑在每个表中是唯一的,即一个表中只有一个“数据+国家/地区”)

换句话说,我可以使用什么查询来使结果看起来像这样,即 8 行没有日期 + 国家/地区重复项?

Expected Result
Dat         Country     Val
01.10.2021  Australia   10
01.10.2021  Canada      50000
01.10.2021  Greece      50100
01.10.2021  Japan       1000
02.10.2021  Australia   20
02.10.2021  Canada      60000
02.10.2021  Greece      60100
02.10.2021  Japan       2000

还有什么可能是一个好的方法来制作这样的 SQL 脚本来根据这样的几个标准合并表?

【问题讨论】:

    标签: sql database ms-access union-all


    【解决方案1】:

    您只需要UNION ALL 用于2 个表,并在WHERE 子句中使用NOT EXISTS 用于table1

    SELECT t1.Dat, t1.Country,  t1.Val FROM table1 AS t1
    WHERE NOT EXISTS (SELECT 1 FROM table2 AS t2 WHERE t2.Country = t1.Country AND t2.Dat = t1.Dat)
    UNION ALL
    SELECT Dat, Country, Val FROM table2
    ORDER BY Dat, Country
    

    结果:

    Dat         Country     Val
    1/10/2021   Australia   10
    1/10/2021   Canada      50000
    1/10/2021   Greece      50100
    1/10/2021   japan       1000
    2/10/2021   Australia   20
    2/10/2021   Canada      60000
    2/10/2021   Greece      60100
    2/10/2021   Japan       2000
    

    【讨论】:

      【解决方案2】:

      基本的方式就是把这些任务拆分成子任务。

      这里更小、更容易的子任务可能如下:

      1. 获取table2 中存在,但table1 中不存在的所有行(匹配条件“日期”和“国家/地区”)强>。
      2. 获取table1 中存在,但table2 中不存在的所有行(匹配条件“日期”和“国家”)强>。
      3. 获取同时存在于表 2 和表 1 中的所有行(匹配条件“日期”和“国家/地区”)只取行来自table2
      4. UNION ALL 放在您之前创建和测试过的部分之间(以确保所有正常工作)。

      因此,这是一个可能的简单解决方案:

         SELECT table2.Dat,
                table2.Country,
                table2.Val
           FROM table2
      LEFT JOIN table1
             ON (table1.Country = table2.Country)
            AND (table1.Dat = table2.Dat)
          WHERE table1.Country IS NULL
            AND "comment: Getting from table2 everything that is absent in table1 (matching country and date criteria)"
      
      UNION ALL
      
         SELECT table1.Dat,
                table1.Country,
                table1.Val
           FROM table1
      LEFT JOIN table2
             ON (table1.Country = table2.Country)
            AND (table1.Dat = table2.Dat)
          WHERE table2.Country IS NULL
            AND "comment: Getting from table1 everything that is absent in table2 (matching country and date criteria)"
      
      UNION ALL
      
         SELECT table2.Dat,
                table2.Country,
                table2.Val
           FROM table2,
                table1
          WHERE table1.Country = table2.Country
            AND table1.Dat = table2.Dat
            AND "comment: getting from table2 everything that exists in table1 (matching country and date criteria)"
      
       ORDER BY table2.Dat,
                table2.Country;
      

      结论

      所以这里的问题只是从“重叠集”中取出对应的行,然后合并它们。

      要了解更多,您可以查看Venn diagram and some other answers here (SO)non-Venn way explanation here

      【讨论】:

      • 3 个子查询过多。 All from table2 + from table1 which not exist in table2 - 这就足够了。
      • @akina,好吧,这里编写的每个子查询都可以用作单独的脚本,供其他人使用...但是另一方面,您可能是对的,两个子查询在这里可能会快一点。 ..
      猜你喜欢
      • 2013-09-30
      • 1970-01-01
      • 2015-11-08
      • 2016-07-21
      • 1970-01-01
      • 2019-09-20
      • 2021-07-30
      • 1970-01-01
      相关资源
      最近更新 更多