have two tables with the following strcuture :
Test Type
ID
GroupID
Type
Type Group
GroupID
Description
Now type group has lot of data in it, I want only
GroupID which has specific name
so I did....
SELECT * FROM
TypeGroup tg
WHERE tg.Description = 'Test'
OR tg.Description = 'Test1'
Now this gives me two groupID's so far its fine....
Now based on the groupID's I need to get all the types
from Test Type Table and I did something like this :
SELECT Type
FROM TypeGroup tg, TestType tt
WHERE tg.Description = 'Test'
OR tg.Description = 'Test1'
AND tg.GroupID = tt.GroupID
The above where its not happy, How can I perform to get
the desired results :
The result what I'm getting now is everything and some
repetition from TestType table...
Sample Data :
Type Group Table
1, Test
2, Test1
3, Test2
4, Test4
5, Test10
Test Type Table
1, 1, Something
2,1, Something else
3,1, Something different
4, 2, Very Different
5,3,Testing is good
6,3, Do More Testing<br. 7,4,Yield Better Results>
8,5, The better the results, better it is
From the Query I'm looking for is
Something,Somethng else, Something different, Very Different
Thanks a lot for the help.
First, you are using the 'old form' of JOINs. A JOIN should be in the new form for reliability. If I understand your question correctly, this may work for you:
Code Snippet
SET NOCOUNT ON
DECLARE @.TypeGroup table
( TypeGroupID int IDENTITY,
TypeDescrip varchar(20)
)
INSERT INTO @.TypeGroup VALUES ( 'Test' )
INSERT INTO @.TypeGroup VALUES ( 'Test1' )
INSERT INTO @.TypeGroup VALUES ( 'Test2' )
INSERT INTO @.TypeGroup VALUES ( 'Test4' )
INSERT INTO @.TypeGroup VALUES ( 'Test10' )
DECLARE @.TestType table
( TestTypeID int IDENTITY,
TypeGroupID int,
TestDescript varchar(50)
)
INSERT INTO @.TestType VALUES ( 1, 'Something' )
INSERT INTO @.TestType VALUES ( 1, 'Something else' )
INSERT INTO @.TestType VALUES ( 1, 'Something different' )
INSERT INTO @.TestType VALUES ( 2, 'Very Different' )
INSERT INTO @.TestType VALUES ( 2, 'Very Different' )
INSERT INTO @.TestType VALUES ( 3, 'Testing is good' )
INSERT INTO @.TestType VALUES ( 4, 'Testing is good' )
INSERT INTO @.TestType VALUES ( 3, 'Do More Testing' )
INSERT INTO @.TestType VALUES ( 4, 'Yield Better Results' )
INSERT INTO @.TestType VALUES ( 5, 'The better the results, better it is' )
SELECT DISTINCT tt.TestDescript
FROM @.TypeGroup tg
JOIN @.TestType tt
ON tg.TypeGroupID = tt.TypeGroupID
WHERE ( tg.TypeDescrip = 'Test'
OR tg.TypeDescrip = 'Test1'
)
No comments:
Post a Comment