Wednesday, 7 August 2013

SQL Server 2008 Vertical data to Horizontal

SQL Server 2008 Vertical data to Horizontal

I apologize for submitting another question on this topic, but I've read
through many of the answers on this and I can't seem to get it to work for
me.
I have three tables I need to join and pull info on. One of the tables is
only 3 columns and stores the data vertically. I would like to transpose
that data to a horizontal format.
The data will look like this if I just join and pull:
SELECT
a.app_id,
b.field_id,
c.field_name,
b.field_value
FROM table1 a
JOIN table2 b ON a.app_id = c.app_id
JOIN table3 c ON b.field_id = c.field_id
Result:
app_id | field_id | field_name | field_value
-----------------------------------------------------
1234 | 101 | First Name | Joe
1234 | 102 | Last Name | Smith
1234 | 105 | DOB | 10/15/72
1234 | 107 | Mailing Addr | PO BOX 1234
1234 | 110 | Zip | 12345
1239 | 101 | First Name | Bob
1239 | 102 | Last Name | Johnson
1239 | 105 | DOB | 12/01/78
1239 | 107 | Mailing Addr | 1234 N Star Ave
1239 | 110 | Zip | 12456
Instead, I would like it to look like this:
app_id | First Name | Last Name | DOB | Mailing Addr | Zip
--------------------------------------------------------------------------
1234 | Joe | Smith | 10/15/72 | PO BOX 1234 | 12345
1239 | Bob | Johnson | 12/01/78 | 1234 N Star Ave | 12456
In the past, I just resorted to looking up all the field_id's I needed in
my data and created CASE statements for each one. The app the users are
using contains data for multiple products, and each product contains
different fields. Considering the number of products supported and the
number of fields for each product (many, many more than the basic example
I showed, above) it takes a long time to look them up and write out huge
chunks of CASE statements.
I was wondering if there's some cheat-code out there to achieve what I
need without having to look up the field_ids and writing things out. I
know the PIVOT function is likely what I'm looking for, however, I can't
seem to get it to work correctly.
Think you guys could help out?

No comments:

Post a Comment