简体   繁体   中英

Single MySQL query with row averages based on conditions

I have problem, where i know how to proceed with multiple queries, but dont really now

I Have a table with

  BP {
  name: varchar
  systolic : int
  diastolic : int
  timestamp: date
  }

I need to get the Names from BP table that satisfy the following ANY of the following conditions.

  1. If systolic > 180 OR diastolic > 110
  2. If (systolic >= 140 AND systolic < 180) , then take the next 2 readings of systolic, get the average and if that average is >= 140. Then Condition Satisfied.
  3. Same diastolic. If (diastolic >= 90 AND systolic < 110) , then take the next 2 readings of diastolic, get the average and if that average is >= 90. Then Condition Satisfied.

There will be more many rows with the same unique name . (meaning many BP Recordings per person).

I can certainly get this working if i jus get all the values and parse it myself in PHP.

But Im wondering if theres a better way to do it directly in MySQL.

Update

My goal here was to find the people that have Hypertension. And those conditions above the guidelines in categorising a person as Hypertensive.

In this example, I simplified the table. And I want to get the name(s) of those that meet these hypertensive guidelines.

Condition 1 is very simple to address. Condition 2 & 3 are whats troubling me. Right now, I simply get the list of people with systolic >=140 AND systolic < 180 , Filter them out based on name, order it by timestamps and enumerate through each set, if I find a systolic >= 140 AND systolic < 180 , i break enumeration and check the next 2 readings of that name and take the average. I repeat that with diastolic. and Merge the results with all the three conditions, removing any duplicate entries.

this is what I currently do.

Yes, its possible to do in MySQL. I thinks its even possible to do it as a SELECT statement (but this would be very complex, difficult to maintain and probably very slow).

Since you're currently not even sure if you can do it in MySQL and are asking here, I suggest that implementing a solution in PHP might be a better use of your time - you could cut and paste code from an answer, which might appear to give the correct results, but would you be in a position to evaluate the nuances of the solution? Would you be able to fix it when it breaks? Upgrade it to accommodate new functionality?

If it were me, I would implement it as a finite state machine (actually 3 FSMs, one for each condition) using a MySQL procedure. You didn't say what you are supposed to do when the condition is met - but if you simply record the fact in the database, you can do whatever you like with a simple SQL statement. Another advantage of this approach is that you don't need to recalculate all the data every time you want to apply the conditions.

I'll describe the process for applying condition 2. All 3 conditions (and many more) could be implemented in a single pass of the data. The first could be done with a simple select.

1) select the data which has not been previously processed, ordered by name, then timestamp

in a loop:
   2.1) if the name is different from the last stored value, reset s1 and s2
   2.2) if (systolic  >= 140 AND systolic < 180)
      2.2.1) if s1 is set
          2.2.1.1 if s2 is set
               caclulate average if over limit, flag record, reset systolic1 and systolic2
          2.2.1.2 else
               set s2 to current systolic pressure
       2.2.2) else
           set s1 to current systolic pressure
    2.3) else
        reset s1 and s2
 repeat for all records

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM