?
Solved

MS SQL - Output to columns equally.

Posted on 2016-07-30
5
Medium Priority
?
83 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 2000 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 51

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 SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

777 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