【发布时间】: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