Qual seria a consulta usando subconsulta que seria equivalente a:
SELECT CPF, COUNT(*) FROM notas_fiscais
WHERE YEAR(DATA_VENDA) = 2016
GROUP BY CPF
HAVING COUNT(*) > 2000
Solução extra:
SELECT n.CPF, n.NUM FROM (SELECT CPF, COUNT(*) AS NUM
FROM NOTAS_FISCAIS
WHERE YEAR(DATA_VENDA) = 2016
GROUP BY CPF) n WHERE n.NUM > 2000
ORDER BY n.NUM;
Output:
CPF | NUM |
---|---|
492472718 | 2008 |
3623344710 | 2012 |
50534475787 | 2037 |