How do I order this select?

I have a query that produces the following result:
mysql> select patient.patient_id_string, survey.survey_date
-> from survey, patient where survey.patient_id = patient.patient_id and survey.survey_score is not null and patient.location_id = 1
-> order by survey.survey_date desc, survey.survey_time desc, patient.patient_id_string;
--------------------------------+
| patient_id_string | survey_date |
--------------------------------+
| 2134 | 2007-07-27 |
| 2134 | 2007-07-27 |
| 8967 | 2007-07-26 |
| 2345 | 2007-07-25 |
| 2134 | 2007-07-25 |
| 5234 | 2007-07-25 |
| 2453 | 2007-07-25 |
| 5243 | 2007-07-25 |
| 3452 | 2007-07-25 |
| seuoth | 2007-07-11 |
| 23454523 | 2007-07-11 |
| 245524 | 2007-07-11 |
--------------------------------+
12 rows in set (0.00 sec)
However, I would actually like to sort it by desc survey date, but if there is a patient_id_string with multiple entries, then those are grouped together. In the above example it would look like:
--------------------------------+
| patient_id_string | survey_date |
--------------------------------+
| 2134 | 2007-07-27 |
| 2134 | 2007-07-27 |
| 2134 | 2007-07-25 |
| 8967 | 2007-07-26 |
| 2345 | 2007-07-25 |
| 5234 | 2007-07-25 |
| 2453 | 2007-07-25 |
| 5243 | 2007-07-25 |
| 3452 | 2007-07-25 |
| seuoth | 2007-07-11 |
| 23454523 | 2007-07-11 |
| 245524 | 2007-07-11 |
--------------------------------+
(the third 2134 patient id was grouped with the other two)
I just can't figure this out. Any ideas? I know I should be able to do it in the database rather than in my own code.

This..?
SQL> select * from test;
        ID DT
         1 28/07/2007
         2 26/07/2007
         2 23/07/2007
         3 27/07/2007
