oracle sql查找三次尝试的最大平均值

时间:2017-04-13 15:02:53

标签: sql oracle oracle-sqldeveloper

嘿,需要帮助才能找到三个柱子的最大平均重量(尝试1,尝试2和尝试3,并显示它们属于哪个运动以及它们发生的日期。

Atm,以下查询输出的结果,所有练习,它们发生的所有日期以及所有平均权重。我希望它选择最大平均重量,它所属的运动和它发生的日期。附件是查询的结果,我突出了我想要的输出。 Result of query

 SELECT e.exercise_description
 , occ.occ_date, (COALESCE(OE.ATTEMPT1, 0)
 + COALESCE(OE.ATTEMPT2, 0)
  + COALESCE(OE.ATTEMPT3, 0))
   /(3 -(COALESCE(OE.ATTEMPT1 - OE.ATTEMPT1, 1)
    + COALESCE(OE.ATTEMPT2 - OE.ATTEMPT2, 1)
     + COALESCE(OE.ATTEMPT3 - OE.ATTEMPT3, 1))
  ) AS row_avg
   FROM EXERCISE E INNER JOIN Occurrence_Exercise OE
  ON E.EXERCISEID=OE.EXERCISEID
    INNER JOIN OCCURRENCE OCC ON OE.OCCURRENCEID=OCC.OCCURRENCEID;

1 个答案:

答案 0 :(得分:0)

你可以使用window row_number()函数来获得最大的平均值

SELECT e.exercise_description
     , occ.occ_date
  FROM 
      (SELECT oe.exerciseid
            , oe.occurrenceid
            , ROW_NUMBER() OVER(ORDER BY (COALESCE(oe.attempt1, 0) + COALESCE(oe.attempt2, 0)+ COALESCE(oe.attempt3, 0))
                                          / (NVL2(oe.attempt1, 1, 0) + NVL2(oe.attempt2, 1, 0) + NVL2(oe.attempt3, 1, 0))
                               ) AS rn
       FROM Occurrence_Exercise oe 
      ) srt
 INNER JOIN exercise e 
    ON srt.exerciseid=e.exerciseid
 INNER JOIN occurrence occ 
    ON srt.occurrenceid=occ.occurrenceid
 WHERE srt.rn = 1