Yet another rows to column question (sorry)

  • 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

  • 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/

  • 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

  • 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[/url]

  • < 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

  • 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

  • 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

  • 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

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply