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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Mark WillsTopic 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB Reporting Tools

From novice to tech pro — start learning today.