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 |
|
fluffywalrus
Starting Member
1 Post |
Posted - 2011-10-01 : 13:12:06
|
| Any help would be appreciated with the following:I have several tables that all have the same two columns, position and value, such that position is the key and both are non null. Position is simply the row number starting at 1 and value is text of variable length. I am looking for an alternative to nesting full joins due to running out of memory in trying to create the result set below.T11 A2 B3 DT21 B2 C3 ET31 B2 Fresult setT1value T2value T3valueA null nullB B Bnull C nullD null nullnull E nullnull null FMy goal is to line up any overlaps between two or more tables while keeping each column in the result set in ascending order by the corresponding table's position. I am completely open to changing the original tables if that makes sense. One attempt that partially worked in Oracle was to union all the individual tables and then use a combination of fname and the decode function to distribute the unionized table across the desired columns. Again any ideas are appreciated and if any clarification would help ask away. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-01 : 13:54:11
|
| [code]DECLARE @T1 table(position int,val char(1))insert @T1SELECT 1, 'A' union allSELECT 2, 'B' union allSELECT 3, 'D'DECLARE @T2 table(position int,val char(1))insert @T2SELECT 1, 'B' union allSELECT 2, 'C' union allSELECT 3, 'E' DECLARE @T3 table(position int,val char(1))insert @T3SELECT 1, 'B' union allSELECT 2, 'F'SELECT [T1value],[T2value],[T3value]FROM(SELECT DENSE_RANK() OVER (ORDER BY val) AS Rnk,val,catFROM(SELECT val,'T1value' AS CatFROM @T1UNION ALLSELECT val,'T2value' FROM @T2UNION ALLSELECT val,'T3value'FROM @T3)t)rPIVOT (MAX(Val) FOR Cat IN ([T1value],[T2value],[T3value]))poutput-----------------------------------------T1value T2value T3valueA NULL NULLB B BNULL C NULLD NULL NULLNULL E NULLNULL NULL F[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|