简体   繁体   English

SQL部分完全外部联接

[英]SQL Partial Full Outer Join

This is a continuation of my previous post here . 这是我以前在这里发表的文章的继续。 I have a table like this: 我有一张这样的桌子:

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

If I run this query, thanks to Juan Carlos Oropeza: 如果我运行此查询,则要感谢Juan Carlos Oropeza:

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

I get this table: 我得到这张桌子:

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

Now I have another table like this: 现在,我有另一个这样的表:

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

If I run this query: 如果我运行此查询:

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

I get this table: 我得到这张桌子:

Id      Returns 
1       2
9       2
21      1
41      1

I need to combine these two tables to create a table like this: 我需要结合这两个表来创建一个像这样的表:

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

Here's my Full outer join statement: 这是我的完全外部连接语句:

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]

But that gives me the following table: 但这给了我下表:

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

How do I get the Returns column to attach? 如何获得“ Returns列的附件? I'm not sure which join to use in this case but my best educated answer would be a full outer join. 我不确定在这种情况下要使用哪个联接,但是我受过良好教育的答案是完全外部联接。 Any ideas? 有任何想法吗?

In the select, you only select the fields name, id, and count. 在选择中,您仅选择字段名称,ID和计数。 You have to add B.returns to your select statement. 您必须在选择语句中添加B.returns。

You need to reference the joined table in your SELECT statement. 您需要在SELECT语句中引用联接表。 And also GROUP BY that referenced column. 还有该引用列的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"

Semantics, but I consider it best practice to JOIN tables on the same aggregate level. 语义,但我认为最好的做法是在同一聚合级别上联接表。 So I'd recommend running each aggregate table separately, then joining. 因此,我建议分别运行每个聚合表,然后再加入。 This prevents accidental data-duplication. 这样可以防止意外的数据复制。 Like this 像这样

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]

Use a full join on the aggregated results you already have. 对您已有的汇总结果使用full join When there is a row missing on either of the tables, use COALESCE to show that result as 0 or some other value. 当任何一个表上都缺少行时,请使用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]

You can find the aggregates separately and then do the joining: 您可以分别找到聚合,然后进行联接:

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];

You just need to add returns to your select list. 您只需要在选择列表中添加退货即可。 Use a full outer join if you want all rows from both tables. 如果希望两个表中的所有行都使用完全外部联接。 Non matches will have null values in the fields from the non-matching table. 不匹配项将在不匹配表的字段中具有空值。

Left join or Right join will give all rows in one table and matches from the other. 左联接或右联接将给出一个表中的所有行,并给出另一个表中的匹配项。 Inner join will only return rows where there is a match. 内部联接只会返回匹配的行。

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

You just need to fetch elements from query B 您只需要从查询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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM