Way to pivot data without aggregation

  • I have a table with ProductId, ItemId, PropertyId, and ValueId columns. My ValueId is a varchar(20) and happens to be the data I want pivoted under the PropertyId which would be my columns. ProductId and ItemId would be the row. Is there a way to do this with Pivot?

    thanks,

    vmon

  • Sorry, but no. The PIVOT operator requires some sort of aggregation. Whether you're averaging or summing or whatever.

    Someone did post a CASE statement SELECT which is a fake pivot, though. Check out this thread: http://www.sqlservercentral.com/Forums/Topic399930-338-1.aspx

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • well - for better or for worse - Min( [varchar]) returns the [varchar], so you could use that as your aggregate. Assuming you can make sure that the grouping only returns one valueID per column+row grouping, that should give you what you wish.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Totally agree with Matt and you can use either Min or Max ... just be carefull if you have Nulls !


    * Noel

  • ...or duplicated data...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 1 through 4 (of 4 total)

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