Update Statement w/ Order By and other goodies

this is a continuation of my previous post : Re: create update statement with joins/select Please see it for table structure and sample data.
Ok so this is building on my previous SQL question that was resolved by you brilliant SQLers out there. the below code will do exactly what i need it to do, but I was wanting less user interaction:
update offender_case_identifiers ociq
   set Identifier_type = 'ARN2'
where ociq.offender_book_id in
      (select offender_book_id from v_header_block
        where offender_id_display = :ppid)
    and ociq.comment_text != :parn
   AND ociq.identifier_type = 'ARN'I was only wanting the user to have to enter the ":pid" and have the script take care of the rest. I have found the best way to ensure that I'm finding the most recent "ARN" from the offender_case_identifiers (which is then going to need to be set to ":parn") is to run the following script:
select oci.identifier_type
, vhb.offender_id_display
,oci.comment_text
,vhb.offender_book_id
,oci.offender_book_id
from   offender_case_identifiers oci, v_header_block vhb
where
oci.offender_book_id = vhb.offender_book_id
and vhb.offender_id_display = :ppid
and oci.identifier_type like 'ARN'
order by comment_text descThe above code was the original starting point for my previous final SQL statement, listed first in this post, however, I have added the "ORDER BY" at the bottom of it. For additional sample data, here is the result of the above statement:
IDENTIFIER_TYPE     OFFENDER_ID_DISPLAY     COMMENT_TEXT     OFFENDER_BOOK_ID     OFFENDER_BOOK_ID_1
ARN     *0000382183*     01550228     *1,011,683*     1,011,683
ARN     *0000382183*     01550228     *1,011,683*     1,011,683
ARN     *0000382183*     01531810     *823,281*     823,281
ARN     *0000382183*     01531810     *823,281*     823,281
ARN     *0000382183*     01531810     *823,281*     823,281
ARN     *0000382183*     01531810     *823,281*     823,281
ARN     *0000382183*     01529717     *804,301*     804,301
ARN     *0000382183*     01525871     *759,982*     759,982
ARN     *0000382183*     01525871     *759,982*     759,982
ARN     *0000382183*     01516263     *688,100*     688,100
ARN     *0000382183*     01516263     *688,100*     688,100
ARN     *0000382183*     01516263     *688,100*     688,100
ARN     *0000382183*     01515789     *682,680*     682,680
ARN     *0000382183*     01510809     *624,140*     624,140
ARN     *0000382183*     01507681     *590,261*     590,261
ARN     *0000382183*     01507681     *590,261*     590,261
ARN     *0000382183*     01507681     *590,261*     590,261
ARN     *0000382183*     01484993     *454,927*     454,927
ARN     *0000382183*     01484993     *454,927*     454,927
ARN     *0000382183*     01484993     *454,927*     454,927
ARN     *0000382183*     01477592     *448,231*     448,231
ARN     *0000382183*     01477592     *448,231*     448,231
ARN     *0000382183*     01456544     *428,902*     428,902
ARN     *0000382183*     01456544     *428,902*     428,902
ARN     *0000382183*     01429778     *403,175*     403,175
ARN     *0000382183*     01427677     *401,055*     401,055
ARN     *0000382183*     01427677     *401,055*     401,055
ARN     *0000382183*     01427677     *401,055*     401,055
So, as you can see, I want the query to select from the above results, the top/1st record of "Comment_Text" and set it to the ":parn" WITHOUT prompting the user to enter it. Then I want the script to change all other "Identifier_Type" to 'ARN2' that do not share the first "Comment_Text"....so in this example, I would want all 'ARN' changed to 'ARN2' that do not have "Comment_text" = 01550228.
I hope this isnt too confusing, because it is to me. Thanks in advance for all the help.

