Is the order in which a UNION ALL query returns rows guaranteed?

Guys
I'm doing a kind of query where I want to return matching rows in a priority order and take the first one. THink of it as getting the most specific error message for a particular context.
SELECT * FROM (
SELECT msg FROM errormessages WHERE device = 'x'
UNION ALL
SELECT msg FROM errormessages WHERE client = 'y'
UNION ALL
SELECT msg FROM (SELECT msg from errormessages WHERE class LIKE 'generic_' ORDER BY class)
UNION ALL
SELECT 'missing error message' FROM dual
WHERE ROWNUM =1
If UNION ALL is guaranteed to return rows in this order, great. If not, would it be better to have:
SELECT * FROM ( SELECT * FROM(
SELECT 1 as ord, msg FROM errormessages WHERE device = 'x'
UNION ALL
SELECT 2, msg FROM errormessages WHERE client = 'y'
UNION ALL
SELECT 4+SUBSTR(class, 7, 1), msg FROM errormessages WHERE class LIKE 'generic_'
UNION ALL
SELECT 9999, 'missing error message' FROM dual
) ORDER BY ord )
WHERE ROWNUM =1Or, another option:
SELECT msg FROM(
SELECT
  CASE
    WHEN device = 'x' THEN 1
    WHEN client = 'y' THEN 2
    WHEN class LIKE 'generic_' THEN 4+SUBSTR(class, 7, 1)
  END as ord, msg
FROM (
  SELECT * FROM errormessages WHERE device = 'x' OR  client = 'y' OR class LIKE 'generic_'
  UNION ALL
  SELECT 999, 'missing error' FROM dual
) ORDER BY ord
)WHERE ROWNUM =1As you can see there is a complication in the generic; devices may advertise that they accept 1,2,3 or 4 lines of info, so I want to use the advertised capabiltiy to pick the most relevant message from the generic set if no specifics exist for the device or the client
Which was would you go?
Cheers
Edited by: charred on Feb 11, 2009 4:56 AM - code tags

charred wrote:
So tell me guys, do I go for:
4 queries unioned together
or
1 query with 4 ORs in the where clause and a case when to determine priority?It will depend on your conditions, but if you look at your explain plans you should get an idea for which is the better one; typically I would expect this to be the OR method...
SQL> select * from emp where deptno = 20
  2  union all
  3  select * from emp where job = 'CLERK'
  4  union all
  5  select * from emp where sal > 2500;
Execution Plan
Plan hash value: 3153085224
| Id  | Operation                    | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT             |          |    14 |   546 |     8  (75)| 00:00:01 |
|   1 |  UNION-ALL                   |          |       |       |            |          |
|   2 |   TABLE ACCESS BY INDEX ROWID| EMP      |     5 |   195 |     2   (0)| 00:00:01 |
|*  3 |    INDEX RANGE SCAN          | DEPT_IDX |     5 |       |     1   (0)| 00:00:01 |
|*  4 |   TABLE ACCESS FULL          | EMP      |     4 |   156 |     3   (0)| 00:00:01 |
|*  5 |   TABLE ACCESS FULL          | EMP      |     5 |   195 |     3   (0)| 00:00:01 |
Predicate Information (identified by operation id):
   3 - access("DEPTNO"=20)
   4 - filter("JOB"='CLERK')
   5 - filter("SAL">2500)
Statistics
          1  recursive calls
          0  db block gets
         18  consistent gets
          1  physical reads
          0  redo size
       1328  bytes sent via SQL*Net to client
        396  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         14  rows processed
SQL> select * from emp
  2  where deptno = 20 or job = 'CLERK' or sal > 2500;
Execution Plan
Plan hash value: 3956160932
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT  |      |    10 |   390 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| EMP  |    10 |   390 |     3   (0)| 00:00:01 |
Predicate Information (identified by operation id):
   1 - filter("DEPTNO"=20 OR "SAL">2500 OR "JOB"='CLERK')
Statistics
          1  recursive calls
          0  db block gets
          8  consistent gets
          0  physical reads
          0  redo size
       1146  bytes sent via SQL*Net to client
        396  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          9  rows processed
SQL>

Similar Messages

  • What is the order in which I must change my Apple ID on all my Mac products:iTunes, iCloud for iphone-MacIntel- MacAirbook

    I wish to change my email account on my AppleID. But I am faced with having to go back and forth between old and new emails/accounts to get ALL my Mac products changed to the new email address. I have needed to delete acounts, and then reset accounts back to the old account to only re-delete or re-set.
    Is there a specific order in which to do update all my Mac products? What item do I reset first, what product follows the next? I do not have an ipad but might as well add it to the list for when I do get one and have to do this all again.
    What I have done so far-
    My apple ID is  currently set to the original email account (aol) that I wish to delete.
    my icloud on my iphone has been deleted
    my iTunes is set to the old aol account
    Now... what item gets changed first, second.....

    First verify that your existing Apple ID password conforms to these rules:
    If not, change your Apple ID password first.
    To change your password read Apple ID: Changing your password
    Then, sign out of all the Apple services on all your Apple products. Use the link in Step 8 of this document as a checklist to make sure you do all of them:
    Apple ID: Changing your Apple ID
    The order in which you change your Apple ID does not matter.
    Subsequent to changing your Apple ID read and follow:
    Apple ID: What to do after you change your Apple ID

  • ADF: controlling the order in which VO records are inserted

    All,
    in an ADF BC project, we have some complex MD insert pages that add records to master and detail tables at the same time. The application sits on top of CDM RuleFrame, which exposes the datamodel as DB views with instead-of triggers and adds (Designer) a few nifty things like auto-populated PK fields. Sad enough, we can't use these auto-generated PKs as the 'refresh after insert' settings in an EO work with a RETURNING clause, which is not supported for instead-of triggers. This means we'll have to find the PK sequences ourselves from the EOs and populate these fields ourselves, before committing to the database. So far so good, but things go wrong when we want to insert master and detail at the same time. The PK and FK of the child record are already filled in at insert time (which is nice) but for some reason the child records get inserted before the parents, resulting in an error on the FK as the parent record does not yet exist (which is not nice).
    So, here's the question: is there a way to control the order in which new VO records are inserted ? On what is the default order based (maybe a little renaming could help us out to get the alphabet our way) ?
    We know defining the FKs as 'initially deferred' will make sure they are only checked at commit time, but our DBA prefers to not use that solution unless strictly necessary. (is there anything bad about deferred keys why we should believe him? ;o) )
    And additionally, is there a way around all this manual PK-fetching ? We've tried using the refresh(int refreshMode) method of EntityImpl, but it didn't seem to work.
    Many thanks in advance for your tips, suggestions and solutiosn !
    Best regards,
    Benjamin De Boe

    Benjamin,
    To handle the problem with the child/parent records being inserted in the wrong order - have a read of the Oracle ADF Developer's Guide for Forms/4GL developers, section 26.7. I use that technique with great success.
    John

  • How to control the order in which records get indexed

    Hi,
    I like to control the order in which the ATG (10.1.2) product records get indexed in Endeca (3.1.1). I like the products/categories to be indexed in the order in which they are sequenced in BCC (in otherwords baseed on the sequence num they have). How to do this? Please help

    If you want TopLink to set the values in a particular order you can order the mappings on a descriptor by setting their ‘weight’ (DatabaseMapping.setWeight(int). All DirectToField type mappings are defaulted to a weight of 1 and all relationship mappings are defaulted to a weight of MAX_INTEGER. Mappings with a lower weight value will be processed first by TopLink.
    --Gordon

  • Ordering in Union All Query

    Hi,
    Is Order by in Union ALL query allowed? If not what is the solution while using Unoin ALL?
    thx

    What isn't working? These are all OK:
    SELECT dummy FROM dual
    UNION ALL
    SELECT dummy FROM dual
    ORDER BY dummy;
    SELECT dummy FROM dual
    UNION ALL
    SELECT dummy FROM dual
    ORDER BY 1;
    SELECT dummy AS c1 FROM dual
    UNION ALL
    SELECT dummy FROM dual
    ORDER BY c1;The name used in the ORDER BY clause has to be one of the names in the first SELECT list, or a number.

  • How can I change the order in which photos are presented on an Apple TV?

    I am showing photos on my Apple TV via my Apple MacBook, iTunes and Home Sharing.
    That all works nicely for me, except that I wish to change the order in which a few photos
    are being presented.  How can I change the presentation order on the Apple TV?

    Welcome to the Apple community.
    You'll need to change the order in iPhoto. Open the album you wish to re-order, manually sort them to the order you want, then use the time and date and batch change options as applicable from the photos menu to retag your photos.

  • You used to be able to shuffle a playlist and see the order in which the songs would sync into your iPod.  I haven't been able to figure that out. Can anyone help me?

    You used to be able to shuffle a playlist and see the order in which the songs would sync into your iPod.  I haven't been able to figure that out. Can anyone help me?

    With your playlist in "List view", click on the header over the column of numbers all the way to the left.  This will then reveal the true playback (and sync) order.
    However, the sort order in iTunes may not agree with the sort order on your device, but that is a separate issue and seems to have too many variables to go into here.

  • How do I flip the order in which podcast episodes sync?

    With Zune it is very EZ to simply flip the order in which podcast episodes sync. So that, for instance, from the downloaded content on my Mac which down loaded episode 321 they apear 123 on my iPod classic.. And my levles are all out of wack on some CDs. some that I downloaded directly from iTunes and outhers from my hard copies.

    There might be another way.
    There are utilities which can batch-rename files, including options that sort by date, begin all the resulting filenames with a common prefix, and then add a sequential "padded" filename suffix. If you then display the resulting collection of files in alphabetical order (as in Finder's Arrange by Name in icon view), they should be sorted chronologically.
    I haven't tried any of these, but the site for [A Better Finder Rename|http://www.publicspace.net/ABetterFinderRename/index.html], shareware, states:
    "A Better Finder Rename knows how to extract EXIF shooting date and time information from digital camera images and exploit them in creating sequence numbers or adding time and date information to the file name. "
    There may be suitable freeware alternatives -search for *file renamer* in [Version Tracker|http://www.versiontracker.com/macosx>.

  • JSTL Change the order in which children tags are processed

    Hi there,
    Is there any way one can manipulate the order in which tags are executed/processed within for example an iteration tag? I.e. let's say I have the following:
    <m:my_iterate_tag data="data">
      <dotag1>XX</dotag1>
      <dotag2>YY</dotag2>
    </m:my_iterate_tag/>The user can set as a preference in my application that they want <dotag2> to
    be displayed/executed first before <dotag1>, e.g. like in a column in a table. How can I change
    the order of these tags when I execute the <m:my_iterate_tag>? Do I do it in
    <m:my_iterate_tag> or is there another or better way? Please help.
    Thanks,
    Marius Botha

    Okay, lets start with getting the order in which to display columns... So you say:
    get the order of these columns from the db (e.g. client
    says 1=title, 2=number, don't display "type" column)Based on the DB, then, create a java.util.List that would have the column names in the order they want... (not real code...)
    ArrayList columnOrder = ["title", "number"]
    When you get to iter_tag:table.doStartBody, then create a java.util.HashMap (lets say) called tableColumnData.
    Then in the iter_tag:column tag, you would the value provided in the tag mapped to the property name as the key in the tableColumnData map:
    tableColumnData.put("number", <this iteration's number value>);
    tableColumnData.put("title", <the value returned from the g:href tag>);
    tableColumnData.put("type", <anything>);
    Then, in the iter_tag:table.doEndBody, you do a loop, getting the name of the column to display from the columnOrder list, and using that name to pull the value out of the tableColumnData map:
        for (int i = 0; i < columnOrder.size(); i++) {
            out.println("<td>" + tableColumnData.get(columnOrder.get(i)) + "</td>");
        }You would put the columnOrder in a scope where you won't have to relaod it all the time, like maybe the user's session. You would put the tableColumnDataMap to make sure it is unique to the table... either in the pageContext, or store it in the iter_tag and make a method where the nested tags can retrieve it.

  • Lion has changed the order in which Finder items are sorted in Icon View; Acc vs. Desc.  How can you change this order?

    The order in which items are sorted has changed in Lion and I can't figure out how, or if, it can be changed.  Previously in Snow Leopard if you sorted by 'Date' in Icon View, your icons would be arranged from oldest to newest.  The same was true for 'Kind' and all the order sort options.  In Kind my *.zip files were positioned last.
    Now in Lion, the order is reversed.  Newest and Zips on top... the accending vs. decnding order has been changed.  How can I change the sort order in Icon View?

    I managed to fix the reverse order issue by deleting the com.apple.finder.plist and com.apple.finder.plist.lockfile in the user library folder. I did have to use terminal to delete the files and then re-launch finder from the force quit window. Here are the step-by-step instructions:
    Hold down the option key while selecting the Go menu. Select Library.
    Navigate to the Preferences.
    Find the com.apple.finder.plist.lockfile and delete it.
    Open Terminal (Application/Utilites)
    In Terminal type in sudo rm (include a space after the rm and do not hit enter)
    Drag com.apple.finder.plist file to the terminal window. It will fill in the path to the file)
    Press return.
    Type in your password and hit return. (The cursor will not move when you type in your password)
    From the Apple menu, select force quit.
    Highlight finder and select re-launch.
    Be careful when using Terminal. There is no undo button and you can accidentally delete important files.
    I hope this helps.
    Henry

  • I have multiple email accounts on my iPad. How do I control the order in which the accounts are displayed?

    I have multiple email accounts on my iPad. How do I control the order in which the accounts are displayed?

    In landscape mode, the mailbox list is automatically displayed (in portrait mode, you will need to tap the button to,show it).
    To edit the list, tap edit as shown below.

  • [svn] 4241: A collection of minor fixes for FXG transcoding to SWF: matrix attribute name correction; allowing for 0x prefix for hexadecimal colors; allowing for an id attribute on FXG nodes; fixing blur filter quality encoding; fixing the order in which

    Revision: 4241
    Author: [email protected]
    Date: 2008-12-05 11:00:45 -0800 (Fri, 05 Dec 2008)
    Log Message:
    A collection of minor fixes for FXG transcoding to SWF: matrix attribute name correction; allowing for 0x prefix for hexadecimal colors; allowing for an id attribute on FXG nodes; fixing blur filter quality encoding; fixing the order in which bevel filter colors are encoding - the SWF specification appears to have the wrong order specified; updating SwfxPrinter to display a linestyle's fillstyle (if present).
    Temporarily removing generated id attribute emitted in FXG to SWF optimization as this attribute is not present in the AS impl.
    Also updating compiler error base types to allow a rootcause to be specified for additional detail to a compiler message exception.
    QE: No
    Doc: No
    Checkintests: Pass
    Reviewer: Discussed compiler base error change with Paul.
    Modified Paths:
    flex/sdk/trunk/modules/compiler/src/java/flex2/compiler/CompilerException.java
    flex/sdk/trunk/modules/compiler/src/java/flex2/compiler/as3/EmbedEvaluator.java
    flex/sdk/trunk/modules/compiler/src/java/flex2/compiler/util/CompilerMessage.java
    flex/sdk/trunk/modules/swfutils/src/java/flash/fxg/FXGConstants.java
    flex/sdk/trunk/modules/swfutils/src/java/flash/fxg/dom/AbstractFXGNode.java
    flex/sdk/trunk/modules/swfutils/src/java/flash/fxg/dom/TextGraphicNode.java
    flex/sdk/trunk/modules/swfutils/src/java/flash/fxg/swf/AbstractFXGGraphics.java
    flex/sdk/trunk/modules/swfutils/src/java/flash/swf/TagDecoder.java
    flex/sdk/trunk/modules/swfutils/src/java/flash/swf/TagEncoder.java
    flex/sdk/trunk/modules/swfutils/src/java/flash/swf/tools/SwfxPrinter.java

  • How can I change the order in which songs appear in my iTunes Music Library?

    How can I change the order in which songs appear in my iTunes Music Library?

    Kay,
    If you are looking at the entire music library ("Music") you can sort by any of the columns by clicking the column header.  If there is a column you can't see, enable it by going to View > View Options.
    Within a playlist, you can do the same, and in addition you can click above the column of sequence numbers, which will then allow you to drag tracks up or down to get any order you wish.

  • Any way to control the order in which data is exported?

    I found it surprisingly easy to set up a form to submit its contents via a formmail script -- perhaps too easy...
    Using Acrobat 9 (and the demo version of Acrobat X), I've modified a form that students fill out so that when they hit the submit button it pushes the form contents to a script which relays it to an e-mail address specified on the form -- no problems there.
    But the order in which those fields are listed in the e-mail bears no relation to the order the fields appear in the form -- or alphabetical order of the field names or data values or the tab order. It's the same order every time. And as far as I can tell the mailing script isn't doing anything to change the order of the data.
    This is mostly an issue of making the e-mail friendly for the supervisor who has to look at the e-mails as they come in. (I've done a number of searches on Google and here in the forums before posting and haven't found any references to similar problems, so maybe I do have a funky script.)
    I'm wondering if a) there's something native to the form that could change the order of the data and b) there's some straightforward way of controlling that. (I imagine I could write some JavaScript to control the output I had to, but it seems like a needless step.)
    Thanks for any insight anyone can provide.

    function(){return A.apply(null,[this].concat($A(arguments)))}
    A solution would be to modify the server script to output in the order you want, as opposed to just looping over the collection of fields present in the incoming data as formmail type scripts typically do.
    That is what I wound up doing -- making a copy of the mailing script and reformatting the message body string to contain just the data responses my colleage wanted in the order he wanted. It beats the old method of requiring users to fill out the PDF, save the document and then e-mail in the PDF as an attachment.

  • Can I change the order in which my different e-mail accounts are displayed in TB?

    I use TB to access e-mails from 5 different accounts. They are displayed in the order in which I added them but I would like to change that order. Is it possible, please?

    Many thanks @Airmail for taking the time to provide the solution.

Maybe you are looking for

  • My ipod 4th gen can't be recognized by itunes after itouch software update

    my ipod 4th gen can't be recognized by itunes after itouch software update. I have reset both PC and itouch to no avail

  • ASSERTION_FAILED while activation of Transformation !

    Hello , While activation of transformation in Development system BI7.0 , following error appears : =============== Error analysis     The following checkpoint group was used: "No checkpoint group specified"     If in the ASSERT statement the addition

  • Rights to use the Visualizer in a video

    Does anyone know if there any restrictions on using segments of the "visualizer" in a music video for an original song. Does one need permission from Apple?

  • Bottom Left Corner of Finder

    Hi peoples, This is probably a stupid question, but what is the icon in the bottom left hand corner of Finder? It's only there in certain windows, like Documents and Pictures. It looks like four diamonds/parallelograms in a grid. Here's a screenshot-

  • Search result functionality

    Hello everybody, I don't understand the following functionality on Search Results; When I try to find an Activity for a certain BP (Get: Partner and I fill in a name and I press Go) the systems returns one Activity (detailed). But when I now press Go