Merge results from two fields

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stacey

    Merge results from two fields

    What is the best way to go about merging the results of two fields in
    the same table?

    I have two different fields that hold lists of names, some of them
    identical, some different. From those I'd like to generate a merged
    list with all the distinct names.

    For example:

    Field1 contains: Jack, Joe, Jim, Paul
    Field2 contains: Peter, Paul, Joe, Jim

    The result should be: Jack, Joe, Jim, Peter, Paul

  • David Portas

    #2
    Re: Merge results from two fields

    Why are you storing delimited lists in columns? This is a bad idea in a
    relational database. Best to create a new table and put the data in
    there. SELECT DISTINCT will give you a distinct list from a table but
    it's tricky to manipulate delimited lists in SQL.

    --
    David Portas
    SQL Server MVP
    --

    Comment

    • Stacey

      #3
      Re: Merge results from two fields

      I'm sorry, I meant to convey that these names are different records in
      the particular fields.

      So - I may have the following records:

      <pre>
      Rec# Field1 Field2
      1: Jack Peter
      2: Joe Paul
      3: Jim Joe
      4: Paul Jim
      </pre>

      and would like a result of:
      Jack
      Joe
      Jim
      Paul
      Peter

      Comment

      • Thomas R. Hummel

        #4
        Re: Merge results from two fields

        Create table statements and insert statements for sample data will
        prevent these kinds of misunderstandin gs. In the future, please try to
        include those in your posts.

        Off the top of my head (meaning it might not be the best solution
        performance-wise), the following should work:

        CREATE TABLE My_Table (field1 VARCHAR(20), field2 VARCHAR(20))
        GO

        INSERT INTO My_Table VALUES ('Jack', 'Peter')
        INSERT INTO My_Table VALUES ('Joe', 'Paul')
        INSERT INTO My_Table VALUES ('Jim', 'Joe')
        INSERT INTO My_Table VALUES ('Paul', 'Jim')
        GO

        SELECT Field1 AS Name FROM My_Table
        UNION
        SELECT Field2 FROM My_Table
        GO

        HTH,
        -Tom.

        Comment

        • Taw

          #5
          Re: Merge results from two fields

          This should do it

          select distinct Field1 from TableName
          union
          select distinct Field2 from TableName


          Stacey wrote:[color=blue]
          > I'm sorry, I meant to convey that these names are different records[/color]
          in[color=blue]
          > the particular fields.
          >
          > So - I may have the following records:
          >
          > <pre>
          > Rec# Field1 Field2
          > 1: Jack Peter
          > 2: Joe Paul
          > 3: Jim Joe
          > 4: Paul Jim
          > </pre>
          >
          > and would like a result of:
          > Jack
          > Joe
          > Jim
          > Paul
          > Peter[/color]

          Comment

          • --CELKO--

            #6
            Re: Merge results from two fields

            Do not use SELECT DISTINCT and UNION together. It is redundant and
            most SQL products will not see that, so you get three sorts instead of
            one.

            Comment

            Working...