November 22, 2004 at 8:30 am
I have a 2 column tabel, with column name Person_id and Comp_id
I would like to select the Person_id(s) that satisfy criterias like the following
select Person_id from P_Person where Comp_id=1 and Comp_id=2
But this does not return anything, so how do I accomplish a select statment that will return those Person_id(s) that fullfill my critera and match both comp_id=1 and comp_id=2 ?
so specific person_id(s) will be present several times in Personid column
November 22, 2004 at 8:36 am
select P1.Person_id from P_Person P1 join P_Person P2 on P1.Person_id =P2.Person_ID where P1.Comp_id=1 and P2.Comp_id=2
* Noel
November 22, 2004 at 8:53 am
Thanks Noel!
November 22, 2004 at 10:43 am
November 22, 2004 at 11:22 am
Sorry but I believe that does NOT answer the question posted
* Noel
November 22, 2004 at 3:04 pm
Yes the second suggestion returns not only where Comp_id=1 AND Comp_id=2 but
when Comp_id=1 or Comp_id=2
But how can I accomplish a SP where I can send in a string of parameters that ALL need to be fullfilled in the way as the example above?
IS this possible without knowing how many parameters to send in to the SP?
Ex of the string with comp_id. (1,2,3) or (1) or (1,2) :
so the string must be variable, but the criteria must be the same, all the parameters in the string have to exist to be able to return a Person_id.
The table
Person_id | Comp_id
1 1
1 2
1 3
2 2
2 3
3 1
November 23, 2004 at 1:16 am
Well, while I somehow believe your data model has some kind of flaw, you might want to have a look at
http://www.sommarskog.se/dynamic_sql.html
http://www.sommarskog.se/arrays-in-sql.html
http://www.sommarskog.se/dyn-search.html
Should give you some ideas how you can accomplish what you're after.
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
November 23, 2004 at 3:16 am
Try :
SELECT p1.Person_id
FROM P_Person p1
JOIN P_Person p2
ON p2.Person_id = p1.Person_id
AND p2.Comp_id = 2
WHERE p1.Comp_id = 1
November 23, 2004 at 3:41 am
You can write this query as following
select Person_id from P_Person where Comp_id>=1 and Comp_id<=2
or
select Person_id from P_Person where (Comp_id=1 or Comp_id=2)
shifan
November 23, 2004 at 4:03 am
An alternative, generic, solution would be :
SELECT Person_id
FROM P_Person
WHERE Comp_id IN (1, 2, 3, ...)
GROUP BY Person_id
HAVING COUNT(*) >= @NoParams
November 23, 2004 at 4:06 am
November 23, 2004 at 8:27 am
I agree, both of the last two posts are better generic solutions.
Bob Monahon
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply