Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Yet another rows to column question (sorry) Expand / Collapse
Author
Message
Posted Tuesday, August 14, 2012 7:45 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, September 11, 2014 3:28 AM
Points: 571, Visits: 594
Hi,

I have read Jeff Moden's series on converting rows to columns but can't see how to do what I need to do. The scenario is this:

I have two fields - Model & Feature, which represents cars (models) that have features (i.e. 3 doors, air-conditioning etc.), and the data is transactional, so if a Model hs more than one feature there is more than one row.

The question is - how do I get the Features into columns, and here's the rub - the number of features is not fixed, and they are all different. SO what I'd like is:

Model A | Feature 1 | Feature 2
Model B | Feature 1 | Feature 2 | Feature 3

and Feature 1 on Model A is not necessarily the same as Feature 1 on Model B.

Is this do-able? All assistance *gratefully* received.

Jason
Post #1344720
Posted Tuesday, August 14, 2012 7:55 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 7:18 AM
Points: 1,247, Visits: 9,875
Knocking up some easily consumable sample data would help people to provide better answers, but what you're trying to do would require either dynamic SQL or picking an arbitrary maximum number of features to pivot/crosstab.

You can use the Row_Number() function, partitioned by model in order to assign a number to each feature.

Jeff's series does cover dynamic CrossTabs, so it pretty much has everything you need:

http://www.sqlservercentral.com/articles/Crosstab/65048/
Post #1344728
Posted Tuesday, August 14, 2012 8:14 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 3, 2013 8:24 AM
Points: 1,240, Visits: 5,421
If you're trying to do what I think you are, then the answer is that you can't do it in SQL. First Normal Form for databases is that all records must have the same shape. That is, all of the rows have to have the same number of columns. Excel lets you violate this, because it's not a database application, but SQL Server won't let you violate First Normal Form. All of the rows must have the same number of columns.

You can get around this in a couple of ways.
* You could include NULL values for any additional columns
* You could store all of the features in a single column
* You could produce an XML document instead.

Drew


J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Post #1344741
Posted Tuesday, August 14, 2012 8:18 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 10:30 AM
Points: 2,848, Visits: 5,095
drew.allen (8/14/2012)
If you're trying to do what I think you are, then the answer is that you can't do it in SQL. First Normal Form for databases is that all records must have the same shape. That is, all of the rows have to have the same number of columns.
...


Until you are happy to get something like:

Model A | Feature 1 | Feature 2 | NULL
Model B | Feature 1 | Feature 2 | Feature 3

Which you can do in SQL as all rows will have same number of columns, just not all of them will be populated...



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1344748
Posted Tuesday, August 14, 2012 8:21 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, September 11, 2014 3:28 AM
Points: 571, Visits: 594
< sound of penny dropping >

Drew, you've given me the solution - concatenate the Features (with a delimiter) in one column.

As the purpose is essentially to populate an excel file using MS Query, I can have the SP dump into a hidden sheet and write a quick VBA script to parse it back into columns using the delimiter . . . . Sorted.

Many thanks,

jason
Post #1344753
Posted Tuesday, August 14, 2012 11:16 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, November 30, 2012 3:46 AM
Points: 18, Visits: 100
Hi,
Have you gone through the following post.I had a similar problem.



http://www.sqlservercentral.com/Forums/Topic1322992-392-1.aspx




Regards
Ravi T
Post #1344902
Posted Tuesday, August 14, 2012 11:32 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 5:04 PM
Points: 23,396, Visits: 32,228
santa326 (8/14/2012)
Hi,
Have you gone through the following post.I had a similar problem.



http://www.sqlservercentral.com/Forums/Topic1322992-392-1.aspx




Regards
Ravi T


Make it easy on others:

http://www.sqlservercentral.com/Forums/Topic1322992-392-1.aspx



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1344910
Posted Wednesday, August 15, 2012 12:32 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, September 11, 2014 3:28 AM
Points: 571, Visits: 594
Thanks everyone. I've found a solution. I've added Row_Numbers partitioned by Model, and then used CASE and the rownumbers to return the Features into columns - easy when you know how

Thanks again everyone,

J
Post #1345138
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse