Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sql Query using the IN clause..

Posted on 2014-11-25
5
Medium Priority
?
107 Views
Last Modified: 2014-12-02
I have a table.. we'll call Family.
In the table I have these records

XTYPE  ID    Nme      OtherStuff
---- -----  -------   --------------
 MST  01    Clan1      bla bla bla
 SON  01    Clan1       bla XXX YYY
 OTH  01    Clan1      ZZZ sdfafaf
 MST  02    Clan2      asdfafafaf
 OTH  02    Clan2       a2334141414
 MST  03    Clan3      jwj3j3j3j3j3
 SON  03    Clan3      uuuu bbbb cccc


What I am trying to accomplish is this:
I want to get all the "othestuff" data fields (there are really
more than just one) for each MST based upon the presnt of the 'SON'
entry.

So eventuly my query would return
 MST  01    Clan1      bla bla bla
 MST  03    Clan3      jwj3j3j3j3j3

because of the presence of the SON entry.

How can I code this in SQL?  I tried doing it with a

Select * From Faimly
Where XTYPE IN(Select * From Family Where XTYPE='SON')  

But that didn't return the expected results.
0
Comment
Question by:datafarmer
5 Comments
 
LVL 18

Accepted Solution

by:
Simon earned 1400 total points
ID: 40465797
Close...

Select * From Family
Where ID IN(Select ID From Family Where XTYPE='SON')  
AND XTYPE='MST'

It's the ID values that are shared between the 'MST' and 'SON' records.

This assumes you are trying to return the 'MST' records that have a 'SON' record that shares the same ID value, which is what I understand you want to do.
0
 
LVL 9

Assisted Solution

by:sarabhai
sarabhai earned 300 total points
ID: 40466143
CREATE TABLE #Family
(
XTYPE nvarchar(10),
ID nvarchar(5),
Name nvarchar(10),
OtherStuff nvarchar(25)
)

INSERT INTO #Family
SELECT 'MST','01','Clan1','bla bla bla' union all
 SELECT 'SON','01','Clan1','bla XXX YYY' union all
 SELECT 'OTH','01','Clan1','ZZZ sdfafaf' union all
 SELECT 'MST','02','Clan2','asdfafafaf' union all
 SELECT 'OTH','02','Clan2','a2334141414' union all
 SELECT 'MST','03','Clan3','jwj3j3j3j3j3' union all
 SELECT 'SON','03','Clan3','uuuu bbbb cccc'
 
SELECT * FROM #Family

;with family
AS
(
SELECT * FROM #Family WHERE XTYPE = 'MST'
)
SELECT * FROM #family WHERE ID IN (SELECT ID FROM family) AND XTYPE = 'SON'
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40466502
No points please.
Just want to report an error in sarabhai's solution. He swapped 'MST' with 'SON' so the correct should be:
;with family 
 AS
 (
 SELECT * FROM #Family WHERE XTYPE = 'SON'
 )
 SELECT * FROM #family WHERE ID IN (SELECT ID FROM family) AND XTYPE = 'MST'

Open in new window

0
 
LVL 11

Assisted Solution

by:John_Vidmar
John_Vidmar earned 300 total points
ID: 40469709
SELECT	a.*
FROM	Family	a
JOIN	Family	b	ON	a.ID = b.ID
			AND	a.XTYPE = 'MST'
			AND	b.XTYPE = 'SON'

Open in new window

0
 

Author Comment

by:datafarmer
ID: 40475690
I accepted the first one, because it was a basic flaw in my original query.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question