Note that there are some explanatory texts on larger screens.

plurals
  1. POComplex Oracle query involving max date
    primarykey
    data
    text
    <p>I'm having trouble getting the data I want from an Oracle database. Any help would be greatly appreciated. Here's a sample of what my table looks like:</p> <pre><code>Table: Vaccinations Patient_ID | Shot_ID | Series | Date_Taken ------------------------------------------- 123 | 5 | B | 8/1/2011 123 | 5 | 3 | 2/1/2011 123 | 5 | 2 | 1/10/2011 123 | 5 | 1 | 1/1/2011 456 | 3 | 2 | 1/10/2011 456 | 3 | 1 | 1/1/2011 123 | 5 | 2 | 10/1/2010 123 | 5 | 1 | 9/1/2010 </code></pre> <p>The series column indicates which shot was administered for a particular <code>Shot_ID</code>. 'B' indicates a booster was given, '2' means second, '1' means first, and so on but '3' is the max and then boosters follow. What I'm trying to do is grab all of the latest series of shots for a patient, for a particular type of shot (<code>Shot_ID</code>). For example, I want to grab patient 123's latest series of shots for <code>Shot_ID</code> = 5, so I would want to return the first four records in this case (all colums should be returned in those rows). The last two should be omitted because a new series of shots was started on 1/1/2011. Anyway, I have an algortihm in mind, but I'm having trouble writing the query for it. It would go something like this:</p> <ol> <li><p>Get the Max Date for patient 123, <code>shot_id = 5</code>. Return the row and look at its series (in this case, 'B').</p></li> <li><p>Get the next lowest date from the Max Date and look at its series (in this case, '3'). If the series is between 1 and B, return the row. If no other records exist, then end the query.</p></li> <li><p>Get the next lowest date from step 2 and look at its series (in this case, '2'). If the series is less than the series from step 2, return the row. Otherwise, end the query.</p></li> </ol> <p>You keep repeating these steps till you get to <code>series = 1</code>, which is returned or till you reach a series that is greater than or equal to the current series, which is not returned. So , the output should look like this:</p> <pre><code>123 | 5 | B | 8/1/2011 123 | 5 | 3 | 2/1/2011 123 | 5 | 2 | 1/10/2011 123 | 5 | 1 | 1/1/2011 </code></pre> <p>This query seems to be pretty complex, but maybe I'm just overthinking it. Thank y'all for your time.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload