SQL Transpose / Pivot - Help

  • PS: The attachment has a screenshot of the tables described below which is easier to read

    Hi All,

    I am stuck on finding a solution to transpose source data from a system via a metadata look-up table into a destination table. I need a method to transpose/pivot the source data into columns (which are by various data-types). The datatypes for each column are listed in a metadata table.

    Source Data Table:

    Table Name: Source

    SrcID AGE City Date

    01 32 London 01-01-2013

    02 35 Lagos 02-01-2013

    03 36 NY 03-01-2013

    Metadata Table:

    Table Name:Metadata

    MetaID Column_Name Column_type

    11 AGE col_integer

    22 City col_character

    33 Date col_date

    Destination table:

    The source data to be loaded into the destination table(as shown below):

    Table Name: Destination

    SrcID MetaID col_int col_char col_date

    01 11 32 - -

    01 22 - London -

    01 33 - - 01-01-2013

    02 11 35 - -

    02 22 - Lagos -

    02 33 - - 02-01-2013

    03 11 36 - -

    03 22 - NY -

    03 33 - - 03-01-2013

    Any help would be much appreciated.

    Thank you,

    Aarion

  • I would unpivot[/url] the source table, join it with the metadata table, use cross tabs[/url] to obtain the desired result and run away from that horrible design.

    That destination table looks like real horror for any RDBMS.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (4/1/2014)


    I would unpivot[/url] the source table, join it with the metadata table, use cross tabs[/url] to obtain the desired result and run away from that horrible design.

    That destination table looks like real horror for any RDBMS.

    Horror?!? I LOVE designs like that ... they make the consultant in me go "kaaAACHIIINNNGGG"!! 😀

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • AarionSSQL (4/1/2014)


    PS: The attachment has a screenshot of the tables described below which is easier to read

    Hi All,

    I am stuck on finding a solution to transpose source data from a system via a metadata look-up table into a destination table. I need a method to transpose/pivot the source data into columns (which are by various data-types). The datatypes for each column are listed in a metadata table.

    Source Data Table:

    Table Name: Source

    SrcID AGE City Date

    01 32 London 01-01-2013

    02 35 Lagos 02-01-2013

    03 36 NY 03-01-2013

    Metadata Table:

    Table Name:Metadata

    MetaID Column_Name Column_type

    11 AGE col_integer

    22 City col_character

    33 Date col_date

    Destination table:

    The source data to be loaded into the destination table(as shown below):

    Table Name: Destination

    SrcID MetaID col_int col_char col_date

    01 11 32 - -

    01 22 - London -

    01 33 - - 01-01-2013

    02 11 35 - -

    02 22 - Lagos -

    02 33 - - 02-01-2013

    03 11 36 - -

    03 22 - NY -

    03 33 - - 03-01-2013

    Any help would be much appreciated.

    Thank you,

    Aarion

    If you want coded help, please see the first link under "Helpful Links" in my signature line below. You came really close... just need to push it over the edge to get the best help for your question.

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

  • Thank you Luis .. apologies for the late reply. I had no internet access overnight but back in the office now.

    I have done this:

    Unpivot data

    Left Join to Metadata table

    Pivot data

    it worked.

    Many thanks for your help on this!

    As for the design I am totally against it but there are times when you just have to build a sample solution to prove your point and this is one of them!

  • It is a complete horror! Now to model it and prove my point! 😀

  • Thanks Jeff, I was just after the idea. I have got the code working after Luis comments.

    Thank you to all for replying!

    aarionsql (4/2/2014)


    Thank you Luis .. apologies for the late reply. I had no internet access overnight but back in the office now.

    I have done this:

    Unpivot data

    Left Join to Metadata table

    Pivot data

    it worked.

    Many thanks for your help on this!

    As for the design I am totally against it but there are times when you just have to build a sample solution to prove your point and this is one of them!

  • I'm glad that you could get it done and it's even better to know that you're aware of this terrible design.

    Good luck with this project (you'll need it).

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

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

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