Question

I need to get the records which are in a range of 20 days following a certain process.

There is a table medical that contains data as shown below:

Here is the SQLFIDDLE:

enter image description here

Rule is : If Group is A3, then all operations within 20 days (to the same patient in the same hospital) are free of charge.

So, here all the operations until 09.02.2013 should be free of charge. (20.01.2013 + 20 days)

Question: How can I retrieve all the records within 20 days following the day an A3 group process is applied?

Was it helpful?

Solution

I took the liberty of changing the datatype of dat and changed the name of group to groupe.

WITH modified_medical
     AS (SELECT hospital, pid, dat
           FROM medical
          WHERE groupe = 'A3')
  SELECT *
    FROM medical a, modified_medical b
   WHERE     b.hospital = a.hospital
         AND b.pid = a.pid
         AND a.dat BETWEEN b.dat AND b.dat + 20
ORDER BY a.hospital, a.pid, a.dat;

Sample fiddle here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top