加入

时间:2017-02-16 19:30:30

标签: sql sql-server union

这是我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语句中,但我不知道该怎么做,我尝试的所有内容都会因为该联合而导致语法错误。< / p>

正确的方法是什么?

更新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'.

3 个答案:

答案 0 :(得分:1)

查看您的日期似乎需要一个左连接和分组

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 

答案 1 :(得分: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 

答案 2 :(得分:0)

我在加入之前得到了计数,因此我们不会因为表之间的加工而产生人为计数通胀问题。我也不明白为什么一个联盟是必要的,因为一个完整的外部联接似乎会起作用......

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

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>js book example1</title>
</head>

<body>
  <p>test</p>
  <script>
    document.write("<h2>Table of Fibonacci Numbers</h2>");
    for (i = 0, j = 1, k = 0, fib = 0; i < 50; i++, fib = j + k, j = k, k = fib) {
      document.write("Fibonacci(" + i + ") =" + fib);
      document.write("<br>");
    }
  </script>
</body>
</html>