PL / SQL在case语句中获取平均值

时间:2013-01-08 00:03:58

标签: oracle plsql sum case average

表:

+-----+----+----------+
| QTR | MO | PCT_PERF |
+-----+----+----------+
|   1 | 1  |       89 | 
|   1 | 2  |       73 |
|   1 | 3  |       95 |
+-----+----+----------+

这个的正确语法是什么?

CASE WHEN QTR=1 THEN ROUND(SUM(PCT_PERF WHERE MO IN (1,2,3))/3,2)
 WHEN QTR=2 THEN ROUND(SUM(PCT_PERF WHERE MO IN (4,5,6))/3,2) 
 WHEN QTR=3 THEN ROUND(SUM(PCT_PERF WHERE MO IN (7,8,9))/3,2)
 ELSE ROUND(SUM(PCT_PERF WHERE MO IN (10,11,12))/3,2) END QTR_PCT

因此,当QTR为1时,QTR_PCT应为86。 (89 + 73 + 95)/ 3 = 85.67

1 个答案:

答案 0 :(得分:1)

我相信Oracle的AVG(“平均”)功能可以解决您的问题。我没有Oracle数据库方便确认这一点:

SELECT QTR, ROUND(AVG(PCT_PERF), 2) AS QTR_PCT FROM ... GROUP BY QTR