Solved

MS SQL - Output to columns equally.

Posted on 2016-07-30
5
80 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

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 7

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 7

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 50

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

707 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