简体   繁体   中英

How to reference an aliased column in where clause that contains a grouping function

I have a simple sql query below, which produces a usable result for me. However I would ideally like to further limit the results of this query to only include results where the aliased column total is less then 10. I have tried directly referencing the aliased column in a where clause, and also duplicating the COUNT() portion in a where clause, but that doesn't work. Here is the query, thanks in advance for the help.

SELECT COUNT(DISTINCT iDomainID) AS totInFile, iFileGroup
FROM Domains.`ApacheVirtualHosts`
GROUP BY iFileGroup

You can further filter using a HAVING clause:

SELECT COUNT(DISTINCT iDomainID) AS totInFile, iFileGroup
FROM Domains.`ApacheVirtualHosts`
GROUP BY iFileGroup
HAVING COUNT(DISTINCT iDomainID) < 10;

A HAVING clause will allow you to filter data using an aggregate function.

You need a HAVING clause:

SELECT COUNT(DISTINCT iDomainID) AS totInFile, iFileGroup
FROM Domains.`ApacheVirtualHosts`
GROUP BY iFileGroup
HAVING COUNT(DISTINCT iDomainID) < 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM