views:

58

answers:

1

I have table in sql as

User | Account
-----+---------
  1  |   25
  1  |   31
  1  |   35
  1  |   44
  1  |   50
  1  |   59 

and output need in as three columns

  1  |   25   |  31
  1  |   35   |  44
  1  |   50   |  59
+1  A: 

Ok, so as u said sql server 2005, I will show you the implementation using rownumber.

Assumptions: 1.) Base group should be Id. Account of ID 1 and ID 2 should not be in the same row.

Solution with Mock data:

SELECT  ID,
        Max(Case When OrderCaluse = 0 Then Account Else NULL END) AS Account1,
        Max(Case When OrderCaluse = 1 Then Account Else NULL END) AS Account2
FROM
( 
    Select  ID,
            Account,
            (RowNum+1)/2 As GroupClause, 
            (RowNum+1)%2 as OrderCaluse
    FROM
    (
        Select  *,
                ROW_NUMBER() Over (Partition by Id order by account) As RowNum
        FRom
        (
            Select 1 as Id, 25 as Account
            Union ALL
            Select 1, 31
            Union ALL
            Select 1, 35
            Union ALL
            Select 1, 44
            Union ALL
            Select 1, 50
            Union ALL
            Select 1, 59
        ) AS UserAccount
    ) AS T
) AS T1
Group By Id,GroupClause

Result:

1   25  31
1   35  44
1   50  59

Hope it helps. For your use, just remove the Inner temp table UserAccount and use your physical table.

Nitin Midha