选择数据库中表的计数总和

发布于 2024-11-19 18:52:13 字数 588 浏览 4 评论 0原文

我有以下查询:

SELECT SUM(count) 
  FROM (SELECT 'artist' AS table_name, COUNT(*) as count FROM artist
        UNION
        SELECT 'persons' AS table_name, COUNT(*) as count FROM persons
        UNION
        SELECT 'track' AS table_name, COUNT(*) as count FROM track)

它按预期工作并返回正确的计数。但是现在,当我执行以下查询时,我得到了错误的计数:

SELECT SUM(count) 
  FROM (SELECT COUNT(*) as count FROM artist
        UNION
        SELECT COUNT(*) as count FROM persons
        UNION
        SELECT COUNT(*) as count FROM track)

为什么第一个查询得到正确的计数,而第二个查询却没有?

I have the following query:

SELECT SUM(count) 
  FROM (SELECT 'artist' AS table_name, COUNT(*) as count FROM artist
        UNION
        SELECT 'persons' AS table_name, COUNT(*) as count FROM persons
        UNION
        SELECT 'track' AS table_name, COUNT(*) as count FROM track)

It works as expected and returns the proper count. But now when I do the following query I get the incorrect count:

SELECT SUM(count) 
  FROM (SELECT COUNT(*) as count FROM artist
        UNION
        SELECT COUNT(*) as count FROM persons
        UNION
        SELECT COUNT(*) as count FROM track)

Why is it the first query gets the proper count and the second one does not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丘比特射中我 2024-11-26 18:52:14

怎么会不准确呢?一种可能是因为,在第二个查询中,您使用了 UNION,并且两个或更多行包含相同的值 - 因为 UNION 删除了重复的值。尝试使用 UNION ALL,它返回所有行,而不仅仅是唯一的行。

How is it inaccurate? One way might be because, in the second query, you hvae UNION, and two or more rows contain the same value--because UNION removes duplicate values. Try it with UNION ALL, which returns all rows, not just unique ones.

决绝 2024-11-26 18:52:13

UNION 消除重复值,因此如果两个计数碰巧相同,则消除一个,只对一个求和。尝试使用 UNION ALL 代替。

SELECT sum(count) FROM
(SELECT COUNT(*) as count FROM artist
UNION ALL
SELECT COUNT(*) as count FROM persons
UNION ALL
SELECT COUNT(*) as count FROM track)

The UNION eliminates duplicate values, so if two counts happen to be the same, one is eliminated and only one is summed. Try using UNION ALL instead.

SELECT sum(count) FROM
(SELECT COUNT(*) as count FROM artist
UNION ALL
SELECT COUNT(*) as count FROM persons
UNION ALL
SELECT COUNT(*) as count FROM track)
别想她 2024-11-26 18:52:13

如果您可以忍受近似值,只需一次性计算所有表格

SELECT
   Total_Rows= SUM(st.row_count)
FROM
   sys.dm_db_partition_stats st
WHERE
   (index_id < 2) --cover both heaps and clustered indexes
   AND
   OBJECT_SCHEMA_NAME (object_id) <> 'sys' --ignore system stuff

这将在一瞬间运行

采用您的原始数据,您可以一次性获得每个表格和整体的计数。

SELECT
    *,
    SUM(count) OVER () AS GrandTotal
FROM (SELECT 'artist' AS table_name, COUNT(*) as count FROM artist
    UNION
    SELECT 'persons' AS table_name, COUNT(*) as count FROM persons
    UNION
    SELECT 'track' AS table_name, COUNT(*) as count FROM track)

If you can live with approximate, just count all tables in one go

SELECT
   Total_Rows= SUM(st.row_count)
FROM
   sys.dm_db_partition_stats st
WHERE
   (index_id < 2) --cover both heaps and clustered indexes
   AND
   OBJECT_SCHEMA_NAME (object_id) <> 'sys' --ignore system stuff

This will run in a flash

Adopting your original, you can get count per table and overall in one go..

SELECT
    *,
    SUM(count) OVER () AS GrandTotal
FROM (SELECT 'artist' AS table_name, COUNT(*) as count FROM artist
    UNION
    SELECT 'persons' AS table_name, COUNT(*) as count FROM persons
    UNION
    SELECT 'track' AS table_name, COUNT(*) as count FROM track)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文