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 in statement
  • use distinct count count each contactid on matching guid's
  • use having retain contacts equal amount of matching guid's you've put in statement

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

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -