Results 1 to 4 of 4

Thread: UNION queries and sorted data

  1. #1
    Join Date
    Dec 2006
    Posts
    2

    UNION queries and sorted data

    Hi,
    I have a query that has 3 union staments with each of them having same joins for all but 1. Althought the query fetches the data correctly, it sorts it out on the basis of the joins. I need that the data fetched be unsorted and in the order in which it ispresent in the DB. Can someone help me on that.

    -nkrust

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    Use UNION ALL, UNION sorts and discards duplicates.

  3. #3
    Join Date
    Dec 2006
    Posts
    2
    Quote Originally Posted by skhanal
    Use UNION ALL, UNION sorts and discards duplicates.
    I tried ur suggestion, but what i want is the data to be out in the same manner in which it resides in the table, with union all, it outputs the data hapazardly

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    You have to use ORDER BY clause to order resultset in the order you want.

    UNION does not return data in the order record is stored. This will be against the relational theory on which RDBMS is built.

Posting Permissions

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