sql - How to return rows matched in a table without multiple EXISTS clauses? -
i want pull results 1 table match all specified values specified values in table. can this:
select * contacts exists (select 1 dbo.contactclassifications contactid = contacts.id , classificationid = '8c62e5de-00fc-4994-8127-000b02e10da5') , exists (select 1 dbo.contactclassifications contactid = contacts.id , classificationid = 'd2e90aa0-ac93-4406-af93-0020009a34ba') , exists etc... however falls on when 40 exists clauses. error message "the query processor ran out of internal resources , not produce query plan. rare event , expected extremely complex queries or queries reference large number of tables or partitions. please simplify query."
the gist of
- select contacts guid
instatement - use
distinct countcount each contactid on matching guid's - use
havingretain contacts equal amount of matching guid's you've putinstatement
sql statement
select * dbo.contacts c inner join ( select c.id dbo.contacts c inner join dbo.contactclassifications cc on c.id = cc.contactid cc.classificationid in ('..', '..', 38 other guids) group c.id having count(distinct cc.classificationid) = 40 ) cc on cc.id = c.id test script @ data.stackexchange
Comments
Post a Comment