Solved

Sql Query using the IN clause..

Posted on 2014-11-25
5
104 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 51

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Viewers will learn how the fundamental information of how to create a table.

632 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