SQL 部分完全外连接

SQL Partial Full Outer Join

这是我之前 的延续。我有一个像这样的 table:

Name    Id      Amount 
Name1   1       99
Name1   1       30
Name1   9       120.2
Name2   21      348
Name2   21      21
Name3   41      99

如果我 运行 这个查询,感谢 Juan Carlos Oropeza:

SELECT
       [Name],
       [Id],
       count([Amount]) as 'Count'
FROM 
       table1
GROUP BY [Name], [Id]

我明白了 table:

Name    Id      Count 
Name1   1       2
Name1   9       1
Name2   21      2
Name3   41      1

现在我有另一个 table 这样的:

Id      Return Amount
1       100
1       134.3
9       912.3
9       21
21      23.23
41      45

如果我运行这个查询:

SELECT
    [Id],
    count([Return Amount]) as 'Returns'
FROM 
    table2
GROUP BY [Id]

我明白了 table:

Id      Returns 
1       2
9       2
21      1
41      1

我需要将这两个 table 组合起来创建一个 table,如下所示:

Name    Id      Count      Returns
Name1   1       2          2
Name1   9       1          2
Name2   21      2          1
Name3   41      1          1

这是我的完整外连接语句:

SELECT
       [Name],
       [Id],
       count([Amount]) as 'Count'
FROM table1 AS A
FULL OUTER JOIN (
            SELECT
                [Id],
                count([Count]) as 'Returns'
            FROM 
                table2
            GROUP BY [Id]
) B ON A.[Id] = B.[Id]
GROUP BY [Name], [Id]

但这给了我以下 table:

Name    Id      Count 
Name1   1       2
Name1   9       1
Name2   21      2
Name3   41      1

如何获取要附加的 Returns 列?我不确定在这种情况下使用哪个连接,但我受过最好的教育的答案是完全外部连接。有什么想法吗?

您可以单独找到聚合然后进行连接:

select t1.*,
    t2.*
from (
    select [Name],
        [Id],
        count([Amount]) as [Count]
    from table1
    group by [Name],
        [Id]
    ) t1
full join (
    select [Id],
        count([Return Amount]) as [Returns]
    from table2
    group by [Id]
    ) t2 on t1.[Id] = t2.[Id];

在 select 中,您只有 select 字段名称、ID 和计数。您必须将 B.returns 添加到 select 语句中。

您只需将 return 添加到您的 select 列表中。如果您想要来自两个 table 的所有行,请使用完全外部联接。 non-matching table.

的字段中的非匹配项将具有空值

左联接或右联接将给出一个 table 中的所有行和另一个匹配。内部联接只会 return 行匹配。

http://www.sql-join.com/sql-join-types/

对已有的汇总结果使用 full join。当任一表中缺少一行时,使用 COALESCE 将该结果显示为 0 或其他一些值。

SELECT 
 COALESCE(t1.[Name],'Unknown') as Name
,COALESCE(t1.[Id],t2.[Id]) as ID
,COALESCE(t1.Count,0) as Count
,COALESCE(t2.[Returns],0) as Returns
FROM (SELECT
       [Name],
       [Id],
       count([Amount]) as 'Count'
      FROM table1
      GROUP BY [Name], [Id]) t1
FULL JOIN (SELECT
           [Id],
           count([Return Amount]) as 'Returns'
           FROM table2
           GROUP BY [Id]) t2 
ON t1.[Id]=t2.[Id]

您只需要从查询 B 中获取元素

SELECT
       [Name],
       [Id],
       B.Returns,
       count([Amount]) as 'Count',

FROM table1 AS A
FULL OUTER JOIN (
            SELECT
                [Id],
                count([Count]) as 'Returns'
            FROM 
                table2
            GROUP BY [Id]
) B ON A.[Id] = B.[Id]
GROUP BY [Name], [Id],B.Returns

您需要在 SELECT 语句中引用加入的 table。还有 GROUP BY 引用的列。

SELECT
       [Name],
       [Id],
       count([Amount]) as 'Count',
       B."Returns"
FROM table1 AS A
FULL OUTER JOIN (
            SELECT
                [Id],
                count([Count]) as 'Returns'
            FROM 
                table2
            GROUP BY [Id]
) B ON A.[Id] = B.[Id]
GROUP BY [Name], [Id], B."Return"

语义,但我认为最好的做法是在同一聚合级别上加入 table。所以我建议 运行 每个聚合 table 分开,然后加入。这可以防止意外 data-duplication。像这样

SELECT
    A.Name
    ,A.Id
    ,A."Count"
    ,B."Returns"
FROM
   (SELECT
       [Name],
       [Id],
       count([Amount]) as 'Count'
    FROM 
       table1
    GROUP BY [Name], [Id]
   ) A
FULL OUTER JOIN (
            SELECT
                [Id],
                count([Count]) as 'Returns'
            FROM 
                table2
            GROUP BY [Id]
    ) B ON A.[Id] = B.[Id]