我不明白为什么这不起作用以及如何解决它,我尝试了各种方法,比如写作
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/