Solved

Sql Query using the IN clause..

Posted on 2014-11-25
5
100 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 350 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 75 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 46

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 75 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now