SQL> select * from test
  2  order by max(dt) over(partition by id order by  null) desc,dt desc;
        ID DT
         1 28/07/2007
         3 27/07/2007
         2 26/07/2007
         2 23/07/2007                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Similar Messages

  • How do i include this 'select' stmt in a 'procedure'.

    create or replace procedure proc1
    AS
    BEGIN
    select table1.name,table1.symbol,table1.quantity,table2.price,(table1.quantity*table2.price) AS Total
    from table1,table2
    where table2.date=(select date from
    table2,table1,table3
    where table1.date=table3.date
    AND table1symbol=table2.symbol)
    END;
    here, name and symbol are varchar2
    and quantity and price are number
    date is date
    the main problem is tht select in a procedure requires an INTO clause
    The normal select query is running but i am unable to transform it into a procedure using variables and cursors
    can u solve this prob for me...??

    > The normal select query is running but i am unable to
    transform it into a procedure using variables and cursors
    There are a couple of ways to define cursors - even a plain SQL as what you have posted is a cursor.
    The details:
    Oracle® Database PL/SQL User's Guide and Reference
    Chapter 6. Performing SQL Operations from PL/SQL
    http://download-east.oracle.com/docs/cd/B19306_01/appdev.102/b14261/toc.htm
    The basics - for an explicit cursor you want to use bulk collection 99% of the time in order for performance and scalability. So (assuming the SQL has been analysed and optimised):
    create or replace procedure proc1 as
    -- define an explicit cursor
    cursor myCursor is
    select
    table1.name,table1.symbol,table1.quantity,
    table2.price,(table1.quantity*table2.price) AS Total
    from table1,table2
    where table2.date=(
    select
    date
    from table2,table1,table3
    where table1.date=table3.date
    and table1.symbol=table2.symbol
    -- define an array type for fetching the rows into
    type TBuffer is table of myCursor%ROWTYPE;
    -- define an array for fetching the rows into
    buffer TBuffer;
    begin
    open myCursor;
    loop
    -- fetch a max of 1000 rows at a time
    fetch myCursor bulk collect into buffer limit 1000;
    -- process these rows
    for i in 1..buffer.Count
    loop
    -- buffer needs to be subscripted to get to the row,
    -- and buffer contains the columns that were selected
    -- from the tables, e.g.
    DBMS_OUTPUT.put_line( 'Processing '||buffer(i).name );
    Proc2( buffer(i) );
    end loop;
    exit when myCursor%NOTFOUND;
    end loop;
    close myCursor;
    end;
    All the details of bulk processing and cursors are in the PL/SQL User Guide - with examples.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • How can I fix this select without using subquery?

    Hi guys is there any way to do the where below (in bold) without using an aggregate query before?
    select
    SUM(NET_AMOUNT)
    as net,
    SUM(TAX_AMOUNT)
    as tax,
    SUM(NET_AMOUNT)+
    SUM(TAX_AMOUNT)
    as total from ledger left join alloc on ledger.trxid=alloc.trxid where
     (SUM(NET_AMOUNT)+
    SUM(TAX_AMOUNT))
    > allocated
    net and tax are in the ledger and allocated is in the alloc table. Sometimes the total of the amount (net+tax) is greater than the allocated so I need to retrieve all the rows where allocated is null or  tot is greater than allocated.
    It's possible without CTE?
    Thanks

    Resolved.
    SUM(NET_AMOUNT)
    as net,
    SUM(TAX_AMOUNT)
    as tax,
    SUM(NET_AMOUNT)+
    SUM(TAX_AMOUNT)
    as total from ledger left join alloc on ledger.trxid=alloc.trxid ....group by....
    having ((SUM(NET_AMOUNT)+
    SUM(TAX_AMOUNT))
    > allocated)
    Sometimes I need to post the issue in the forum just to remember how do the things.
    Thanks and sorry

  • I am having trouble with my bookmark bar changing order between my devices. I have them organized for convenience, but when I arrange them on one device they change on the others to a different order. how can I fix this?

    I have 2 desktops and a laptops connected by Firefox sync which I set up at some time during the beta. I have several folders, dividers, and bookmarks on the toolbar which I try to keep organized based on their use (web design, hardware/tech support sites, personal, etc) but when I arrange them on one computer it screws them up on the others. I would like for it either to keep them in the same order on all machines, or at least have to arrange them on each machine separately without them affecting the other so they stay where I put them. Does anyone know how I can accomplish this?

    This is what I'd suggest get your machines matched. Sync your machines so all data is on both. Then pick one to get all of your organizing as you want it. (or do this before you even setup sync) Then, when you do your initial sync from that organized machine, select: replace all other devices with this computers data. (you can also do that as Manage Account > reset sync.
    After that, changes in order on one machine should be reflected on the other. Remember though Sync '''merges ''' the data from your machines. If you work on two machines side by side and make order changes on both without syncing either, once you do sync, Sync will have to again figure out how to merge the incongruities, which may not be what you expect.. Be careful, if order is important to you.

  • How to add order reason code field to selection screen of VL10

    Hi,
       How to add Order reason code(vbak-augru) to the selection screen of VL10.
      1. How to find enhancement for it?
       2.I want to display Order reason code field on output list also?
      3.Pls give steps i am new to enhancements
    thanking u advance...
    surya

    Hi,
    there is no user exit available for this report program, one alternative is copy the program associated with this standard program i.e. RVV50R10C to a Z program and modify it as per your requirement or if you want to modify the standard program you need access key.
    Regards,
    Raghavendra

  • When I was ready to place the order for my iPhoto book, I realized that it was for a soft cover book and I wanted hard cover.  How can I change this without losing my prepared book?

    When I was ready to place the order for my iPhoto book, I realized that it was for a soft cover book and I wanted hard cover.  How can I change this without losing my prepared book?

    Duplicate your book and try to change the theme in the copy.  Chnaging the theme may change the layout  of your text fileds. That is why you need the copy to be able to compare the pages before and after.
    Ctrl-click the book in the Source list and select "Duplicate".
    Click the "Change Theme" button in the upper right corner of the Book pane, make sure, you have the same theme selected and the same size. Click Hardcover.

  • My MacBook pro won't allow me to double click to open files or select the music I want to play, how do I fix this?

    When going to open a file or select a song to play in iTunes or opening links I used to be able to double click on my track pad to open files or links, now I have to tap once with two fingers to open the "right click" option on a normal mouse pad to be able to select it. How do I fix this in order to be able to just double click the track pad to open the folder or link?

    Hi kylec4544,
    I'm sorry to hear you are having this issue with your MacBook Pro. If you are having issues opening things by double clicking on them, you may want to double check to make sure the double click speed hasn't accidentally been set too high, as noted in the following article:
    OS X Yosemite: Mouse & Trackpad pane of Accessibility preferences
    Regards,
    - Brenden

  • How BOM and routing is selected for Planned order and Production order?

    Hi,
    Can any1 plz tell me how BOM and routing is selected for a planned order and production order. ?

    Hello Mathisuthan,
    BOM and Routing selection for the planned order and production order through production version, If u have more than one BOM and more than one Routing then u can maintain this information as Production Version in the system.
    Production version you maintained
    MM01/MM02 -- MRP4--- Production version
    Or you can create Production Versions in Mass also with Transaction Code "C223"
    In the case no production version maintained/created  for the material, then system by default  will pick the first BOM and routing.
    I hope this information helpful to you.
    Regards
    Umesh Mali

  • When I burn a disk from my playlist the songs are out of order. How can I fix this? Thanks!

    When I burn a disk from my playlist the songs are out of order. How can I fix this? Thanks!

    I'm not 100% sure but I think you create your play order manually then select the playlist, right click it (or use ctrl+click) & select "Copy to play order".
    I think when you burn that playlist it should obey that order, it has been so long since I made a mix CD that I'm not certain, good luck.

  • How can i order iphone 5 to Japan? Can this new iphone use in Japan?

    how can i order iphone 5 to Japan? Can this new iphone use in Japan?

    Settings > Wi-Fi then select your wireless network from the list

  • When I open a PDF document and select print, Adobe stops working and closes.  How do I correct this?

    When I open a PDF document and select print, Adobe stops working and closes.  How do I correct this?

    Hi tbytrzll,
    In order to help you better please let me know below mentioned information:
    - Operating system version and name.
    - Version of Adobe reader.
    - Have you tried to update the Reader and if you are trying to print using any other application what is the result.
    Have you tried any steps to resolve the issue.
    Regards,
    Ajlan Huda.

  • Unable progress the lines due to ware house not selected.How do we fix this? So that it will pick the warehouse automatically

    We are unable progress the lines due to ware house not selected. We checked the Defaulting rules everything looks good. But line Items not picking the Warehouse - based on the sourcing rules. 
    If we enter manually and then progress lines getting scheduled.
    How do we fix this? So that it will pick the warehouse automatically.

    Hi,
    The issue with ATP- it’s not sourcing from the ware house, even though the sourcing rules defined the correct warehouse. When we enter manually and progress –order is progressing.
    Thanks

  • When I transfer photos as events, they change their order from iPhoto on my Mac, how do I stop this?

    When I transfer photos as events, to my iPad they change their order from iPhoto on my Mac, how do I stop this?

    Thank you for your response.
    I am sorry I should have said Image Capture not Previews. The files end up as duplicates on my desktop or in Pictures. I was told by someone that the same thing happens in iPhoto as well as not being able to select which photos to save and that using Image Capture should be better.
    My iPhoto is version 8.1.2 and Image Capture is 6.0.1.
    Thanks
    Desmond

  • Thunderbird release 31.3 messes up auto-complete (suggesting incorrect options) and changes email addresses even once selected. How can we fix this?!?

    Prior to release 31.3, when I compose an email and start typing the first character it would pop up a list of my contacts in alphabetical order, and I could select one, and hit enter to open a new line. Following the update to version 31.3, when I compose an email and start typing the first character, the drop-down list of email addresses are no longer in alphabetical order, such that it provides options that make no sense with the first character I typed. In addition, when I finally find the email address I'm looking for and select it, then I hit enter to open a new line, and it changes the email address I just selected to another contact. How do we fix this issue? I want the old Thunderbird back!!! Or, at least, I want the new version to function correctly. Please help!

    https://support.mozilla.org/en-US/questions/1037642

  • Unable to delete email from trash bin.  How do I do this?  If I select and chose delete, it doesn't.  What am I doing wrong?  My grand daughte is using my iPad and I don't want her to see some of the mail there.

    Unable to delete email from trash bin.  How do I do this?  If I select and chose delete, it doesn't.  What am I doing wrong?  My grand daughte is using my iPad and I don't want her to see some of the mail there.

    There are two ways that should work. When you are in the trash folder, you can swipe on the email in the list - left to right - and a red Delete button should pop up. Or you can select - Edit - at the top of the list of messages and then select the message and tap delete.
    Not sure exactly how this works - but if you have restrictions enabled - especially since you let your grandchild use the device - that may be restricting what you can do in the mail app. Go to Settings>General>Restrictions. If you have it set to on - turn it off or see if you have mail restricted.

Maybe you are looking for

  • IPhone 4 will not connect to Apple

    No matter how hard I try, or how many times I try I cannot get my sister in law's iPhone 4s to update it's software to 5.1  Despite it being connected to a strong wireless signal that I managed to update mine from on my laptop, it downloads the updat

  • Why won't Mail save stationery?

    I just did a clean install of 10.6, and Mail won't save custom stationery I try to create. It does create the Custom category and shows what I saved there, but the text I entered is missing - only my signature shows up. The message has several of Mai

  • Weird Laptop Wireless Connection Problem

    I have an HP DV6 6C16NR i7-2670QM laptop and am having a very annoying wireless internet connection problem. I'll connect fine to my home network, but if I restart and try to connect again, it won't. The only way to fix it, is if I change the channel

  • I purchased the CS6 Master Collection and it will not install on my Mac.

    I keep getting an Installation Failed message- It says Inconsistency in the installer database.  I have tried restarting my computer but this does not help.  My Mac is fairly new - about 8 months old and is running the OS X 10.8.2. Any ideas??

  • Reversing Order of Slides in Slideshow

    We were finalizing a slideshow and went into Settings and must have accidentally clicked some shortcut - our entire 124 slide slideshow reversed in order. Does anyone know what happened? More important, how can we reverse this? Thanks.