Groups | Blog | Home
all groups > sql server (alternate) > march 2006 >

sql server (alternate) : T-SQL Problem


Jose
3/20/2006 4:18:57 PM
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
Tom Moreau
3/20/2006 7:37:28 PM
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
..
[quoted text, click to view]
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
Tom Moreau
3/21/2006 12:00:00 AM
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
..
[quoted text, click to view]
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
Jose
3/21/2006 1:10:30 AM
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
Jose
3/21/2006 9:56:02 AM
Excellent!!! That's just what I wanted.

Thanks for your help - it's most appreciated.

Jose
AddThis Social Bookmark Button