Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Yet another rows to column question (sorry)


Yet another rows to column question (sorry)

Author
Message
Ulysses_Infinity
Ulysses_Infinity
SSC Eights!
SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)

Group: General Forum Members
Points: 820 Visits: 661
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
HowardW
HowardW
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1211 Visits: 9892
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/
drew.allen
drew.allen
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2653 Visits: 9924
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
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
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!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Ulysses_Infinity
Ulysses_Infinity
SSC Eights!
SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)

Group: General Forum Members
Points: 820 Visits: 661
< 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
santa326
santa326
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 126
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
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24200 Visits: 37964
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

Cool
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)
Ulysses_Infinity
Ulysses_Infinity
SSC Eights!
SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)SSC Eights! (820 reputation)

Group: General Forum Members
Points: 820 Visits: 661
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search