How to fill a 5x5 table in each cell from left to right with query results instead of top to bottom

I am working with SQL Server Reporting Services and need to build a result set that outputs in a 5x5 table and each new result should go into the cell to the right rather than just display from top to bottom.  Alternatively, I could settle for top to bottom as long as I could limit each column to 10 results and the continued results would simply spill over into the next column.

TIA for any expert assistance!
dstjohnjrAsked:
Who is Participating?
 
Mark WillsConnect With a Mentor Topic AdvisorCommented:
Good example, they have it hard coded though, so, not quite as dynamic as returning rowsets... But is does paint your requirement beautifully....

it is a bit challenging to do that in straight SQL - getting it to wrap to the next column...

So, using the product master from Adventureworks as an example, and the Row_Number and NTILE functions from SQL 2005, then we can get all the product names to dynamically work out how many rows and split them into 5 columns alphabetically down column 1 then overflow to column 2 etc... (number of columns ie 5, is a constant - could be what ever number you want, simply change the 5 to be the number of columns)...






 
select row_number 
,max(case when ntile = 1 then name else '' end) as column1 
,max(case when ntile = 2 then name else '' end) as column2 
,max(case when ntile = 3 then name else '' end) as column3 
,max(case when ntile = 4 then name else '' end) as column4 
,max(case when ntile = 5 then name else '' end) as column5 
from (
select name, ntile, ROW_NUMBER() OVER (partition by ntile ORDER BY name) AS ROW_NUMBER
FROM
(
SELECT Name, ntile(5) over (order by name) as ntile
FROM Production.Product 
) a
) b
group by row_number
order by row_number
 
GO

Open in new window

0
 
Mark WillsTopic AdvisorCommented:
not exactly sure what you are after, but there are constructs like crosstabs, pivot and so on in SSRS and SQL 2005. If you could give a simple example - maybe number the cells from 1 to 25 so we can see the sequence ?
0
 
dstjohnjrAuthor Commented:
Basically, here is the type of output I am looking to achieve:

http://www.sos.state.co.us/pubs/electionresults2006G/CO-COUNTY-INDEX.htm

There is a total of 64 results that will be returned from the query in the result set.  I only want to output 16 rows per column, then have the next set of results spill over into the next column.

Hope this is possible, and I am certainly open to any sort of creative ideas / ways to accomplish this, instead of just outputting one long column of results.

TIA!
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
dstjohnjrAuthor Commented:
Thanks Mark!  I'm going to give this a try today and I'll let you know how it goes.  Thanks so much for your expertiise!
0
 
Mark WillsTopic AdvisorCommented:
need to check it out first, and thankyou for posting such an interesting / challenging problem...
0
 
Mark WillsTopic AdvisorCommented:
Any news ? Kind of like this problem, and keen to hear...
0
 
dstjohnjrAuthor Commented:
Hi Mark - Your proposed code / solution was definitely key in helping us to achieve our goal of displaying results spread across multiple columns.  I am posting the code we finally ended up with on our deployed report, which is working flawlessly, thanks to your initial expert assistance!

If you would like to see our final deployment in action, I will be more than happy to share with you, I just cannot post it here, at least not right now.  Please feel free to e-mail me at dennis at veriatech dot com.

Thanks so much for your expert assistance!
select 
	row_number,
	max(case when ntile = 1 then description else '' end) as column1, 
	max(case when ntile = 2 then description else '' end) as column2, 
	max(case when ntile = 3 then description else '' end) as column3, 
	max(case when ntile = 4 then description else '' end) as column4, 
	max(case when ntile = 5 then description else '' end) as column5, 
	max(case when ntile = 1 then countyid else '' end) as column1id, 
	max(case when ntile = 2 then countyid else '' end) as column2id, 
	max(case when ntile = 3 then countyid else '' end) as column3id, 
	max(case when ntile = 4 then countyid else '' end) as column4id, 
	max(case when ntile = 5 then countyid else '' end) as column5id
from (
select countyid, description, ntile, ROW_NUMBER() OVER (partition by ntile ORDER BY description) AS ROW_NUMBER
FROM
(
SELECT CountyID, Description, ntile(5) over (order by description) as ntile
FROM County
) a
) b
group by row_number
order by row_number

Open in new window

0
 
dstjohnjrAuthor Commented:
Thanks again Mark!  You rock!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.