【问题标题】:SQL Union with Join带连接的 SQL 联合
【发布时间】:2017-02-16 19:30:30
【问题描述】:

这是我previous post 的延续。我的两张表是这样的:

Table1
Name    Id      Amount 
Name1   1       99
Name1   1       30
Name1   9       120.2
Name2   21      348
Name2   21      21
Name3   41      99
Name6   20      23

Table2
Name    Id      Return Amount 
Name1   1       99
Name1   1       30
Name1   9       120.2
Name2   21      348
Name2   21      21
Name3   41      99
Name4   19      923.2
Name5   23      12

我需要一个如下所示的结果表:

Name    Id      Amounts    Returns
Name1   1       2          2
Name1   9       1          2
Name2   21      2          1
Name3   41      1          1
Name4   1       0          1
Name5   23      0          1
Name6   20      1          0

我尝试过这样的事情:

SELECT
    [Name],
    [Id],
    ISNULL(count([Amount]), 0) as 'Amounts'
FROM table1 AS A
GROUP BY [Name], [Id]
UNION (
            SELECT
                [Name],
                [Id],
                ISNULL(count([Return Amount]), 0) as 'Returns'
            FROM 
                table2 AS B
            GROUP BY [Name], [Id]
)

但这给了我:

Name    Id      Amounts
Name1   1       4
Name1   9       3
Name2   21      3
Name3   41      2
Name4   1       1
Name5   23      1
Name6   20      1

我知道我需要将Return Amount 放在 SELECT 语句中,但我不知道该怎么做,由于该联合,我尝试的所有操作都会导致语法错误。

解决这个问题的正确方法是什么?

更新 1: 试过了

SELECT
    A.[Name],
    A.[Id],
    count(A.[Amount]),
    count(B.[Return])
FROM (
    SELECT
        [Name],
        [Id],
        count([Amount]) as 'Amounts'
    FROM table1
    UNION
    SELECT 
            [Name],
            [Id],
            count([Return]) as 'Returns'
    FROM table2
) AS A
LEFT JOIN table2 as B on A.[Id] = B.[Id]
GROUP BY A.[Name], A.[Id]

但我得到了错误:

Msg 8120, Level 16, State 1, Line 8
Column 'table1.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 207, Level 16, State 1, Line 4
Invalid column name 'Amount'.

【问题讨论】:

  • 你不是刚问这个吗?您可以使用该答案来解决此问题。 stackoverflow.com/questions/42280207/…
  • 返回 9 应该是 1 而不是 2 ..
  • @manderson 这不是因为这里的区别在于table2 中的某些行的id 与table1 中的任何内容都不匹配,但我仍然希望它们出现在结果表中。
  • 在给定样本数据的情况下,Name1 ID 9 的返回计数似乎不正确。

标签: sql sql-server union


【解决方案1】:

使用 UNION 合并两个表,并在两列中拆分金额和返回:

select name, id,  count(Amount) AS Amounts, count("Return Amount") AS returns
from ( 
   select name, id, Amount, NULL as "Return Amount" 
   from table1
   union all
   select name, id, NULL, "Return Amount" 
   from table2
)  t1
group by name, id 

【讨论】:

  • @Richard:嗯,这就是您为该列命名的方式:Return Amount
  • 完美,成功了!
【解决方案2】:

查看你的约会似乎你需要一个左连接和分组

select a.name, a.id,  count(a.Amount) Amountes, count(b.Returns)
from ( 
   select name, id, Amount
   from table1
   union 
   select name, id, `Return Amount` 
   from table2
)  t1 as a 
left join Table2 as b on a.id = b.id
group by a.name, a.id 

【讨论】:

  • 这没有给我我想要的,因为name4name5 没有出现在结果表中,因为它们的 id 与 table. I essentially need to union the two tables then combine wherever there's duplicate id` 数字中的任何内容都不匹配
  • 我不得不进行编辑,但这仍然不起作用。在帖子中更新
【解决方案3】:

我会在加入之前得到计数,这样我们就不会因为表之间的卡数而导致人为计数膨胀的问题。我也不明白为什么联合是必要的,因为完全外部连接似乎可以工作......

我们必须使用合并,因为我们不知道名称或 ID 是否存在于两个表中,例如名称 6 和名称 5

SELECT Coalesce(T1.Name, T2.Name) as Name
     , Coalesce(T1.ID, T2.ID) as ID
     , Coalesce(T1.Amount,0) as Amount
     , coalesce(T2.Returns,0) as Returns
FROM (SELECT Name, ID, count(1) as Amount from table1 GROUP BY name, ID) T1
FULL OUTER JOIN 
     (SELECT Name, ID, count(1) as Returns from table2 GROUP BY name, ID) T2
 on T1.Name = T2.Name
and T1.ID = T2.ID

【讨论】:

    猜你喜欢
    • 2016-12-13
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多