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
 Other Forums
 Other Topics
 Is possible this select?

Author  Topic 

jocaro
Starting Member

2 Posts

Posted - 2012-03-09 : 07:34:12
hello

I have the following tables. The child table is subordinate to the main (with CodeOne key)

Principal
CodeOne (integer)


1
2
.
4
.

Secondary
CodeOne (integer) - CodeTwo (integer)


1 - 1
.....
4 - 7
4 - 9

.....

I need to select the main table records that meet the following condition in the child table, in the group of existing records for each CodeOne: (CodeTwo = x1 and CodeTwo = x2) or CodeTwo = x3.

For example: select CodeOne if [(CodeTwo = 7 and CodeTwo = 9) or CodeTwo = 3]. In this case CodeOne = 4 will be selected with data example (codes in red)

Is possible create that select and, if so, what would the syntax

Thanks in advance

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-03-09 : 09:54:56
select distinct CodeOne
from secondary s1
where CodeTwo=3 or (CodeTwo=7 and exists(select * from secondary s2 where s2.CodeOne=s1.CodeOne and s2.CodeTwo=9))


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

jocaro
Starting Member

2 Posts

Posted - 2012-03-11 : 06:20:15
quote:
Originally posted by webfred

select distinct CodeOne
from secondary s1
where CodeTwo=3 or (CodeTwo=7 and exists(select * from secondary s2 where s2.CodeOne=s1.CodeOne and s2.CodeTwo=9))


No, you're never too old to Yak'n'Roll if you're too young to die.



Hello
Thank you very mucho for your help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-11 : 11:39:12
[code]
select CodeOne
from secondary
where codeTwo IN (3,4,7)
group by codeOne
having (min(codeTwo) = 3
AND min(codeTwo)=max(codeTwo))
OR (min(codeTwo) = 4
AND COUNT(DISTINCT codeTwo)=2)
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-03-12 : 06:49:39
quote:
Originally posted by jocaro

quote:
Originally posted by webfred

select distinct CodeOne
from secondary s1
where CodeTwo=3 or (CodeTwo=7 and exists(select * from secondary s2 where s2.CodeOne=s1.CodeOne and s2.CodeTwo=9))


No, you're never too old to Yak'n'Roll if you're too young to die.



Hello
Thank you very mucho for your help


welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -