Solved

SQL Server - generating pivot table-like views

Posted on 2012-03-21
11
380 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

705 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