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.

"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.

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 ?

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_NUMBERFROM(SELECT Name, ntile(5) over (order by name) as ntileFROM Production.Product ) a) bgroup by row_numberorder by row_numberGO

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.

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.