实例声明中的SQL滞后

时间:2018-06-22 09:40:27

标签: sql oracle lag window-functions

如果当前CLUSTERn =上一个CLUSTERn,然后将上一个PRODCAT作为PREVCAT添加到当前行,我将不胜感激case语句出了什么问题。

ORA-30484: missing window specification for this function
30484. 00000 -  "missing window specification for this function"
*Cause:    All window functions should be followed by window specification,
           like <function>(<argument list>) OVER (<window specification>)
*Action:
Error at Line: 11 Column: 30
SELECT  CLUSTERn,
        MEMBERn,
        COUNT(*) OVER ( PARTITION BY CLUSTERn ORDER BY MEMBERn, PRODCAT, STARTd, ENDd ) AS NEWRANK,
        CASE WHEN CLUSTERn = LAG(CLUSTERn) THEN LAG(PRODCAT) ELSE 'New' END AS PREVCAT,
        STATUS,
        PRODCAT,
        JOINTYPE,
        JOINRANK,
        CSP,
        PROGID,
        PROMNAME,
        PROMOID,
        COHORT,
        FWEEK,  
        STARTd,
        ENDd,
        SOURCE
FROM(

1 个答案:

答案 0 :(得分:6)

我不确定这是什么困惑。你有:

(CASE WHEN CLUSTERn = LAG(CLUSTERn)
      THEN LAG(PRODCAT)
      ELSE 'New'
 END) AS PREVCAT,

您缺少OVER子句-对于所有窗口函数来说都是非常基本的。

没有示例数据,很难确定您真正想要的是什么。也许:

(CASE WHEN CLUSTERn = LAG(CLUSTERn) OVER (ORDER BY MEMBERn, PRODCAT, STARTd, ENDd)
      THEN LAG(PRODCAT) OVER (ORDER BY MEMBERn, PRODCAT, STARTd, ENDd)
      ELSE 'New'
 END) AS PREVCAT,

也可能不需要CASELAG()具有三个参数的形式,可让您指定默认值:

LAG(PRODCAT, 1, 'NEW') OVER (PARTITION BY ClusterN ORDER BY STARTd, ENDd)