By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,413 Members | 1,594 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,413 IT Pros & Developers. It's quick & easy.

T-SQL Problem

P: n/a
Hi All,

Sorry if this seems incredibly basic but what is the syntax to solve
this query. I have a table (ENC_UserDefinedData) with 4 columns, ID,
UserID, UserDefinedField and Value. I want to select all the UserIDs
that match this criteria:

[ENC_UserDefinedData].[UserDefinedField] = 8
AND
[ENC_UserDefinedData].[Value] LIKE '%Picasso%'

AND

[ENC_UserDefinedData].[UserDefinedField] = 9
AND
[ENC_UserDefinedData].[Value] = '48'

Thanks in advance,

Jose

Mar 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
UserDefinedField cannot be simultaneously 8 and 9. Therefore, you'll get no
rows. However, if you want *either* of the two pairs of criteria, then use:

SELECT
*
FROM
UserDefinedData
WHERE
(
[ENC_UserDefinedData].[UserDefinedField] = 8
AND
[ENC_UserDefinedData].[Value] LIKE '%Picasso%'
)
OR
(
[ENC_UserDefinedData].[UserDefinedField] = 9
AND
[ENC_UserDefinedData].[Value] = '48'
(
--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"Jose" <di*********@avandis.co.uk> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Hi All,

Sorry if this seems incredibly basic but what is the syntax to solve
this query. I have a table (ENC_UserDefinedData) with 4 columns, ID,
UserID, UserDefinedField and Value. I want to select all the UserIDs
that match this criteria:

[ENC_UserDefinedData].[UserDefinedField] = 8
AND
[ENC_UserDefinedData].[Value] LIKE '%Picasso%'

AND

[ENC_UserDefinedData].[UserDefinedField] = 9
AND
[ENC_UserDefinedData].[Value] = '48'

Thanks in advance,

Jose

Mar 21 '06 #2

P: n/a
Thanks for your reply Tom.

I would like that all conditions are met - let me explain. In words I
would like "get me the users who answered "apple" for questions 5 AND
answered "red" for question 3". Each row in my table contains an answer
to a question.

Do you have any more suggestions?

Kind regards,
Jose

Mar 21 '06 #3

P: n/a
That's different. You're looking at what's called Relational Division:

select
UserID
from
ENC_UserDefinedData
where
(
[ENC_UserDefinedData].[UserDefinedField] = 8
AND
[ENC_UserDefinedData].[Value] LIKE '%Picasso%'
)
OR
(
[ENC_UserDefinedData].[UserDefinedField] = 9
AND
[ENC_UserDefinedData].[Value] = '48'
)
group by
UserID
having
count (*) >= 2

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"Jose" <di*********@avandis.co.uk> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Thanks for your reply Tom.

I would like that all conditions are met - let me explain. In words I
would like "get me the users who answered "apple" for questions 5 AND
answered "red" for question 3". Each row in my table contains an answer
to a question.

Do you have any more suggestions?

Kind regards,
Jose

Mar 21 '06 #4

P: n/a
Excellent!!! That's just what I wanted.

Thanks for your help - it's most appreciated.

Jose

Mar 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.