Solved

MS SQL - Output to columns equally.

Posted on 2016-07-30
5
54 Views
1 Endorsement
Last Modified: 2016-08-10
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.
1
Comment
Question by:egarrison
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41735900
i think this expression
( row_number() over ( order by colum ) -1) / 4
will return 4 riws with the same value...
1
 
LVL 6

Expert Comment

by:bcnagel
ID: 41736873
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

0
 
LVL 6

Accepted Solution

by:
bcnagel earned 500 total points
ID: 41736876
If my assumptions in the comment above are correct, you should be able to do something like the following.


Create a sample table:
select *
	into #SampleData
	from (
		values 
		(1000),
		(1001),
		(1002),
		(1003),
		(1004),
		(1005),
		(1006),
		(1007),
		(1008),
		(1009),
		(1010),
		(1011)
		) as der(Val)

Open in new window


Use row_number() and dense_rank() to group and display:
;with cte_RowNumberAndRank as (
	select der.Val, der.OverallRowNumber, dense_rank() over(order by (OverallRowNumber-1)/6) as [Rank]
		from (
			select *, row_number() over(order by Val) as OverallRowNumber
				from #SampleData s
			) as der
	)

,cte_RowInRank as (
	select r.Val, r.[Rank], row_number() over(partition by [Rank] order by r.OverallRowNumber) as RowInRank
			from cte_RowNumberAndRank r
	)

-- 
select der.[Rank] as GroupNumber, der.OutputRow, 
	max(case when der.RowInRank in (1,2) then Val end) as C1,
	max(case when der.RowInRank in (3,4) then Val end) as C2,
	max(case when der.RowInRank = 5 then Val end) as C3,
	max(case when der.RowInRank = 6 then Val end) as C4
	from (
		select rr.Val, rr.[Rank], rr.RowInRank, 
			case when rr.RowInRank in (1,3,5,6) then 1 else 2 end as OutputRow
			from cte_RowInRank rr
		) der
	group by [Rank], OutputRow
	order by [Rank], OutputRow

Open in new window

1
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41742487
egarrison, do you still need help with this question?
0
 
LVL 3

Author Closing Comment

by:egarrison
ID: 41751349
Great logic.  I ended up using a slightly modified version of this. Thanks.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server - SQL field is defined as text 3 26
TSQL previous 5 22
TSQL - IF ELSE? 3 25
Help Extract Specific in SQL 8 16
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now