| Author |
Topic |
|
dmoonme
Starting Member
13 Posts |
Posted - 2008-06-24 : 21:51:11
|
| Sample dataSURVEYID,FIELDID,VALUE1,VALUE2,VALUE320,5,Mentor,None,NULL20,246,Fluent,Fluent,Fluent21,5,None,None,NULL21,246,Limited,Limited,LimitedThe select statement that's not working, no records are returned.SELECT dbo.SurveyDetails.FIELDID, dbo.SurveyDetails.VALUE1, dbo.SurveyDetails.VALUE2, dbo.SurveyDetails.VALUE3 FROM dbo.SurveyDetails WHERE (dbo.SurveyDetails.FIELDID=246) AND(dbo.SurveyDetails.FIELDID=5 AND (VALUE1 LIKE '%mentor%' OR VALUE2 LIKE '%mentor%')) |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-06-24 : 22:15:03
|
| Does value2 has '%mentor%?SELECT FIELDID, VALUE1, VALUE2, VALUE3 FROM dbo.SurveyDetails WHERE FIELDID in (246,5) AND (VALUE1 LIKE '%mentor%' OR VALUE2 LIKE '%mentor%') |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-25 : 01:24:31
|
quote: Originally posted by dmoonme Sample dataSURVEYID,FIELDID,VALUE1,VALUE2,VALUE320,5,Mentor,None,NULL20,246,Fluent,Fluent,Fluent21,5,None,None,NULL21,246,Limited,Limited,LimitedThe select statement that's not working, no records are returned.SELECT dbo.SurveyDetails.FIELDID, dbo.SurveyDetails.VALUE1, dbo.SurveyDetails.VALUE2, dbo.SurveyDetails.VALUE3 FROM dbo.SurveyDetails WHERE (dbo.SurveyDetails.FIELDID=246) AND(dbo.SurveyDetails.FIELDID=5 AND (VALUE1 LIKE '%mentor%' OR VALUE2 LIKE '%mentor%'))
Thats the expected result as there're no records matching filter criteria you gave. you've given (dbo.SurveyDetails.FIELDID=246) AND(dbo.SurveyDetails.FIELDID=5 AND (VALUE1 LIKE '%mentor%' OR VALUE2 LIKE '%mentor%'))there are no records in your sample data with FIELDID=246 and FIELDID=5. did you meant or?(dbo.SurveyDetails.FIELDID=246) OR(dbo.SurveyDetails.FIELDID=5 AND (VALUE1 LIKE '%mentor%' OR VALUE2 LIKE '%mentor%'))then you will get these20,5,Mentor,None,NULL20,246,Fluent,Fluent,Fluent21,246,Limited,Limited,Limited |
 |
|
|
dmoonme
Starting Member
13 Posts |
Posted - 2008-06-25 : 01:55:56
|
| The desired result would be20,5,Mentor,None,NULL20,246,Fluent,Fluent,FluentCould I use a subquery to get the desired results? Not sure how this would work. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-25 : 01:58:06
|
quote: Originally posted by dmoonme The desired result would be20,5,Mentor,None,NULL20,246,Fluent,Fluent,FluentCould I use a subquery to get the desired results? Not sure how this would work.
what do you think is your the criteria for getting it? you also have another one with FileID 246 |
 |
|
|
dmoonme
Starting Member
13 Posts |
Posted - 2008-06-25 : 13:53:35
|
| SURVEYID,FIELDID,FIELDNAME,VALUE1,VALUE2,VALUE320,5,Accounting,Mentor,None,NULL20,246,Spanish,Fluent,Fluent,Fluent21,5,Accounting,None,None,NULL21,246,Spanish,Limited,Limited,LimitedI included the fieldname in the sample data for explaination purposes.What I want is someone who can mentor in accounting AND speaks spanish fluently.BTW thanks for your replies. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-25 : 13:59:43
|
quote: Originally posted by dmoonme SURVEYID,FIELDID,FIELDNAME,VALUE1,VALUE2,VALUE320,5,Accounting,Mentor,None,NULL20,246,Spanish,Fluent,Fluent,Fluent21,5,Accounting,None,None,NULL21,246,Spanish,Limited,Limited,LimitedI included the fieldname in the sample data for explaination purposes.What I want is someone who can mentor in accounting AND speaks spanish fluently.BTW thanks for your replies.
ok. i guess this is what you're looking forSELECT * FROM YourTable tINNER JOIN(SELECT SURVEYID FROM YourTableGROUP BY SURVEYIDHAVING SUM(CASE WHEN FIELDNAME='Accounting'AND (VALUE1 LIKE '%mentor%' OR VALUE2 LIKE '%mentor%') THEN 1 ELSE 0 END)>0AND SUM(CASE WHEN FIELDNAME='Spanish' AND (VALUE1 LIKE '%Fluent%' OR VALUE2 LIKE '%Fluent%')) THEN 1 ELSE 0 END)>0)tmpON tmp.SURVEYID=t.SURVEYID |
 |
|
|
dmoonme
Starting Member
13 Posts |
Posted - 2008-06-25 : 14:43:48
|
| thanks visakh16 that works great if value1 and value2 are specified.What if only one field and value1 and value2 are given?In other words, someone who can mentor in accounting and any fluency level in spanish. |
 |
|
|
dmoonme
Starting Member
13 Posts |
Posted - 2008-06-26 : 01:19:56
|
| Maybe I need to use a cursor? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-26 : 01:59:29
|
quote: Originally posted by dmoonme thanks visakh16 that works great if value1 and value2 are specified.What if only one field and value1 and value2 are given?In other words, someone who can mentor in accounting and any fluency level in spanish.
wont the query works for it too. As you are using OR it will return those records having mentor as value for any of fields value1 or value2 with fieldname spannish. |
 |
|
|
|
|
|