问题
Following SQL query:
select * from er_101
where cd_relnaam IN (
select cd_relnaam
from er_101
group by cd_relnaam
having count(*) > 1)
AND ld_relopdrachtgever = '1'
Though I need to have that sub query also limits on ld_relopdrachtgever = '1'
How is that possible with an HAVING statement?
回答1:
You can also use WHERE
in sub-query.
SELECT * FROM er_101
WHERE cd_relnaam IN (
SELECT cd_relnaam
FROM er_101
WHERE ld_relopdrachtgever = '1' <--You can add WHERE clause before GROUP BY
--^^^^^----
GROUP BY cd_relnaam
HAVING COUNT(*) > 1)
AND ld_relopdrachtgever = '1'
回答2:
see this "article" about finding duplicate records http://blog.sqlauthority.com/2007/07/11/sql-server-count-duplicate-records-rows/
来源:https://stackoverflow.com/questions/13085483/where-clause-in-sub-query