Results 1 to 3 of 3

Thread: UNION Query

  1. #1
    Join Date
    Oct 2002
    Location
    Cochin, INDIA
    Posts
    9

    Unanswered: UNION Query

    I have a query as given below:

    select a,b,c from x
    union
    select a,b,c from y
    union
    select a,b,c from z

    When I execute the above query, I get a resultset.

    How do I know which record is from which sub-query.

    Thanks in advance,
    Saju Joseph

  2. #2
    Join Date
    Sep 2002
    Location
    UK
    Posts
    5,171
    Provided Answers: 1

    Re: UNION Query

    Originally posted by peruvachira
    I have a query as given below:

    select a,b,c from x
    union
    select a,b,c from y
    union
    select a,b,c from z

    When I execute the above query, I get a resultset.

    How do I know which record is from which sub-query.

    Thanks in advance,
    Saju Joseph
    You don't. If you need to know, do this:

    select 'x' source,a,b,c from x
    union
    select 'y' source,a,b,c from y
    union
    select 'z' source,a,b,c from z

    Bear in mind that this changes the results: any record that exists in more than one of x, y, z will now appear more than once.

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    yes, and since rows that would have come from more than one of the selects are now distinguished by the first column, there's really no point in allowing the database to sort the entire result set (on all columns!) in order to try to eliminate duplicates which just aren't going to be there, by virtue of that first column

    so it should be

    select 'x' source,a,b,c from x
    union all
    select 'y' source,a,b,c from y
    union all
    select 'z' source,a,b,c from z

    rudy

Posting Permissions

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