sql-server - 我如何在 where 语句中使用 Count(*)?

标签 sql-server tsql dataexplorer

我不明白为什么这不起作用以及如何解决它,我尝试了各种方法,比如写作

select COUNT(p.OwnerUserId)

但这不起作用,我不明白错误消息。我不使用 MS SQL(我使用 SQLite 和 MySQL)。

我如何编写此查询以便按 10 或 50 筛选 QC? (其中 QC > 50 和 ...)

基本上将下面的 SQL 插入此 URL,运行它,您将在结果中看到 1。 https://data.stackexchange.com/stackoverflow/query/new

SELECT
    TOP 100
    p.OwnerUserId  AS [User Link],
    sum(ViewCount) as VC,
    avg(ViewCount) as AVC,
    COUNT(p.OwnerUserId ) as QC

FROM Posts p
join Users on p.OwnerUserId = Users.Id
where PostTypeId = 1 and ViewCount<10000 and CommunityOwnedDate is null
group by p.OwnerUserId
order by AVC desc

最佳答案

您需要使用 Having 子句来过滤聚合字段

试试这个:

SELECT
    TOP 100
    p.OwnerUserId  AS [User Link],
    sum(ViewCount) as VC,
    avg(ViewCount) as AVC,
    COUNT(p.OwnerUserId ) as QC

FROM Posts p
join Users on p.OwnerUserId = Users.Id
where PostTypeId = 1 and ViewCount<10000 and CommunityOwnedDate is null
group by p.OwnerUserId  
HAVING COUNT(p.OwnerUserId ) > 50
order by AVC desc

关于sql-server - 我如何在 where 语句中使用 Count(*)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4834100/

相关文章:

sql - 表 A 的 PK 被表 B 的 FK 引用。不能丢弃表 A 的 PK

sql-server - UNION 与 SELECT DISTINCT 和 UNION ALL 性能对比

mysql - 如何更新名称与 SQL 格式匹配的表的行

tsql - 等待 ADO.NET 或 TSQL 中的事务复制

sql - SQL Server 中的作业历史记录

sql - 如何在 SQL SERVER 中比较两个多值列

mysql - SELECT 、 FROM 或 WHERE 中的嵌套 SELECT 子句及其区别

sql - 如何使用 Stack Exchange Data Explorer 查找每个用户的热门标签?

sql - 我需要更改什么才能从 SEDE 中获取已删除、锁定的帖子的数量?

mysql - 如何通过 data.stackexchange.com 获取每个标签的问题和答案?