Pivot in SQL 2005

  • I have 3 tables

    Properties

    Products

    Property Product Junction

    I want to dynamically crosstab with the properties as columns (Not explicitly defined) with the product name vertical with the id. then have the values in the middle. Just like an excel sheet. Is that possible?

  • You can do it by turning the Properties to a @Dec, e.g.

    SELECT @columns = COALESCE(@columns + ',[' + cast(Invested as varchar) + ']',

    '[' + cast(Invested as varchar)+ ']')

    FROM #AA

    GROUP BY Invested

    and then using that as part of an Execute (@SQLquery). What's killing me is I get that working but in the Output, I can't find any way to turn, say, if the property and Product Name don't have a correlation into a 0 instead of NULL.

    Heading home but if noone ays anything overnight, i'll explain what i have a bit better. ExpertsExchange has a good example for 'Dynamic Pivot Table' in the meantime.

  • You might want to have a look at the following article:

    http://www.sqlservercentral.com/articles/Crosstab/65048/



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

Viewing 3 posts - 1 through 2 (of 2 total)

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