SQL添加2个计数查询的结果

时间:2014-04-12 17:19:33

标签: sql sql-server

我希望在2个表上运行相同的查询并获得组合结果。内部子查询返回2个字符串,我想计算2个表中存在多少个字符串。下面的查询分别给出了每个表的结果,但是如何将它组合成单个查询或在SQL Server中添加每个表的数值结果?感谢

select count(1)
from Team
where Team.TeamName IN (select Data FROM CSVTest_Match WHERE Header in ('Home Team', 'Away Team'))

select count(1)
from TeamAlias
where TeamAlias.TeamName  IN (select Data FROM CSVTest_Match WHERE Header in ('Home Team', 'Away Team'));

2 个答案:

答案 0 :(得分:2)

试试这个:

SELECT COUNT(*)
FROM
(
    select 1
    from Team
    where Team.TeamName IN (select Data FROM CSVTest_Match WHERE Header in ('Home Team', 'Away Team'))
    UNION ALL
    select 1
    from TeamAlias
    where TeamAlias.TeamName  IN (select Data FROM CSVTest_Match WHERE Header in ('Home Team', 'Away Team'))
) t

答案 1 :(得分:0)

如果您不关心重叠计数(请参阅我的评论):

select sum(num_recs)
  from (select count(*) as num_recs
          from team t
          join CSVTest_Match c
            on t.teamname = c.data
         where c.header in ('Home Team', 'Away Team')
        union all
        select count(*) as num_recs
          from teamalias t
          join CSVTest_Match c
            on t.teamname = c.data
         where c.header in ('Home Team', 'Away Team')) X;