Solved

Sql Query using the IN clause..

Posted on 2014-11-25
5
102 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 50

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ACESS 2010 Query Criteria 23 57
2 Global Vars, 1 List Box 4 34
Sql Query to return all values in Capital letters 2 17
Query Task 8 23
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
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…

710 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