声明でcount(*)を使用するにはどうすればよいですか?
-
27-10-2019 - |
質問
なぜこれが機能していないのか、それを修正する方法がわかりません、私は書くなどのさまざまなことを試しました
select COUNT(p.OwnerUserId)
しかし、それはうまくいかず、エラーMSGSがわかりません。私はMS SQLを使用しません(SQLiteとMySQLを使用しています)。
QCを10または50でフィルタリングできるように、このクエリを書くにはどうすればよいですか? (ここでQC> 50および...)
基本的に、以下のSQLをこのURLに接続して実行すると、結果に1が表示されます。http://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
解決
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 ) between 10 and 50 -- <<<<<
order by AVC desc
別のオプションは、それをサブクエリにすることです
SELECT
TOP 100
FROM (
SELECT
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
) SQ
WHERE QC >= 50
order by AVC desc
他のヒント
節を使用して集約フィールドをフィルタリングする必要があります
これを試して:
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
アグリゲートを使用している場合は、使用する必要があります having
それ以外の where
.
所属していません StackOverflow