Click here to Skip to main content
15,895,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have a table, having data like this..



Id Address Name
1 Street A John
2 Street B kim


CSS
I want to display the result like

Id_1    Address_1       val_1       Id_2       Address_2    Val_2
1   Street A    John         2         Street_B          Kim



I have tried with joins and pivot but no result..
Please help me..

Thanks in advance.
Posted

1 solution

You could do it with a Cartesian product of the table with itself, then filter out records where ID1 = ID2

select * from Table T1 cross join Table T2 where t1.Id <> t2.Id
 
Share this answer
 
Comments
njdcjk 18-Jul-14 3:53am    
I want everything to be in a single row
Sprint89 18-Jul-14 4:19am    
You could do it in dynamic SQL, get the number of records, build a string in a loop that would have that number of records, add a where clause and exec the string. Pretty ugly though

http://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/
njdcjk 18-Jul-14 5:14am    
Sorry.I think u did'nt get my requirement properly.
Here is my table's structure..With three columns

Id Address Name
1 Street A John
2 Street B kim


And the expected output is like this:



Id_1 Address_1 val_1 Id_2 Address_2 Val_2
1 Street A John 2 Street_B Kim


For example purpose i have taken two columns only....but this will be more and the column names should come accordingly..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900