0

如何更改下面的脚本,以便我可以对 QTR_PCT 给出的值进行 GROUP BY YR、QTR、CODE 以获得所需的输出?

CREATE VIEW REPORTS.MY_VIEW
(YR, QTR, QTR_PCT, CODE)
AS 
SELECT YR, QTR
, CASE WHEN PCTPERF>=85 THEN ROUND(AVG(PCTPERF)) 
  ELSE NULL
  END QTR_PCT
, CODE
FROM ANOTHER_VIEW
GROUP BY YR, QTR, PCTPERF, CODE
ORDER BY CODE ASC;

实际输出:(我没有包括 QTR_PCT 列)

+------+-----+------+
|   YR | QTR | CODE |
+------+-----+------+
| 2012 |   1 | 1039 | 
| 2012 |   1 | 1039 | 
| 2012 |   1 | 1039 |
| 2012 |   2 | 1039 | 
| 2012 |   2 | 1039 | 
| 2012 |   2 | 1039 |  
+------+-----+------+

期望的输出:(我没有包括列 QTR_PCT)

+------+-----+------+
|   YR | QTR | CODE |
+------+-----+------+
| 2012 |   1 | 1039 | 
| 2012 |   2 | 1039 | 
+------+-----+------+
4

1 回答 1

1

我不确定我是否理解您的问题,但也许这会有所帮助,请尝试:

SELECT YR, qtr, code,max(qtr_pct)  aa
FROM (
SELECT YR, qtr, code, pctref,  CASE WHEN pctref >= 85 THEN round(avg(pctref) over (partition BY YEAR, qtr, code))
  ELSE NULL END qtr_pct
FROM ANOTHER_VIEW) t
GROUP BY YR, qtr, code;

这是一个 sqlfiddle 演示

于 2013-01-09T06:11:35.840 回答