Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Transpose / Pivot - Help Expand / Collapse
Author
Message
Posted Tuesday, April 1, 2014 11:46 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 10:46 PM
Points: 17, Visits: 237
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


  Post Attachments 
ScrnPrint.png (10 views, 20.59 KB)
Post #1557141
Posted Tuesday, April 1, 2014 12:30 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 11:52 AM
Points: 3,630, Visits: 8,138
I would unpivot the source table, join it with the metadata table, use cross tabs to obtain the desired result and run away from that horrible design.
That destination table looks like real horror for any RDBMS.



Luis C.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1557160
Posted Tuesday, April 1, 2014 8:12 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 10:55 AM
Points: 4,368, Visits: 6,207
Luis Cazares (4/1/2014)
I would unpivot the source table, join it with the metadata table, use cross tabs 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 at GMail
Post #1557295
Posted Tuesday, April 1, 2014 11:41 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:40 AM
Points: 35,265, Visits: 31,754
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1557317
Posted Wednesday, April 2, 2014 3:15 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, September 26, 2014 2:10 AM
Points: 39, Visits: 119
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!
Post #1557368
Posted Wednesday, April 2, 2014 3:16 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, September 26, 2014 2:10 AM
Points: 39, Visits: 119
It is a complete horror! Now to model it and prove my point!
Post #1557369
Posted Wednesday, April 2, 2014 3:18 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, September 26, 2014 2:10 AM
Points: 39, Visits: 119
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!
Post #1557371
Posted Wednesday, April 2, 2014 9:29 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 11:52 AM
Points: 3,630, Visits: 8,138
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1557562
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse