June 7, 2005 at 8:38 am
I hav a table like this Table T (ID,ID2,Data) Values((1,N,Data) ,(2,N,Data ),(3,C,Data ), (4,N,Data5 ) ,(5,C,Data) ,(6,N,Data7))
Now I Want to get result set like 1 - 3 , 2-5
Since Data Is Same (Inner Join Attributes) And ID2 are opposite (C Vs N) Needed.
But after self join result is like 1 - 3 , 2 - 5 , 1 - 5 , 2 - 3 . Can anyone tell me how to stop multiple combination from this .
June 7, 2005 at 10:29 am
One idea could be to use a tmp table where you insert your result from your inner join and then delete the results you do not want...
June 7, 2005 at 10:44 am
The question is now : how do you know which records that you do no want?
June 8, 2005 at 5:49 am
Hi Remy ,
Right Question , But i want any one combination . When one combination is mapped, there should not be another combination As 1 - 3, 2 - 5, 1 - 5 , 2 - 3 in resultset. By Loop my senior has resolved this problem . Any other method will be appriciated.
Thanks
June 8, 2005 at 7:07 am
Maybe if you can answer this :
how do you invalidate 1 - 5 , 2 - 3 from
1 - 3 , 2 - 5, 1 - 5 , 2 - 3
??
This is a pretty simple self join, but I join can't picture the final condition. Help!!
June 8, 2005 at 1:38 pm
SET NOCOUNT ON
DECLARE @T Table (ID int, ID2 char(1) , Data varchar(10))
insert into @T (ID,ID2,Data) Values(1,'N','Data')
insert into @T (ID,ID2,Data) Values(2,'N','Data' )
insert into @T (ID,ID2,Data) Values(3,'C','Data' )
insert into @T (ID,ID2,Data) Values(4,'C','Data5' )
insert into @T (ID,ID2,Data) Values(5,'C','Data')
insert into @T (ID,ID2,Data) Values(6,'N','Data7')
insert into @T (ID,ID2,Data) Values(7,'N','Data5' )
select * from @t
--SELECT AA.LineNumber,AA.ID2, AA.Data,AA.ID,BB.LineNumber,BB.ID2, BB.Data,BB.ID
SELECT AA.ID,BB.ID,AA.ID2, BB.ID2, BB.Data
FROM
(SELECT COUNT(*) LineNumber, A.ID2 ,A.Data, A.ID
FROM @T A join @T B on A.ID>=B.ID AND A.ID2=B.ID2 AND A.Data=B.Data
where A.Id2='N' AND B.ID2='N'
Group by A.Data,A.ID2,A.ID) AA
INNER JOIN
(SELECT COUNT(*) LineNumber, A.ID2 ,A.Data, A.ID
FROM @T A join @T B on A.ID>=B.ID AND A.ID2=B.ID2 AND A.Data=B.Data
where A.Id2='C' AND B.ID2='C'
Group by A.Data,A.ID2,A.ID) BB
ON AA.LineNumber=BB.LineNumber and AA.Data=BB.Data and AA.ID2<>BB.ID2
Vasc
June 8, 2005 at 2:14 pm
Wow... can you explain the logic of this one??
June 8, 2005 at 2:25 pm
The catch is to label the rows for each DATA and ID2 with Ordered Numbers and match the rows with diff ID2 but the same Data and RowNumber
Vasc
June 8, 2005 at 2:28 pm
I'll sleep on it and watch it again in the morning .
June 9, 2005 at 12:28 am
Hi Rookie,
Fantastic ! U hav made proper solution .
Thanks Again.
June 9, 2005 at 8:20 am
What would the query look like if you had C,N,Y,O as possible id2?
Would you have 4 derived tables now?
June 9, 2005 at 10:28 am
Won't work to have 4 derived tables.
The solution is based on comb of two diff sets.
so if C,N,Y,O form 2 sets the sol will work
ex: C,N can be combined with either Y,O but not with other C,N value Sol will work
If you further say that you can conbine C with N won't work.
Vasc
June 9, 2005 at 11:16 am
I see, this is the kindof solution that can be usefull but on a small scale... Thanks again for the info.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply