Solved

Sql Query using the IN clause..

Posted on 2014-11-25
5
95 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:
SimonAdept earned 350 total points
Comment Utility
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
Comment Utility
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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
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
Comment Utility
I accepted the first one, because it was a basic flaw in my original query.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Viewers will learn how the fundamental information of how to create a table.

743 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

15 Experts available now in Live!

Get 1:1 Help Now