Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
[code]tbl_matches[ID] [Name] [Paired_To]1 Name1 22 Name2 13 Name3 44 Name4 3i tried doing this query select m.[name] , m1.[Name] as Name1 from tbl_matches m inner join tbl_matches m1 on m1.paired_to = m.idit would display like this... [Name] [Name1] ----------------------------------- Name1 Name2 Name2 Name1 Name3 Name4 Name4 Name3the desired result should be like this... [Name] [Name1] ----------------------------------- Name1 Name2 Name3 Name4 what would be the correct query? please help.... [/code]TCC
tishri
Yak Posting Veteran
95 Posts
Posted - 2006-09-02 : 09:30:40
[code]declare @tblmatches table([id] int, [name] varchar(50), [paired_to] int)insert into @tblmatches select 1,'Name1',2union all select 2, 'Name2', 1union all select 3, 'Name3', 4union all select 4, 'Name4', 3select m.[name] , m1.[Name] as Name1 from @tblmatches m inner join @tblmatches m1 on m1.paired_to = m.id[/code]TCC
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts
Posted - 2006-09-02 : 09:33:23
Something like this...
declare @tbl table( id int, name varchar(20), paired_to int)insert into @tblselect 1, 'Name1', 2 union allselect 2, 'Name2', 1 union allselect 3, 'Name3', 4 union allselect 4, 'Name4', 3select * from (select m.[id], m.[name] , m1.[id] as [id1], m1.[Name] as Name1 from @tbl m inner join @tbl m1 on m1.paired_to = m.id) as test where test.id < test.id1