SQL部分完全外部联接

时间:2017-02-16 16:56:41

标签: sql sql-server join full-outer-join

这是我之前的post here的延续。我有一张这样的桌子:

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]

我得到这张表:

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

现在我有另一张这样的表:

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]

我得到这张表:

Id      Returns 
1       2
9       2
21      1
41      1

我需要将这两个表组合起来创建一个这样的表:

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

这是我的Full outer join语句:

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]

但是这给了我下面的表格:

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

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

6 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

您需要在SELECT语句中引用连接表。以及参考列的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"

语义,但我认为在同一聚合级别上JOIN表是最佳实践。所以我建议分别运行每个聚合表,然后加入。这可以防止意外的数据重复。喜欢这个

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]

答案 2 :(得分:1)

对您已有的汇总结果使用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]

答案 3 :(得分:0)

您可以单独找到聚合,然后进行加入:

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

答案 4 :(得分:0)

您只需要在选择列表中添加回报即可。如果需要两个表中的所有行,请使用完全外部联接。非匹配将在非匹配表的字段中具有空值。

左连接或右连接将给出一个表中的所有行,并匹配另一个表中的所有行。内连接仅返回匹配的行。

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

答案 5 :(得分:0)

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

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', 'angular-cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-jasmine-html-reporter'),
      require('karma-chrome-launcher'),
      require('karma-remap-istanbul'),
      require('angular-cli/plugins/karma'),
      require('karma-coverage'),
      require('karma-sourcemap-loader')

    ],
    files: [
      { pattern: './src/test.ts', watched: false }
    ],
    preprocessors: {
      './src/test.ts': ['angular-cli']
    },
    mime: {
      'text/x-typescript': ['ts','tsx']
    },
    remapIstanbulReporter: {
        reports: {
          html: 'coverage',
        lcovonly: './coverage/coverage.lcov'
      }
    },
    angularCli: {
      config: './angular-cli.json',
      environment: 'dev'
    },
    reporters: config.angularCli && config.angularCli.codeCoverage
              ? ['progress', 'karma-remap-istanbul']
              : ['progress', 'kjhtml'],
    coverageReporter: {
      includeAllSources: true
    },
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};