SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


DEMO_CODE = 'DE1'


DEMO_CODE = 'DE1'

Author
Message
RVO
RVO
SSC Eights!
SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)

Group: General Forum Members
Points: 850 Visits: 517
Hi everyone.
I have these two records in [prj_demo_data] table:

prj_code        demo_code    decmo_vchar_data
---------------------------------------------
1425 DE1 U
1425 RAP YES
1426 DE1 B



How do I select only those records where the same project has both:

demo_code = 'DE1' and demo_vchar_data = 'U'
and
demo_code = 'RAP' and demo_vchar_data = 'YES'
ChrisM@Work
ChrisM@Work
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21526 Visits: 19689
SELECT [stuff]
FROM [thing]
WHERE
(demo_code = 'DE1' AND demo_vchar_data = 'U')
OR
(demo_code = 'RAP' AND demo_vchar_data = 'YES')



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Koen Verbeeck
Koen Verbeeck
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34181 Visits: 13270
SELECT *
FROM MyTable WHERE prj_code IN
(
SELECT prj_code
FROM MyTable
WHERE demo_code = 'DE1' and demo_vchar_data = 'U'
INTERSECT
SELECT prj_code
FROM MyTable
WHERE demo_code = 'RAP' and demo_vchar_data = 'YES'
)
AND demo_code IN ('DE1','RAP') AND demo_vchar_data IN ('U','YES');




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
RVO
RVO
SSC Eights!
SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)

Group: General Forum Members
Points: 850 Visits: 517
God Bless you , Koen Verbeeck !

First I thought what an easy task...and then I felt embarrassed when after 3 hours I was still not able
to select those pairs.

I need to read about this INTERSECT to understand how it works...

Thank you so much!
RVO
RVO
SSC Eights!
SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)SSC Eights! (850 reputation)

Group: General Forum Members
Points: 850 Visits: 517
Hello Koen,

I just noticed this returns the same results.
Do I really need this last line ?

SELECT *
FROM prj_demo_data
WHERE prj_code IN
(
SELECT prj_code
FROM prj_demo_data
WHERE demo_code = 'DE1' and demo_data_vchar = 'U'
INTERSECT
SELECT prj_code
FROM prj_demo_data
WHERE demo_code = 'RAP' and demo_data_vchar = 'YES'
)
--code works even without this line?...
--AND demo_code IN ('DE1','RAP') AND demo_data_vchar IN ('U','YES')


Koen Verbeeck
Koen Verbeeck
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34181 Visits: 13270
For the test data you provided, the last line is not necessary. I added it as an extra check.
Say for example project 1425 has a third line with demo_code DE2, than this extra row is not returned.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search