计算来自多个查询的总行数?

时间:2019-03-28 14:09:55

标签: sql sql-server tsql

如何将其绑定为一个查询并从所有3个查询中获取总数? 谢谢

new Map(Object.entries(this.props.json))

3 个答案:

答案 0 :(得分:1)

使用subqueryunion all合并所有结果集:

SELECT SUM(CNT) AS Total_count
FROM (SELECT COUNT(*) AS CNT
      FROM . . .
      UNION ALL
      SELECT . . .
      FROM . . .
      UNION ALL
      SELECT . . . 
      FROM . . .
     ) t;

答案 1 :(得分:1)

一种方法是简单地将SELECT放在前面并使用子查询:

SELECT (SELECT COUNT(*)
        FROM TopicArticleMappings a JOIN
             ArticleAuthor b
             ON A.ArticleId = B.ArticleGuid
        WHERE b.AuthorGuid = '9AC4AB6E-7454-4660-97CD-93D5BC6772C3'
       ) +
       (SELECT COUNT(*)
        FROM JurisdictionAuthors a JOIN
             Authors b
             ON a.AuthorGuid = b.AuthorGuid
        WHERE b.AuthorGuid = '9AC4AB6E-7454-4660-97CD-93D5BC6772C3'
       ) +
       (SELECT COUNT(*)
        FROM TopicConsultingEditors a JOIn
             tAuthors b
             ON a.AuthorRef = b.AuthorRef
        WHERE b.AuthorGuid = '9AC4AB6E-7454-4660-97CD-93D5BC6772C3'
       )

答案 2 :(得分:0)

我将使用变量而不是“联合”,例如:

declare @JurisdictionAuthors int, @TopicConsultingEditors int, @TopicArticleMappings  int

SELECT @TopicArticleMappings = COUNT(*)
FROM TopicArticleMappings a
INNER JOIN ArticleAuthor b
ON A.ArticleId = B.ArticleGuid
WHERE b.AuthorGuid = '9AC4AB6E-7454-4660-97CD-93D5BC6772C3'

SELECT @JurisdictionAuthors = COUNT(*) 
FROM JurisdictionAuthors a
INNER JOIN Authors b
ON a.AuthorGuid = b.AuthorGuid
WHERE b.AuthorGuid = '9AC4AB6E-7454-4660-97CD-93D5BC6772C3'

SELECT @TopicConsultingEditors = COUNT(*) 
FROM TopicConsultingEditors a
INNER JOIN tAuthors b
ON a.AuthorRef = b.AuthorRef
WHERE b.AuthorGuid = '9AC4AB6E-7454-4660-97CD-93D5BC6772C3'

select @JurisdictionAuthors + @TopicConsultingEditors + @TopicArticleMappings