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.
Author |
Topic |
jocaro
Starting Member
2 Posts |
Posted - 2012-03-09 : 07:34:12
|
helloI have the following tables. The child table is subordinate to the main (with CodeOne key)Principal CodeOne (integer) 1 2 . 4 . SecondaryCodeOne (integer) - CodeTwo (integer)1 - 1.....4 - 74 - 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 s1where 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. |
|
|
jocaro
Starting Member
2 Posts |
Posted - 2012-03-11 : 06:20:15
|
quote: Originally posted by webfred select distinct CodeOne from secondary s1where 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.
HelloThank you very mucho for your help |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-11 : 11:39:12
|
[code]select CodeOne from secondarywhere codeTwo IN (3,4,7)group by codeOnehaving (min(codeTwo) = 3AND min(codeTwo)=max(codeTwo))OR (min(codeTwo) = 4AND COUNT(DISTINCT codeTwo)=2)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
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 s1where 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.
HelloThank you very mucho for your help
welcome No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
|
|
|