Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with LIKE query

Author  Topic 

dmoonme
Starting Member

13 Posts

Posted - 2008-06-24 : 21:51:11
Sample data

SURVEYID,FIELDID,VALUE1,VALUE2,VALUE3
20,5,Mentor,None,NULL
20,246,Fluent,Fluent,Fluent
21,5,None,None,NULL
21,246,Limited,Limited,Limited

The 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%')
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-25 : 01:24:31
quote:
Originally posted by dmoonme

Sample data

SURVEYID,FIELDID,VALUE1,VALUE2,VALUE3
20,5,Mentor,None,NULL
20,246,Fluent,Fluent,Fluent
21,5,None,None,NULL
21,246,Limited,Limited,Limited

The 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 these

20,5,Mentor,None,NULL
20,246,Fluent,Fluent,Fluent
21,246,Limited,Limited,Limited
Go to Top of Page

dmoonme
Starting Member

13 Posts

Posted - 2008-06-25 : 01:55:56
The desired result would be

20,5,Mentor,None,NULL
20,246,Fluent,Fluent,Fluent

Could I use a subquery to get the desired results? Not sure how this would work.

Go to Top of Page

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 be

20,5,Mentor,None,NULL
20,246,Fluent,Fluent,Fluent

Could 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
Go to Top of Page

dmoonme
Starting Member

13 Posts

Posted - 2008-06-25 : 13:53:35
SURVEYID,FIELDID,FIELDNAME,VALUE1,VALUE2,VALUE3
20,5,Accounting,Mentor,None,NULL
20,246,Spanish,Fluent,Fluent,Fluent
21,5,Accounting,None,None,NULL
21,246,Spanish,Limited,Limited,Limited

I 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.


Go to Top of Page

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,VALUE3
20,5,Accounting,Mentor,None,NULL
20,246,Spanish,Fluent,Fluent,Fluent
21,5,Accounting,None,None,NULL
21,246,Spanish,Limited,Limited,Limited

I 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 for
SELECT * FROM YourTable t
INNER JOIN
(SELECT SURVEYID
FROM YourTable
GROUP BY SURVEYID
HAVING SUM(CASE WHEN FIELDNAME='Accounting'
AND (VALUE1 LIKE '%mentor%' OR VALUE2 LIKE '%mentor%')
THEN 1 ELSE 0 END)>0
AND SUM(CASE WHEN FIELDNAME='Spanish'
AND (VALUE1 LIKE '%Fluent%' OR VALUE2 LIKE '%Fluent%'))
THEN 1 ELSE 0 END)>0
)tmp
ON tmp.SURVEYID=t.SURVEYID
Go to Top of Page

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.
Go to Top of Page

dmoonme
Starting Member

13 Posts

Posted - 2008-06-26 : 01:19:56
Maybe I need to use a cursor?
Go to Top of Page

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.
Go to Top of Page
   

- Advertisement -