For a table that holds the records of user\'s webpages visiting behavior, how can I select users that visit more than one webpages.
The structure of this tables is:<
While HAVING
is a good approach in this case, remember that queries can be nested:
SELECT userId, pageCount
FROM (
SELECT userId, COUNT(DISTINCT webpageId) AS pageCount
FROM visits
GROUP BY userId) AS n
WHERE pageCount > 1
The actual query plans may differ, especially if HAVING
is an optimized case, but there is no reason why the plans must be different. (Compare plans on the specific RDBMS/version if it is an issue or concern.)