August 23, 2007 at 8:48 am
Hi,
I have a table consisting of two fields, both int, CompanyID and CategoryID
CompanyID, CategoryID
35764 49
35764 55
35764 107
35764 139
35764 141
35764 145
35764 149
35764 164
35764 175
35764 178
35765 49
35765 55
35765 135
35765 138
35765 139
35765 140
35765 141
I want to pass in a number of categoryid's to a stored procedure. eg '49, 55, 141' and return all companies that have all of them categories.
I cannot get it to work, I cannot use OR or IN as the company needs to be in all of the categories. If I use AND it will not work as the categoryid are on different rows, if that makes sense.
Any help would be great.
Thanks
Andy
August 23, 2007 at 8:59 am
August 23, 2007 at 9:21 am
Hi,
Thanks for the link but I cannot see an example of how to help me :-
take the following query :-
SELECT DICTINCT CompanyID FROM CompanyCategory WHERE CategoryID IN (49, 107)
This would return
35764
35765
Note that I don't want companyid 35765 as it doesn't have both category id's
I also cannot do this query
SELECT DICTINCT CompanyID FROM CompanyCategory WHERE CategoryID = 49 AND CategoryID = 106
as this would return 0 records.
I really only want to return companyid
35764
Anymore pointers.
August 23, 2007 at 11:05 am
SELECT <columns> FROM CompanyCategory WHERE CategoryID in
(select companyid from CompanyCategory where categorid in (<all categories you want> having count(distinct categoryid)=<count of categories you want>
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
August 23, 2007 at 2:54 pm
Thanks Matt,
I should have thought of that, with a few modifications it worked a treat.
Cheers
Andy
August 23, 2007 at 9:17 pm
Would you care to post your solution, please?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply