Domanda

Non capisco perché questo non funziona e come risolvere il problema, ho provato varie cose come scrivere

select COUNT(p.OwnerUserId)

, ma che non funziona e non capisco i msg di errore. Non faccio uso di MS SQL (io uso SQLite e MySQL).

Come faccio a scrivere questa query in modo che io possa filtrare il controllo di qualità da 10 o 50? (Dove QC> 50 E ...)

In sostanza collegare lo SQL di seguito in questo URL, eseguirlo e vedrete 1 del nei risultati. 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
È stato utile?

Soluzione

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

Un'altra opzione è quella di renderlo un sottoquery

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

Altri suggerimenti

È necessario utilizzare clausola having per filtrare un campo aggregato

Prova questo:

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

Quando si utilizza aggregati, è necessario utilizzare having invece di where.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top