Link to home
Start Free TrialLog in
Avatar of egarrison
egarrisonFlag for United States of America

asked on

MS SQL - Output to columns equally.

I am trying to convert input data like this:
1000
1001
1002
1003
1004
1005



then output as:

C1                               C2                           C3                               C4  
1000                          1002                          1004                            1005     
1001                          1003

Open in new window


This will be in a Stored Procedure.
So far, I am dividing the number of results to see:
6 Nodes / 4 = 1 full row.

I do 2 options for output. I either have the data in the 4 columns, or I assign a column identity to it such as C1R1, C2R1

The end output from this is actually a print file and the columns need to fill them in this fashion when there are 6 or 600 items.
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

i think this expression
( row_number() over ( order by colum ) -1) / 4
will return 4 riws with the same value...
Is it fair to assume that the input data coming in is always in numeric order? So, to extend your example with more rows, will we always get this kind of data?

1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011

We won't get smaller numbers coming after larger ones, right? Assuming the above assumption is correct, is it also correct that you want to see the numbers above parsed into rows in the following way?

C1          C2          C3          C4
----------- ----------- ----------- ------
1000        1002        1004        1005
1001        1003                
1006        1008        1010        1011
1007        1009                

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of bcnagel
bcnagel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
egarrison, do you still need help with this question?
Avatar of egarrison

ASKER

Great logic.  I ended up using a slightly modified version of this. Thanks.