Solved

How can I rotate cells with data to columns in a single query?

Posted on 2008-10-12
6
397 Views
Last Modified: 2013-11-16
I want to turn/rotate certain information that are displayed in joined rows, so they will become extra columns in the query output.

To illustrate this problem, I have set up an example in which I make use of a small database that contains 3 tables: Products, ProductGroup and Projects. (see the image).
 
Each time a project is done, different products are used or sold. These are registered in the Product table and are linked to the Project, with the ProjectID. The ProductGroupID in the Product table indicates what kind of group this Product belongs to.

To each project,  products are linked that represent costs that are of a certain type and therefore belong to a product group.

I would like to generate an overview of the Projects, where the product groups are dynamically added as columns, to specify the different costs per project. On the image you can see an example of the desired output.
 
However, there are some additional functionality requirements:
1. The product groups that are be displayed as additional columns in the result, should be selected dynamically, by an parameterized query. In this example there are 2 cost groups; in reality there are hundred different product groups that need to be displayed in various ways.
2. Not all the product groups are filled with costs all the time. Hence the absence of travel costs for the project Cloud puffing in this example.

My ideas:
I have thought of several solutions; but they are either too memory consuming, esp. with large selections, or werent meeting up to one the above condition:
1. Use union statements to add extra columns to the result. In this experiment, it proved to become a cluttered SQL string, that wasnt exactly nicely scalable.
2. I have loaded all Products into an array in ASP.NET first and then iterated through a list of Projects, each time testing if the Product perhaps belonged to both that Project as well as a selected product group. This works okay for small lists, but is quite heavy on the processing and needs a lot of programmatic code.

It would be great to get some hints for directions to take in this puzzle.
xx.jpg
0
Comment
Question by:TinoNL
  • 3
  • 3
6 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22697852
What version of SQL Server are you using?  The zone suggests SQL 2000, but just want to confirm.
0
 

Author Comment

by:TinoNL
ID: 22698773
Oh, ik work on both MS SQL2000 as MS SQL2005 platforms. If 2005 with .NET would allow certain technique (some xml output or so?) that would enable a certain solution, then it's no bother migrating for good to 2005.  
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22699121
SQL 2005 has PIVOT keyword that works pretty well other than the fact you must hardcode the column names; however, you can mitigate this by using Dynamic SQL statement or creating SQL Statement in your .NET code based on the values from the table you want to pivot as columns.

http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:TinoNL
ID: 22700930
Thank you for pointing me to this article; it seems like a nice push in the right direction. Downside is hardcoding the column names;
After some time of SQL programming, dynamic SQL always seems to be the easy way out to me. At least; in the end I always came down to static, parameterized equivalents, that proved faster and more readable.  
For generating SQL from .NET; I preferrably use Stored Procedures; a lot of data filtering already needs to be done, before I can start pivotting; this brings me back to the previous arguments.
I have to dive into the thing, but I'm concidering combining this pivot statement with some sort of temporary table that have values for the columns.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 22701092
You could always have your stored procedure update a temp/working table or view and then you return distinct values of the column data to be pivoted to VB then use that to construct query to PIVOT view.

A lot of steps, but allows you to use stored procedure for bulk of T-SQL and then use VB.NET for automation of the hardcoding of columns for representation of data.

Alternative is to pass the data through Access.  You can use the crosstab wizard of manually code with TRANSFORM and PIVOT statements -- don't believe you have to hardcode column names, but maybe I am wrong -- know I didn't have to with crosstab wizard.
0
 

Author Closing Comment

by:TinoNL
ID: 31505418
Cool; I have quite the homework to dive into;) Thanks a lot!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
kill process lock Sql server 9 46
query execution hang 5 28
Mysql Left Join Case 10 48
Increment column based of a FK 8 20
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

786 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