Hi,
So you have a query that shows all rows, and you want to UPDATE the rows in that reuslut set that do not have the greatest value of comment_text. You can use the analytic DENSE_RANK function to number the distinct comment_texts, and then discard the rows that were assigned #1. the remained will be the ones you wqant to UPDATE.
Here's one way to do that:
UPDATE     offender_case_idetifiers
SET     identifier_type     = 'ARN2'
WHERE     (offender_book_id, comment_text)
     IN (
            WITH    got_r_num    AS
                SELECT  vhb.offender_book_id
             ,        oci.comment_text
             ,        DENSE_RANK () OVER (OREDER BY oci.comment_text  desc)
                             AS r_num
             FROM    offender_case_identifiers oci
             ,         v_header_block           vhb
             WHERE   oci.offender_book_id      = vhb.offender_book_id
             AND         vhb.offender_id_display   = :ppid
             AND        oci.identifier_type           = 'ARN'
            SELECT  offender_nook_id
            ,       comment_text
            FROM    got_r_num
            WHERE   r_num     > 1
AND     identifier_type     = 'ARN'
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
If you're asking about a DML statement, such as UPDATE, the sample data will be the contents of the table(s) before the DML, and the results will be state of the changed table(s) when everything is finished.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ {message:id=9360002}

Similar Messages

  • UPDATE statement based on query and return multiple value

    Hi everybody,
    I have two tables: temp3 and temp
    One of them (temp3) should be updated based on the query where clause (id and flag columns).
    I run this query but it updates all of the records in the temp3. I want to update only records with 'UPDATED' flag.
    update temp3 t3
    set (id,name,address) = (select t.id, t.name, t.address from temp t, temp3 t3
    where t.id = t3.id and t.flag = 'UPDATED');
    Does any body know how I can do it?
    I appreciate your help
    Thx

    Hello
    Basically you're missing a where clause on your update statement to restrict rows in t3 to the ones that have a corresponding row in t1.
    SQL> select * from dt_test_t1;
            ID NAME                 ADDRESS              ROW_STATUS
             1 Joseph Bloggs        Some street          UPDATED
             1 Joe Bloggs           Old street           OLD
             2 Fredrick Bloggs      New street           UPDATED
             2 Fred Bloggs          Some street          OLD
             3 Robert Bloggs        Better street        UPDATED
             3 Bob Bloggs           Some street          OLD
           100 Barry Bethel         Some street          UPDATED
           200 Giles Brandreth      Some street          UPDATED
    8 rows selected.
    SQL> select * from dt_test_t3;
            ID NAME                 ADDRESS              ROW_STATUS
             1 Joe Bloggs           Old street
             2 Fred Bloggs          Some street
             3 Bob Bloggs           Some street
             4 Joe Smith            Some street
             5 John Doe             Some street
    --this update as it stands does not work as it updates rows to have
    --null name and address where there is not a proper matching row in
    --t1
    SQL> UPDATE
      2     dt_test_t3 t3
      3  SET
      4     (       t3.name,
      5             t3.address,
      6             t3.row_status
      7     ) = (SELECT
      8             t1.name,
      9             t1.address,
    10             t1.row_status
    11          FROM
    12             dt_test_t1 t1
    13          WHERE
    14             t1.id = t3.id
    15          AND
    16             t1.row_status = 'UPDATED'
    17         );
    5 rows updated.
    SQL> select * from dt_test_t3;
            ID NAME                 ADDRESS              ROW_STATUS
             1 Joseph Bloggs        Some street          UPDATED
             2 Fredrick Bloggs      New street           UPDATED
             3 Robert Bloggs        Better street        UPDATED
    4
    5
    SQL> rollback;
    Rollback complete.
    --Now add in the where clause to make sure we're only updating rows in
    --t3 that have a corresponding row in t1:
    SQL> UPDATE
      2     dt_test_t3 t3
      3  SET
      4     (       t3.name,
      5             t3.address,
      6             t3.row_status
      7     ) = (SELECT
      8             t1.name,
      9             t1.address,
    10             t1.row_status
    11          FROM
    12             dt_test_t1 t1
    13          WHERE
    14             t1.id = t3.id
    15          AND
    16             t1.row_status = 'UPDATED'
    17         )
    18  WHERE
    19     EXISTS( SELECT
    20                     NULL
    21             FROM
    22                     dt_test_t1 t1
    23             WHERE
    24                     t1.id = t3.id
    25             AND
    26                     t1.row_status = 'UPDATED'
    27             );
    3 rows updated.
    SQL> select * from dt_test_t3;
            ID NAME                 ADDRESS              ROW_STATUS
             1 Joseph Bloggs        Some street          UPDATED
             2 Fredrick Bloggs      New street           UPDATED
             3 Robert Bloggs        Better street        UPDATED
             4 Joe Smith            Some street
             5 John Doe             Some streetHTH
    David

  • Update statement with nested selects and alias usage

    Hello, we are trying to build an update statement like this...
    update table1 t1
    set attr1 = ( select someattr
    from (nested select 1) as aux1
    (nested select 2 (nested select 2.1 + nested select 2.2)) ) as aux2
    where some_join_clauses
    where t1.attr2 = 123
    and t1.attr3 = 'abc'
    Alias t1 shound be known at the level of nested select 1,2 and 2.1, 2.2? We are receiving an error message saying that t1.someattr is an invalid identifier. Can you help? Thanks!

    mauramos wrote:
    Hello, we are trying to build an update statement like this...
    update table1 t1
    set attr1 = ( select someattr
    from (nested select 1) as aux1
    (nested select 2 (nested select 2.1 + nested select 2.2)) ) as aux2
    where some_join_clauses
    where t1.attr2 = 123
    and t1.attr3 = 'abc'
    Alias t1 shound be known at the level of nested select 1,2 and 2.1, 2.2? We are receiving an error message saying that t1.someattr is an invalid identifier. Can you help? Thanks!You can only reference elements nested 1 level deeper in a correlated update statement.
    I'd suggest you try with the MERGE statement, or alternatively, post some sample data (insert statements) and table DDL (create statements), with an 'expected output' description and we can try to help you build a suitable statement.

  • Xperia Z2 Lollipop Update PC Companion Error 11 and other Xperias problem in updating

    Hi,
    I'm trying to update my Xperia Z2 D6503 to 5.0 Lollipop throuh Sony PC Companion. However, when it finishes preparing it says "Unable to run update components on your computer" and "Unable to communicate with Sony Update Engine (error 11)."
    I've tried disabling my firewall but that doesn't help either.
    Some details that might be helpful:
    - my Xperia Z2 D6503 was bought unlocked from a local Sony Store
    - my computer is Windows 7 64-bit Home Premium
    - I received the notification for the update (or saw it) around 4pm PST
    Thanks for your time!

    Can you help me?
     I want update Xperia M2 aqua
     I have windows 10
    I did
    but, again:
    unable to communicate with sony update engine (error 11)
    and, i did Disable the Antivirus and firewall
    but, again:
    unable to communicate with sony update engine (error 11)

  • Firefox crashing after update, submitted by Bolivar1234, ribbo, and others

    Same problem as several other people after installing the new 32.0.1, this time on 64-bit Windows 7. I would have simply replied to other people posting, but the Mozilla Support pages no longer provide a flow to register a new account in order to reply to a post. New accounts can only be registered when submitting new questions. This is a separate bug.
    Now to the browser problem.
    When the browser starts, it appears to function _until_ it attempts to update a page. If it attempts to update a page on startup, it fails to load the page and the title bar changes to "Not Responding." Or, on the first link that the user clicks, it again fails to follow the link and the title bar changes to "Not Responding."
    This is _not_ a crash per se. There is no crash information captured and no crash report submitted. The browser simply hangs.
    I have found a temporary workaround by starting Firefox in safe mode, then under
    Tools -> Options -> Advanced, I turned off "Use hardware acceleration when available."
    For now, this allows me to use the browser.
    My hardware is a Lenovo Thinkpad Edge E13.

    '''[https://support.mozilla.org/en-US/kb/uninstall-firefox-from-your-computer Uninstall Firefox from your computer]''' {web link}
    Then
    '''[http://www.mozilla.org/en-US/firefox/all/ Download Firefox Full Installer For All languages And Systems]''' {web link}
    Download the installer and save it somewhere first.

  • Create sales order basis and other

    Good afternoon,
    I need to create a sales order with a copy to another order of sale. Ever used the control of copy of order to order, plus the flow of documents are all linked, ie every order that I create, will be linked to one another ... When you need to check the orders, I see all the flow of documents, where my intereese is only copying the data from order to order.
    Someone has passed this and can share the solution?
    Tks.
    Eduardo Rebelo

    Hello,
    Yes, this is correct solution. But i want the to see the "display document flow" one sales order and not all sales order. If i need to check one sales order, i will go see all sales order the button display document flow.
    Will be a bit confusing to analyze sales orders by button display document flow.
    Lets me know you understand.

  • Software Update, App Store, Terminal, iCal and other Apple Apps will not open.

    Hi,
    I've exhausted all my search methods and still haven't fixed this issue. I have been disconnected from the internet for about 5 months up until now while continually using my MacBook Pro (early 2011) daily for work. Problems arose about a month and a half ago when I noticed the terminal would not open. I trashed the corresponding plist files as recommended by other websites and trouble tickets. Still no avail, the affected programs all seem to be original software that came with Mavericks. When I try to open any of them (Software Update..., App Store, Terminal, iCal, iTunes Store) nothing happens. The dock makes a space for the icon and then before anything even bounces it quickly closes the space again. I've tried opening the contents of the applications and running the root program directly.
    Possibly other relevant information
    Ran Repair Disk Permission on the Macintosh HDFound errors on drive and repaired them
    iTunes will open, but the store in unresponsive (White screen)
    Ran into issues copying files to Macintosh HD from an external USB driveError example "JX879.MXF is in use" and then it would cancel the copy while nothing else was open
    At the same time of these issues my internal iSight camera no longer exists according to the system report
    Upgrading to Mac OSX 10.94 Mavericks had no affect.
    Reset the System Management ControllerHave a recent history of the computer behaving abnormally slow
    Any information anyone could give me would be great. Thank you!

    Launch the Console application in any of the following ways:
    ☞ Enter the first few letters of its name into a Spotlight search. Select it in the results (it should be at the top.)
    ☞ In the Finder, select Go ▹ Utilities from the menu bar, or press the key combination shift-command-U. The application is in the folder that opens.
    ☞ Open LaunchPad. Click Utilities, then Console in the icon grid.
    The title of the Console window should be All Messages. If it isn't, select
              SYSTEM LOG QUERIES ▹ All Messages
    from the log list on the left. If you don't see that list, select
              View ▹ Show Log List
    from the menu bar at the top of the screen.Click the Clear Display icon in the toolbar. Then try the action that you're having trouble with again. Select any messages that appear in the Console window. Copy them to the Clipboard by pressing the key combination command-C. Paste into a reply to this message by pressing command-V.
    The log contains a vast amount of information, almost all of which is irrelevant to solving any particular problem. When posting a log extract, be selective. A few dozen lines are almost always more than enough.
    Please don't indiscriminately dump thousands of lines from the log into this discussion.
    Please don't post screenshots of log messages—post the text.
    Some private information, such as your name, may appear in the log. Anonymize before posting.

  • Update failed causing App Store and other apps to not open anymore

    HELP!!
    I was trying to make a general update of OS X v10.8.2 as sugested from the App Store, but something went wrong and it showed "could'nt be updated because of a problem". Now I can't open App Store, Mail and iTunes shows a message saying "This copy of iTunes is corrupted or is not installed correctly. Please reinstall iTunes". Only I can download anything anymore and the App Store won't even start!
    can anyone PLEASE help?

    See if you can download and install the OS X Mountain Lion Update v10.8.4 (Combo)
    If not, restore OS X.
    Restart your Mac while holding down the Command + R keys so you can access the built in utilities using OS X Recovery
    From there you can restore your Mac.

  • Trying to update Graphics drivers...and others

    Hello,
    I am having a few issues with my laptop.  Think I need to upgrade the graphics drivers. Possibly update other things as well.  Doesn't seem like it's any of the basic "Windows Updates" either.  When I download the Intel driver update, it tells me I need to go to the computer manufacturer...which of course is Toshiba...instead of thru them.
    Any guidance?  How do I update the graphic drivers and check for other updates, drivers or whatever else, for my Toshiba laptop?
    Laptop = Satellite P55t - A5118 running on Intel i7.
    Plz lemme know...thx.
    Dom

    Satellite P55t-A5118
    Downloads here.
    Get all your drivers from the Toshiba site above.
    Many use the Toshiba application "Service Station" to alert them for updates. It's already on your computer, but the latest version can be obtained from here.
       TOSHIBA Service Station
    -Jerry

  • Free iWeb Templates and other goodies?

    Hey, it's me again!
    I was looking for FREE iWeb templates but I haven't been able to any find free ones or great ones.
    Do you know where to get them?
    I was also looking for other free goodies that are on iweb, such as little doodles and pieces of paper. You know what I mean?
    Do you know where to get them?
    You're always appreciated :)

    Hello MissMacintosh,
    MissMacintosh wrote:
    Hey, it's me again!
    Always nice to hear from you.
    As you, I did an extensive search (3 months ago) on Google and Ask, only to find out that there is a poor choice of great / free templates for iWeb '08.
    There are 1 or 2 "user understandable" manuals on how to make your own templates, but I found it less time consuming to choose the "blank" template from a theme, adapt it myself with the tools provided in iWeb and then copy this "custom adapted" template.
    I know this is not the answer you are looking for but it is the best I can provide.
    Hopefully, someone else has found the gold somewhere...
    Kind regards,
    Leo

  • ZFS boot and other goodies

    Hi everyone,
    With the new funky OS-features in Solaris 10/08, does anyone know if such features are going to get support in the OSP/SUNWjet/N1SPS? ZFS boot would be nice, for a change :)
    I haven't seen any updated versions of the OSP plugin for N1SPS for quite a while now, is it still under development?
    Cheers,
    Ino!~

    Hi Ino,
    as far as I know (and I might be mistaken) OSP is not under any active development and all bare metal OS provisioning activities are now domain of xVM Ops Center, which is built on top of Jet, which does support ZFS root/boot installation already.
    If you want to get hacky, you can replace the SUNWjet package on your Jet server by hand (pkgrm/pkgadd), put there the fresh one and SPS/OSP should happily work with it (read: I have not tested it myself)...
    If you want to get supported, then go the xVM OC 2.0 way...
    HTH,
    Martin

  • Mac OS X Server 10.6.7 update - Time Machine not working and other trouble.

    I've upgraded my mid 2010 mac mini server to 10.6.7 with server Combo download.
    After installation (without any errors) i've detected this troubleshooting.
    Time Machine (backup hd is the 2nd 500GB Hd of mini server) dont work more and
    display error message that suggest to verify hard disks (done with no errors detection).
    Mac Help display white page.
    And if try to install Server Admin Tools, installation utility not recognize system HD and cannot be done.
    The mac mini server seems however works and services seems works rights. Can be rebooted without problems.
    I've tried to backup the system disk on the second hd with Carbon Copy Cloner without problem and it recognize system disk well.
    I've upgraded also two others mac minis (mid 2010) with standard 10.6.7 version (no server) and a macbook and a macbook pro without problems (but i've not tried time machine on them).
    Thanks in advance for any help!

    I know the problems with Time Machine on Mac OS server. In your case: Look at the log files, searching for "backupd", if that tells you anything.
    In my case, Time Machine in the last year stopped several times. Occasionally, there was a helpful message in the log files where I could identify a file where it had hung -- typically intermediate build results of Xcode. After excluding or deleting the offending files (where possible), Time Machine ran successfully.
    Another situation which has repeatedly occurred is that time machine just hangs and does not progress, and no log entry helps. In 3 such cases what helped was to delete the spotlight index (e.g., with TinkerTool System) and let it rebuild. It took inordinately long (10 hrs) and there were log entries by mdworker that it had difficulty processing files. But when the spotlight index was finally rebuilt, Time Machine worked again.
    BTW: I don't need Time Machine to backup the server system, for that I run mirrors etc., but for user files it is most convenient and has helped several times. Thus I have excluded nearly everything but /User/*.

  • XBox, Final Fantasy, CIVREV, and other goodies

    So, my boyfriend and I finally made the transition to a gamer family when we purchased a (new to us) XBox 360.
    I'll be honest, I've never really been that much of a gamer, but now, my boyfriend are fighting over the XBox (where it used to be the computer).
    So, I've been playing two games now. Final Fantasy XIII and Civ Rev.
    Any thoughts on either games? What do people like or dislike about the games? I'm going to post more after I get back from my lunch break.
    Don't take what I say as being absolute. There's always room for improvement.

    I've thought the same thing Entropy, especially regarding the characters thus far. The charachter's personalities seem very two dimensional, and don't seem to grow much.
    I do have to say, I like the idea of the Crystogenisis system, but I think the actual system leaves a little to be desired. I just wish there was more choices, more growth options for the characters in terms of what they do. I like the various roles so far (I'm LOVING the Sabatuer role), but it has come a long ways from the old days of there being a warrior, white mage, black mage and the like.
    Also, I have to admit, visually, it's breathtaking. The colors are deep and the beasts are so impressive. I just wish I had a bigger TV to watch it on (plus, the font of the game is a little small on my 26" screen).
    Don't take what I say as being absolute. There's always room for improvement.

  • Since updating to 5.0 avg and other programmes won't run and won't let me change to I.E or google chrome, WHY? VERY, VERY UNHAPPY!

    No matter what I try to open computer asks what I want to use to open file with. AAARRRGGGHHH!!!

    What you're describing sounds like it could be one of a few things:
    1) It's possible that the wireless card in your computer has something wrong with it.
    2) You've may have some sort of obscure problem with your computer's firmware, and you should resinstall the operating system.
    3) There actually is something wrong with your internet connection.
    I know that the 3rd one sounds unlikely, given that your other computer works fine on your internet connection. However, for whatever reason, your internet modem could just be spitting out bandwidth in unequal quantities to each computer. This could also be caused by an inconsistency in your router's QoS settings.
    Either way, I would advise that you make an appointment at the Apple store, or some reputable computer repair shop. I think that the most likely of these three problems would be that something is wrong with your wireless card.
    I hope this helps!

  • After updating cannot contact Google, Mozilla, and other addresses

    I have a Macintosh using a Lion Mac OS X, version 10.7.3, with a 2.5 GHz Intel Core i5.
    Since I have updated Firefox When I try to use www.google.com to contact any company I receive a window stating:
    Firefox can't establish a connection to the server at www.google.com.
    How do I reestablish the connection?

    Thanks. The matter was resolved.

Maybe you are looking for

  • Phone no longer recognized by my computer or my wall charger...HELP

    After the 2.0 upgrade had a few issues, still have a few of the apps quitting etc. these I can deal with while waiting for the fixes. The big problem is the phone now cannot connect to the computer or the charger, tried different cables {still can ch

  • How to get two separate data in a single excel file

    I have a VI from where i get data in 3 columns.I have added a subVI & wants the data coming out of it to be stored in the 4th column along with the initial data real time.How can i do that?

  • Stop email every time someone adds event to shared calendar?

    I have searched high and low for a solution to this, but can't find it anywhere. I share an iCal calendar with someone via iCloud. We both have read/write priveleges, but he created the calendar. In the "Get Info" for this calendar, I have Ignore Ale

  • Mobile Game Programming

    Hey All, I am a decent Java programmer but in last few days I found Java Game Programming intresting and has strong future for ones career. I wanted to know what do Expert over here have to say about this ? and Where do I need to start In order to ge

  • How to install printer on windows

    hi, i bought a canon mp460 that works through my airport express from my mac.... but how would i add this printer to a windows machine??? windows are so complicated... please help me figure this out!?!? help is very appreciated