WHERE clause in sub-query

╄→гoц情女王★ 提交于 2019-12-11 20:18:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!