Link to home
Start Free TrialLog in
Avatar of dstjohnjr
dstjohnjrFlag for United States of America

asked on

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!
Avatar of Mark Wills
Mark Wills
Flag of Australia image

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 ?
Avatar of dstjohnjr

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of Mark Wills
Mark Wills
Flag of Australia 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
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!
need to check it out first, and thankyou for posting such an interesting / challenging problem...
Any news ? Kind of like this problem, and keen to hear...
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

Thanks again Mark!  You rock!