Solved

SQL Server - generating pivot table-like views

Posted on 2012-03-21
11
383 Views
Last Modified: 2012-06-22
Example of tablesThe attached file shows a simple example of the problem I'm trying to resolve.
(My actual problem is one where the number of rows and columns is closer to hundreds or thousands of rows / columns, so I could do with a solution which is not just specific to this case)

I start with a table (Format A) where:
- the names of products (cars in the example) are the column titles;
- the names of attributes (colour, age, engine size etc) are the row titles;
- the values of those attributes are in the main body of the table and may be null.

To save this in SQL Server, I transform the data to a simple table of three columns (Format B):
- car
- attribute_name
- attribute_value
(all varchars)

The two problems I need to solve are:
(1) How can I display the SQL data back in the original format (Format A)? (both cars and attribute names can be in any order)
(2) I need to group the data according to common combinations of attributes.  e.g.
 select colour, engine, age FROM theTable group by colour, engine, age

BUT!!!! in both cases, the solution needs to be dynamic, so the views/SPs have to determine the list of attributes to group on (and list of cars to select from) at runtime.
My actual case will expand by number of attributes and number of procucts (cars) fairly regularly, so hard coding attribute names or car names into the SP or query is not feasible.

(PS - this topic shouldn't be listed under asp.net but for some reason in IE9, I can't deselect this!)
0
Comment
Question by:Beamson
  • 5
  • 4
11 Comments
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 500 total points
ID: 37746678
0
 
LVL 1

Author Comment

by:Beamson
ID: 37746705
Thanks for this.

I've given it a quick look and the dynamic version does look interesting.
However, my limited understanding of pivot tables is that they only do grouped/totalled (numeric) values in the body of the table.

I'll need to spend a little time giving it a go, but if its as simple as shown, I'll be over the moon!
0
 
LVL 1

Accepted Solution

by:
Beamson earned 0 total points
ID: 37747038
The solution is close enough for me.
I couldn't quite follow it but it pointed me in the right direction to start looking at the PIVOT function.

In the end, I found a simpler example, which, whilst it hard codes the possible values for the columns, is easier to grasp.

http://blogs.msdn.com/b/spike/archive/2009/03/03/pivot-tables-in-sql-server-a-simple-sample.aspx

To go from there to doing it dynamically is just a case of generating the
"IncomeDay in ([MON],[TUE],[WED],[THU],[FRI],[SAT],[SUN])) " statement first, then executing it.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37747044
Glad you got the solution, please close this question!!!

Happy Programming
:-)
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 1

Author Comment

by:Beamson
ID: 37747098
I've requested that this question be closed as follows:

Accepted answer: 0 points for Beamson's comment #37747038
Assisted answer: 500 points for BuggyCoder's comment #37746678

for the following reason:

If you already have a pretty good understanding of FOR XML, PIVOT and temporary tables, the other ExpertsExchange thread given by BuggyCoder will probably point you in the right direction.<br /><br />However, if you want to understand the basics behind how it works and are struggling to piece things together, I think the blogs.msdn.com link gets the basics over pretty clearly, from which point, making this dynamic is a relatively small step.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37747099
Have provided the solution that he was able to refer to and finally get his solution from. I think i deserve some points here....
0
 
LVL 1

Author Comment

by:Beamson
ID: 37747350
Er.  You've got all of them.
What is the problem?
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37747365
sorry i didn't see your comments before posting!!!
 
Frankly, I expected you to rate the answer and close, but you have left it on moderators to close it.
Anyways fine with me, the idea is that problem should be resolved and it has(I hope)...

Keep Well
Happy Programming :-)
0
 
LVL 1

Author Comment

by:Beamson
ID: 37747487
No problem!

Maybe I'm doing something wrong - I thought awarding points closes a post.
I rated your answer "A".

Then I received an e-mail from Experts Exchange to say you had reopened it and had requested a moderator.  Not sure what else I can do.  Guess we wait for a moderator to sort it out...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now