我收到一条消息,说该表达式是无效的标识符。我也得到消息说单组by无效。请建议替代。
我有以下Oracle SQL语句:
SELECT SUM(CASE
WHEN PDRVARIANCE >= -1 then '1'
WHEN PDRVARIANCE <-1 then '0'
END)
/ count(*)
* 100 as PercentCompliance ,
TO_CHAR (ACTUALFINISHDATE,'MM/YY') AS DCSMONTH
FROM ACTIVITIES
WHERE DCSMONTH IS NOT NULL
GROUP BY DCSMONTH
现有数据将是这样的......
Variance Month
20 10/12
3.2 10/12
-4.2 10/12
-27.8 10/12
13 11/12
3 11/12
-1.2 12/12
-2 12/12
期望的结果是对每个方差条件和逐月进行“真实”与“假”的百分比并将其显示为......
Percent Month
50 10/12
100 11/12
0 12/12
答案 0 :(得分:0)
您不能在定义的相同范围内使用列别名。
您必须将GROUP BY DSCMONTH
替换为GROUP BY TO_CHAR (ACTUALFINISHDATE,'MM/YY')
(并对where子句执行相同操作),或者将查询包装在另一个查询中。
像,
select dcsmonth, sum(data) / count(*) * 100 percentcompliance
from (
SELECT CASE WHEN PDRVARIANCE >= -1 then 1
WHEN PDRVARIANCE <-1 then 0 END data,
TO_CHAR (ACTUALFINISHDATE,'MM/YY') AS DCSMONTH
FROM ACTIVITIES
WHERE ACTUALFINISHDATE IS NOT NULL
)
GROUP BY DCSMONTH