加入两个表并添加计数结果

时间:2015-11-30 15:35:05

标签: sql select join

我有2张表A&乙

A

select TranType, DocType, Document  FROM
db1.dbo.A

TRANTYPE | DOCTYPE  | DOCUMENT
Coup     | Stat     | 1
Coup     | Stat     | 2
Coup     | Stat     | 3
Coup     | Stat     | ...
Coup     | Stat     | 100
Swp      | Corr     | 1
Swp      | Corr     | 2
Swp      | Corr     | ...
Swp      | Corr     | 5

B ......

SELECT  TranType, DocType, COUNT (*) AS Docs 
FROM db1.dbo.A 
GROUP BY TranType, DocType

TRANTYPE | DOCTYPE  | DOCS
Coup     | Stat     | 100
Swp      | Corr     | 5

B:

SELECT  RecType, SubType, COUNT (*) AS Docs 
FROM db2.dbo.B 
GROUP BY RecType, SubType

RECTYPE  | SUBTYPE  | DOCS
Coup     | Stat     | 50
Cr       | Cr       | 3
Swp      | Cr       | 10

我设法获得2个表的交集并添加计数结果:

A∩B

SELECT a.TranType, a.DocType,
     (SELECT COUNT(*) FROM db2.dbo.B b WHERE b.RecType= a.TranType AND b.SubType = a.DocType) +
     COUNT(a.TranType) AS SUM
FROM db1.dbo.A a 
GROUP BY a.TranType, a.DocType 
ORDER BY TranType

TRANTYPE | DOCTYPE  |  SUM
Coup     | Stat     |  150

任何人都知道如何获得所有参赛作品(联盟)?

TRANTYPE | DOCTYPE  | DOCS
Coup     | Stat     | 150    <----
Swp      | Corr     | 5
Cr       | Cr       | 3
Swp      | Cr       | 10

解: 我的桌子上有不同的排序规则,导致错误。

在此处找到具有正确整理处理的解决方案:SQL Sample Fiddle

4 个答案:

答案 0 :(得分:2)

<强> SQL Fiddle Demo

UNION很简单。只需要匹配列名

UNION ALL保持重复

SELECT TranType, DocType, SUM (Docs)
FROM (
      SELECT  TranType, DocType, COUNT (*) AS Docs 
      FROM TableA
      GROUP BY TranType, DocType
      UNION ALL
      SELECT  RecType as TranType, SubType as DocType, COUNT (*) AS Docs 
      FROM TableB 
      GROUP BY RecType, SubType
     ) T
GROUP BY TranType, DocType
ORDER BY TranType, DocType;

<强>输出

| TranType | DocType |     |
|----------|---------|-----|
|     Coup |    Stat | 150 |
|       Cr |      Cr |   3 |
|      Swp |    Corr |   5 |
|      Swp |      Cr |  10 |

修改

你的第一个查询应该像这样写,以避免每行都有一个子查询。

SELECT TranType, DocType, SUM (Docs)
FROM (
      SELECT  TranType, DocType, COUNT (*) AS Docs 
      FROM TableA
      GROUP BY TranType, DocType
      UNION ALL
      SELECT  RecType as TranType, SubType as DocType, COUNT (*) AS Docs 
      FROM TableB 
      GROUP BY RecType, SubType
     ) T
GROUP BY TranType, DocType
HAVING COUNT(*) > 1;

答案 1 :(得分:1)

我建议使用子查询,然后聚合:

select TranType, DocType, sum(docs) as docs
from ((select TranType, DocType, count(*) as docs
       from db1.dbo.A a
       group by TranType, DocType
      ) union all
      (select RecType, SubType, count(*) as docs
       from db1.dbo.B
       group by RecType, SubType
      )
     ) ab
group by TranType, DocType;

您也可以使用full outer join,但是您必须在最终结果中处理NULL值。

注意:这将返回所有对,即使只有一个表。

答案 2 :(得分:0)

您想要交叉加入吗?

SELECT * 
FROM (
  SELECT  TranType, DocType, COUNT (*) AS ACount 
  FROM db1.dbo.A 
  GROUP BY TranType, DocType
) A, (
  SELECT  RecType, SubType, COUNT (*) AS BCount
  FROM db2.dbo.B 
  GROUP BY RecType, SubType
) B
WHERE A.DocType = B.SubType

答案 3 :(得分:0)

到目前为止,所有解决方案都使问题过于复杂。你还想要计数或总和吗?

SELECT TranType, DocType, SUM(DOCS) DOCS--or COUNT(DOCS) DOCS
FROM (SELECT TranType, DocType, DOCS FROM A
      UNION ALL
      SELECT RecType, SubType, DOCS FROM B) t
GROUP BY TranType, DocType