Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
yoga.redhat
Starting Member
1 Post |
Posted - 2014-07-28 : 14:37:57
|
Child tableI'd. Frame. Lame1. Ymc. M2. xyz. B3. Abc. VTreat. TableTreated. Childid. Treat type6001. 1. Toffee6002. 2. Toffee6004. 3. Toffee6007. 3. Ice creamHow to get a fname and name of person who have both the treat type( toffee and ice cream)Can any one help me get query for that |
|
MichaelJSQL
Constraint Violating Yak Guru
252 Posts |
Posted - 2014-07-28 : 15:14:52
|
There are lost of ways to do this, but this is pretty quick and straight forwardCREATE TABLE #Childtable(ID Int,Fname Varchar(20),Lname Varchar(20))INSERT INTO #ChildtableVALUES(1,'Ymc','M'),(2,'xyz','v'),(3,'Abc','b')CREATE TABLE #TreatTable(Treated int, Childid int, Treattype varchar(20))INSERT INTO #TreatTableVALUES(6001,1,'Toffee'),(6002,2,'Toffee'),(6003,3,'Toffee'),(6007,1,'Ice Cream')SELECT * FROM #TreatTableSELECT * FROM #ChildtableSELECT FNAME FROM #Childtable CTWHERE EXISTS ( SELECT 1 FROM #TreatTable TT WHERE CT.ID = TT.ChildID AND Treattype = 'Toffee' )AND EXISTS ( SELECT 1 FROM #TreatTable TTT WHERE CT.ID = TTT.ChildID AND Treattype = 'Ice Cream' ) |
|
|
|
|
|
|
|