oracle 枢轴和错误ORA-56901:不允许使用非常数表达式

i1icjdpr  于 2023-06-29  发布在  Oracle
关注(0)|答案(1)|浏览(462)
SELECT  *
FROM
(
 SELECT DATE_KEY,CELL_NAME, HOUR_KEY,HANDOVER_PREPARATION_RATE_EUCELL_ERIC_ 
FROM EDW.FCT_PRSE4G_CELL_KPI_H 

WHERE PROVINCE='KJ'
AND DATE_KEY BETWEEN TO_CHAR(TO_DATE(SYSDATE,'DD/MM/YY') -2,'YYYYMMDD') AND TO_CHAR(TO_DATE(SYSDATE,'DD/MM/YY') -1,'YYYYMMDD') 
AND HOUR_KEY BETWEEN 0AND 8
) 
PIVOT ( 
        SUM(HANDOVER_PREPARATION_RATE_EUCELL_ERIC_) FOR DATE_key IN ( TO_CHAR(TO_DATE(SYSDATE,'DD/MM/YY') -2,'YYYYMMDD') AS HAND_48, TO_CHAR(TO_DATE(SYSDATE,'DD/MM/YY') -1,'YYYYMMDD') AS HAND_24)
       
       )  PVT1

错误是关于date_key和IN cluas .我在IN()中使用了子查询,错误是:ORA-00936“select”缺少表达式

SELECT  *
FROM
(
 SELECT DATE_KEY,CELL_NAME, HOUR_KEY,HANDOVER_PREPARATION_RATE_EUCELL_ERIC_ 
FROM EDW.FCT_PRSE4G_CELL_KPI_H 

WHERE PROVINCE='KJ'
AND DATE_KEY BETWEEN TO_CHAR(TO_DATE(SYSDATE,'DD/MM/YY') -2,'YYYYMMDD') AND TO_CHAR(TO_DATE(SYSDATE,'DD/MM/YY') -1,'YYYYMMDD') 
AND HOUR_KEY BETWEEN 0AND 8
) 
PIVOT ( 
        SUM(HANDOVER_PREPARATION_RATE_EUCELL_ERIC_) FOR DATE_key IN ( select TO_CHAR(TO_DATE(SYSDATE,'DD/MM/YY') -2,'YYYYMMDD') AS HAND_48, TO_CHAR(TO_DATE(SYSDATE,'DD/MM/YY') -1,'YYYYMMDD') AS HAND_24 from dual)
       
       )  PVT1
wfypjpf4

wfypjpf41#

你不能像在PIVOT中那样使用动态键;但是,您可以找到今天(午夜)和您的date_key之间的天数差异,然后以该数字为中心:

SELECT  *
FROM    (
  SELECT DATE_KEY,
         CELL_NAME,
         HOUR_KEY,
         HANDOVER_PREPARATION_RATE_EUCELL_ERIC_,
         TRUNC(SYSDATE) - TO_DATE(DATE_KEY, 'YYYYMMDD') AS days
  FROM   EDW.FCT_PRSE4G_CELL_KPI_H 
  WHERE  PROVINCE='KJ'
  AND    DATE_KEY BETWEEN TO_CHAR(SYSDATE -2,'YYYYMMDD')
                      AND TO_CHAR(SYSDATE -1,'YYYYMMDD') 
  AND    HOUR_KEY BETWEEN 0 AND 8
) 
PIVOT ( 
  SUM(HANDOVER_PREPARATION_RATE_EUCELL_ERIC_)
  FOR days IN (2 AS HAND_48, 1 AS HAND_24)
) PVT1
  • 注1:将日期存储为字符串被认为是不好的做法;使用DATE数据类型。*
  • 注2:永远不要在已经是DATE的值上使用TO_DATE(例如SYSDATE)。在最好的情况下,它不会做任何事情,但在最坏的情况下,它会引发异常或生成意外的结果。

相关问题