| Author |
Topic |
|
Dagmd
Starting Member
14 Posts |
Posted - 2007-03-08 : 05:52:30
|
| Hello,I am trying to use an IN statement for a set of values, however instead I need to only return a column when all values are matched. Is there a variation on IN that allows you to test for all values within your IN ' ' statement? or another way to do this besides multiple AND statements?ThanksTalis |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 05:56:22
|
| Use EXISTS keyword.Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 05:57:24
|
| SELECT t1.*FROM Table1 AS t1WHERE EXISTS (SELECT NULL FROM Table2 AS t2 WHERE t2.ColA = t1.Col1 AND t2.ColB = t1.Col2)Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 05:58:52
|
| Use above when comparing to other table, or use INNER JOIN.If only comparing to a number of values, you have to use AND.These are all guesses.Without some proper sample data and your expected output based on the sample data, this is all we can help you with.Peter LarssonHelsingborg, Sweden |
 |
|
|
Dagmd
Starting Member
14 Posts |
Posted - 2007-03-08 : 06:02:18
|
Hi Peter,Thanks. Can you give an example how I change my statement below:- Select * from whateverwhere x=z andy in ('1','2','3')into an EXISTS statement?quote: Originally posted by Peso SELECT t1.*FROM Table1 AS t1WHERE EXISTS (SELECT NULL FROM Table2 AS t2 WHERE t2.ColA = t1.Col1 AND t2.ColB = t1.Col2)Peter LarssonHelsingborg, Sweden
|
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 06:06:06
|
| You don't have to. It is more efficient the way you have written it.Peter LarssonHelsingborg, Sweden |
 |
|
|
Dagmd
Starting Member
14 Posts |
Posted - 2007-03-08 : 06:38:56
|
Ah, but I need y (in example above) to only return if it matches all the values within the IN list.quote: Originally posted by Peso You don't have to. It is more efficient the way you have written it.Peter LarssonHelsingborg, Sweden
|
 |
|
|
richardps
Starting Member
33 Posts |
Posted - 2007-03-08 : 06:41:53
|
| How can Y be equal to '1' and '2' and '3' all at the same time?Can you better explain with perhaps some example values?Thanks,Richard. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 07:14:34
|
| Select *, case when y in ('1','2','3') then 'Found' else 'Not found' end from whateverwhere x = z One other variant...Please post some proper sample data and your expected output based on the sample data.Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 07:15:52
|
Or this?select idfrom ( Select ID from whatever where x = z and y = '1' union all Select ID from whatever where x = z and y = '2' union all Select ID from whatever where x = z and y = '3' ) as xgroup by idhaving count(*) = 3 Peter LarssonHelsingborg, Sweden |
 |
|
|
Dagmd
Starting Member
14 Posts |
Posted - 2007-03-08 : 08:53:43
|
Hi again, here is a sample query that explains what I am currently doing:-select distinct sys.namefrom sysinner join statusx on sys.resourceid = statusx.res_idinner join wkstation-status on sys.res_id = wkstation-status.res_id where statusx.status0 = 'true'and statusx.id0 in('S14','S015','S018')quote: Originally posted by Peso Select *, case when y in ('1','2','3') then 'Found' else 'Not found' end from whateverwhere x = z One other variant...Please post some proper sample data and your expected output based on the sample data.Peter LarssonHelsingborg, Sweden
|
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 09:12:24
|
| What's wrong with that query?It seems to work ok.Peter LarssonHelsingborg, Sweden |
 |
|
|
Dagmd
Starting Member
14 Posts |
Posted - 2007-03-08 : 09:39:30
|
Hi yes, it does work, however - statusx.id0 must be true for each value in the IN list. At the moment - it is true if it matches any value in the list.quote: Originally posted by Peso What's wrong with that query?It seems to work ok.Peter LarssonHelsingborg, Sweden
|
 |
|
|
richardps
Starting Member
33 Posts |
Posted - 2007-03-08 : 10:59:27
|
| Sample data required to fully understand this I think...what is the result of select top 20 statusx.id0 - this should give a bit more of a clue.Perhaps you are after creating a string from 'S14', 'S015', 'S016' or a like statement with %'s? |
 |
|
|
|