?
Solved

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

Posted on 2008-11-14
8
Medium Priority
?
225 Views
Last Modified: 2012-05-05
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!
0
Comment
Question by:dstjohnjr
  • 4
  • 4
8 Comments
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22966611
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
 

Author Comment

by:dstjohnjr
ID: 22967809
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
 
LVL 51

Accepted Solution

by:
Mark Wills earned 2000 total points
ID: 22970206
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
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.

 

Author Comment

by:dstjohnjr
ID: 22970780
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
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22970813
need to check it out first, and thankyou for posting such an interesting / challenging problem...
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22991922
Any news ? Kind of like this problem, and keen to hear...
0
 

Author Comment

by:dstjohnjr
ID: 22995924
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
 

Author Closing Comment

by:dstjohnjr
ID: 31516976
Thanks again Mark!  You rock!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

850 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