Transpose SQL Table??

Posted on 2012-09-03
Last Modified: 2012-09-03
I have a table in SQL 2008 with:

Record   Fieldname        Fieldlabel  FieldValue
1                  1                 Name        Jeroen
1                  2                 Email
1                  3                 Active       False
2                  1                 Name       Piet
2                  2                 Email
2                  3                 Active       False

I want to get an output like:

Record     Name             Email                             Active
1              Jeroen              False
2              Piet             False

Any suggestions how i can do this using SQL Commands?


Question by:ShareIT
    LVL 8

    Accepted Solution

    Working Example:

    DECLARE @t TABLE (Record Int, Fieldname Int, FieldLabel Varchar(200), FieldValue Varchar(200))
    (1, 1, 'Name', 'Jeroen'),
    (1, 2, 'Email', ''),
    (1, 3, 'Active', 'False'),
    (2, 1, 'Name', 'Piet'),
    (2, 2, 'Email', ''),
    (2, 3, 'Active', 'False')
    SELECT * FROM @t
    SELECT Record, MAX(Name) AS Name, MAX(EMail) AS EMail, MAX(Active) AS Active 
    FROM (
    	SELECT  Record,
    	CASE WHEN FieldLabel = 'Name' THEN FieldValue ELSE '' END AS Name,
    	CASE WHEN FieldLabel = 'Email' THEN FieldValue ELSE '' END AS Email,
    	CASE WHEN FieldLabel = 'Active' THEN FieldValue ELSE '' END AS Active
    	FROM @t
    ) v
    GROUP BY Record

    Open in new window


    Author Comment

    Thanks, I also found a similar solution using the Select Pivot syntax on:


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
    Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now