Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Combine two tables into rows and columns

Author  Topic 

KJensen
Starting Member

12 Posts

Posted - 2007-07-09 : 14:01:08
Hi

I need to create a query, which will allow me to have each brand in a column and each productcategory as a row, in order to display a grid of all possible combinations.

Here is a diagram of the tables:


And a script to create them:
http://klausjensen.dk/images/CreateTables.txt

...And another script to insert some testdata:
http://klausjensen.dk/images/TestData.txt

This is the resultset, I would like to get to:


What is the best and easiest way to create a query, that has a resultset as illustrated?

Please note there is a dynamic amount of rows in both tables, so I can't use the methods I have seen for creating summariezed sales reports, ie. this: http://www.stephenforte.net/owdasblog/PermaLink.aspx?guid=2b0532fc-4318-4ac0-a405-15d6d813eeb8

Thanks in advance

- Klaus

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2007-07-09 : 14:36:35
The absolute, hands-down, no-question, best-practices method of handling this is to do it in the presentation layer!
Access, Crystal Reports, Active Reports, Reporting Services, Excel...all have simple methods for pivoting data. SQL is not a reporting environment. When you create dynamic pivot result sets like this you end up with unpredictable output schemas, and that makes the output useless for most third party tools.

e4 d5 xd5 Nf6
Go to Top of Page

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2007-07-10 : 08:27:48
Crystal's Cross Tab Report is much useful in such cases. Try to implement Cross Tab Report.


Mahesh
Go to Top of Page
   

- Advertisement -