【问题标题】:Invalid operation: Default parameter not be supported for window function lag;无效操作:窗口函数滞后不支持默认参数;
【发布时间】:2019-02-10 12:12:33
【问题描述】:

我有 MSSQL Server 脚本,需要重写为 Redshift

这是脚本的原始部分

    SELECT  cog.OrganizationId,
        cog.ClientId,
        REPLACE(REPLACE(AM, 'IAM: ', 'IMP: '),'AM: ', '') AS AM,
        u.ProcedureCodeString,
        t.MonthBilled,
        t.Month,
        t.Year,
        u.ClientCharges,
        LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) AS PreviosMonthCharges,
        LAG(u.ClientCharges,2,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) AS TwoMonthsAgoCharges,
        LAG(u.ClientCharges,3,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) AS ThreeMonthsAgoCharges,
        LAG(u.ClientCharges,4,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) AS FourMonthsAgoCharges,
        LAG(t.MonthBilled,3,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) AS ThreeMonthsAgoDate,
        DATEDIFF(d,CASE WHEN LAG(CONVERT(datetime,t.MonthBilled),3,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) = 0 THEN NULL ELSE LAG(CONVERT(datetime,t.MonthBilled),3,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) END,CONVERT(datetime,t.MonthBilled) ) AS DateDifCheck,
        b.FirstMonth,
        b.FirstFeature,
        CASE WHEN u.ClientCharges > 0
                AND LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) > 0
                AND LAG(u.ClientCharges,2,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) > 0
                AND LAG(u.ClientCharges,3,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) > 0
                AND LAG(u.ClientCharges,4,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) = 0
                AND LAG(t.MonthBilled,3,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) > b.FirstMonth
                AND DATEDIFF(d,CASE WHEN LAG(CONVERT(datetime,t.MonthBilled),3,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) = 0 THEN NULL ELSE LAG(CONVERT(datetime,t.MonthBilled),3,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) END,CONVERT(datetime,t.MonthBilled) ) < 95
                AND DATEDIFF(d,CASE WHEN LAG(CONVERT(datetime,t.MonthBilled),3,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) = 0 THEN NULL ELSE LAG(CONVERT(datetime,t.MonthBilled),3,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.ClientId,t.Year,t.Month) END,CONVERT(datetime,t.MonthBilled) ) > 88
                AND u.ProcedureCodeString <> b.FirstFeature
                THEN 1 ELSE 0 END AS CommissionReportFlag

FROM    dbo.Contacts_ClientOrganization cog WITH(NOLOCK)

这就是我重写它的方式

    SELECT  cog.organizationid,
        cog.clientid,
        REPLACE(REPLACE(AM, 'IAM: ', 'IMP: '),'AM: ', '') AS AM,
        u.ProcedureCodeString,
        t.MonthBilled,
        t.Month,
        t.Year,
        u.ClientCharges,
        LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) AS PreviosMonthCharges,
        LAG(u.ClientCharges,2,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) AS TwoMonthsAgoCharges,
        LAG(u.ClientCharges,3,0) OVER (PARTITION BY cog.ClientId, u.ProcedureCodeString ORDER BY cog.сlientid,t.Year,t.Month) AS ThreeMonthsAgoCharges,
        LAG(u.ClientCharges,4,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.сlientid,t.Year,t.Month) AS FourMonthsAgoCharges,
        LAG(t.MonthBilled,3,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) AS ThreeMonthsAgoDate,
        DATEDIFF(d,CASE WHEN LAG(CONVERT(TIMESTAMP,t.MonthBilled),3,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) = 0 THEN NULL ELSE LAG(CONVERT(TIMESTAMP,t.MonthBilled),3,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) END,CONVERT(TIMESTAMP,t.MonthBilled) ) AS DateDifCheck,
        b.FirstMonth,
        b.FirstFeature,
        CASE WHEN u.ClientCharges > 0
                AND LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) > 0
                AND LAG(u.ClientCharges,2,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) > 0
                AND LAG(u.ClientCharges,3,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) > 0
                AND LAG(u.ClientCharges,4,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) = 0
                AND LAG(t.MonthBilled,3,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) > b.FirstMonth
                AND DATEDIFF(d,CASE WHEN LAG(CONVERT(TIMESTAMP,t.MonthBilled),3,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) = 0 THEN NULL ELSE LAG(CONVERT(TIMESTAMP,t.MonthBilled),3,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) END,CONVERT(TIMESTAMP,t.MonthBilled) ) < 95
                AND DATEDIFF(d,CASE WHEN LAG(CONVERT(TIMESTAMP,t.MonthBilled),3,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) = 0 THEN NULL ELSE LAG(CONVERT(TIMESTAMP,t.MonthBilled),3,0) OVER (PARTITION BY cog.clientid, u.ProcedureCodeString ORDER BY cog.clientid,t.Year,t.Month) END,CONVERT(TIMESTAMP,t.MonthBilled) ) > 88
                AND u.ProcedureCodeString <> b.FirstFeature
                THEN 1 ELSE 0 END AS CommissionReportFlag

FROM    public.contacts_client_organization cog 

但我现在收到此错误

[42601][500310] 亚马逊无效操作:窗口函数延迟不支持默认参数; java.lang.RuntimeException: com.amazon.support.exceptions.ErrorException: Amazon Invalid operation: 默认参数不支持窗口函数滞后;

我该如何解决这个问题?

【问题讨论】:

    标签: sql amazon-redshift window-functions


    【解决方案1】:

    我在官方文档中找到了这个问题的答案

      LAG (value_expr [, offset ])
    [ IGNORE NULLS | RESPECT NULLS ]
    OVER ( [ PARTITION BY window_partition ] ORDER BY window_ordering )
    

    我有默认值,但是 reshift 说我只需要偏移量,所以只需删除默认值,就可以了

    【讨论】:

      猜你喜欢
      • 2017-04-30
      • 2021-01-21
      • 2018-09-16
      • 1970-01-01
      • 2013-01-17
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      相关资源
      最近更新 更多