Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2010
    Posts
    4

    Unanswered: How to combine two rows into one row with same ID?

    I have a data that needs to be arranged from two rows into one row as follow:

    From two rows into -->
    ID X Text
    1234 1 abc
    1234 2 xyz

    One row -->
    ID Text1 Text2
    1234 abc xyz

    If field X=1, then Text needs be put into field Text1 and if X=2, then Text needs to be put into Text2.

    So how I can do that? I think I need to use CASE function somehow?

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    yes, CASE
    Code:
    SELECT id
         , MAX(CASE WHEN x = 1 THEN text ELSE NULL END) AS text1
         , MAX(CASE WHEN x = 2 THEN text ELSE NULL END) AS text2
      FROM daTable
    GROUP
        BY id
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Mar 2010
    Posts
    4
    It works now. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •