how to get data from a table all in one result row

  • I'm need help with a query.

    The table has an id, a name, and a type. Sample:

    ID Name Type

    123 description black

    123 technique breaking

    456 findings nice

    456 technique splitting

    How can I select to get this result:

    123 black breaking

    456 nice splitting

    Thanks very much for any help.

  • You can use cross tabs to accomplish this. To read more about this technique, read the following article.

    http://www.sqlservercentral.com/articles/T-SQL/63681/

    SELECT ID,

    MAX( CASE WHEN Name = 'Description' THEN Type) AS Description,

    MAX( CASE WHEN Name = 'technique ' THEN Type) AS Technique

    FROM MyTable

    GROUP BY ID

    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
  • This worked, I had to add an else and end and it worked like I needed.

    SELECT ID,

    MAX( CASE WHEN Name = 'Description' THEN Type else null end) AS Description,

    MAX( CASE WHEN Name = 'technique ' THEN Type else null end) AS Technique

    FROM MyTable

    GROUP BY ID

    Thanks very much.

Viewing 3 posts - 1 through 2 (of 2 total)

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