两种使用 Count 的方法,它们等效吗?

发布于 2024-11-18 00:44:25 字数 444 浏览 1 评论 0原文

相当于

SELECT COUNT(a.attr)
FROM TABLE a

SELECT B
FROM (SELECT COUNT(a.attr) as B
      FROM TABLE a)

猜不会,但我不确定。

我还假设对于 min、max、avg 等函数,答案是相同的,对吗?

编辑:

这完全是出于好奇,我对此还是新手。下面的计数和上面的计数返回的值有区别吗?

SELECT B, C
FROM (SELECT COUNT(a.attr) as B, a.C
      FROM TABLE a
      GROUP BY c)

再次编辑:我调查了它,吸取了教训:当我尝试了解这些事情时,我应该保持清醒。

Is

SELECT COUNT(a.attr)
FROM TABLE a

equivalent to

SELECT B
FROM (SELECT COUNT(a.attr) as B
      FROM TABLE a)

I would guess no, but I'm not sure.

I'm also assuming the answer would be the same for functions like min, max, avg, correct?

EDIT:

This is all out of curiosity, I'm still new at this. Is there a difference between the value returned for the count of the following and the above?

SELECT B, C
FROM (SELECT COUNT(a.attr) as B, a.C
      FROM TABLE a
      GROUP BY c)

EDIT AGAIN: I looked into it, lesson learned: I should be awake when I try to learn about these things.

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

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

发布评论

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

评论(5

如果没结果 2024-11-25 00:44:25

从技术上讲,它们是不一样的,第一个是简单选择,第二个是带有子选择的选择。
但每个理智的优化器都会为它们生成相同的执行计划。

Technically, they are not the same, the first one is a simple select, the second one is a select with a sub select.
But every sane optimizer will generate the same execution plan for both of them.

友谊不毕业 2024-11-25 00:44:25

结果是相同的,并且将相同于:

SELECT E
FROM
(SELECT D as E
    FROM
    (SELECT C as D
        FROM
        (SELECT B as C
            FROM 
            (SELECT COUNT(a.attr) as B
                FROM TABLE a))))

并且同样毫无意义。

第二个查询本质上是混淆 COUNT,应该避免。

编辑:

是的,添加到 OP 中的编辑查询是同一件事。它只是无缘无故地添加了一个子查询。

The results are the same, and would be the same as:

SELECT E
FROM
(SELECT D as E
    FROM
    (SELECT C as D
        FROM
        (SELECT B as C
            FROM 
            (SELECT COUNT(a.attr) as B
                FROM TABLE a))))

And equally as pointless.

The second query is essentially obfuscating a COUNT and should be avoided.

EDIT:

Yes, your edited query that was added to the OP is the same thing. It's just adding a subquery for no reason.

懒的傷心 2024-11-25 00:44:25

我发布这个答案是为了补充其他答案中已经说过的内容,并且因为您无法格式化注释:)

您可以随时检查执行计划以查看查询是否等效;这就是 SQL Server 的原理:

DECLARE @A TABLE
(
  attr int,
  c int
)

INSERT @A(attr,c) VALUES(1,1)
INSERT @A(attr,c) VALUES(2,1)
INSERT @A(attr,c) VALUES(3,1)
INSERT @A(attr,c) VALUES(4,2)
INSERT @A(attr,c) VALUES(5,2)

SELECT count(attr) FROM @A

SELECT B
FROM (SELECT COUNT(attr) as B
      FROM @A) AS T

SELECT B, C
FROM (SELECT COUNT(attr) as B, c AS C
      FROM @A
      GROUP BY c) AS T

这是 SELECT 语句的执行计划,您可以看到前两个语句没有区别:

在此处输入图像描述

Am posting this answer to supplement what has already been said in the other answers, and because you cannot format comments :)

You can always check the execution plan to see if queries are equivalent; this is what SQL Server makes of it:

DECLARE @A TABLE
(
  attr int,
  c int
)

INSERT @A(attr,c) VALUES(1,1)
INSERT @A(attr,c) VALUES(2,1)
INSERT @A(attr,c) VALUES(3,1)
INSERT @A(attr,c) VALUES(4,2)
INSERT @A(attr,c) VALUES(5,2)

SELECT count(attr) FROM @A

SELECT B
FROM (SELECT COUNT(attr) as B
      FROM @A) AS T

SELECT B, C
FROM (SELECT COUNT(attr) as B, c AS C
      FROM @A
      GROUP BY c) AS T

Here's the execution plan of the SELECT statments, as you can see there is no difference in the first two:

enter image description here

遗弃M 2024-11-25 00:44:25

是的,有。您在第二个中所做的所有操作就是将返回的计数命名为 B。它们将返回相同的结果。

http://www.roseindia.net/sql/sql-as-keyword.shtml

编辑:
更好的例子:
http://www.w3schools.com/sql/sql_alias.asp

第三个示例将有所不同,因为它包含一个分组依据。它将返回每个不同 aC 条目的计数。示例

B          C
w/e        a
w/e        a
w/e        b
w/e        a
w/e        c

将返回

3       a
1       b
1       c

不一定按该顺序

检查所有这些的最简单方法是亲自尝试并查看它返回什么。

Yes there are. All your doing in the second one is naming the returned count B. They will return the same results.

http://www.roseindia.net/sql/sql-as-keyword.shtml

EDIT:
Better example:
http://www.w3schools.com/sql/sql_alias.asp

The third example will be different because it contains a group by. It will return the count for every distinct a.C entry. Example

B          C
w/e        a
w/e        a
w/e        b
w/e        a
w/e        c

Would return

3       a
1       b
1       c

Not necessarily in that order

Easiest way to check all of this is to try it for yourself and see what it returns.

尬尬 2024-11-25 00:44:25

您的第一个代码示例是正确的,但第二个代码示例没有任何意义。
您只需选择所有数据两次,无需任何操作。
因此,第一个和第二个样本的输出将相等。

Your first code sample is correct, but second does not have any sense.
You just select all data twice without any operations.
So, output for first and second samples will be equal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文