Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server - generating pivot table-like views

Posted on 2012-03-21
11
Medium Priority
?
396 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

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

Happy Programming
:-)
0
 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

972 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