我有redshift脚本
这是脚本部分的代码
SELECT cog.organizationid,
cog.clientid,
t.MonthBilled,
t.Month,
t.Year,
u.ClientCharges,
cp.supervisorcontactid AS Principal,
cc.fullname AS CSL,
LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) AS PreviosMonthCharges,
CASE WHEN u.ClientCharges IS NULL THEN 0 ELSE u.ClientCharges END -
CASE WHEN LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL THEN 0
ELSE LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month)
END AS Delta,
CASE WHEN (CASE WHEN u.ClientCharges IS NULL THEN 0 ELSE u.ClientCharges END - CASE WHEN LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL THEN 0
ELSE LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) END) > 0 THEN 'Gain'
WHEN CASE WHEN u.ClientCharges IS NULL THEN 0 ELSE u.ClientCharges END - CASE WHEN LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL THEN 0
ELSE LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) END < 0 THEN 'Loss'
ELSE NULL END AS GainsLosses,
CASE WHEN u.ClientCharges >=0 AND LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL AND t.MonthBilled <> '1/1/2015' THEN 'New' ELSE 'Existing' END AS ClientType,
CASE WHEN u.ClientCharges IS NULL THEN 'InActive' ELSE 'Active' END AS ActiveStatus,
CASE WHEN LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NOT NULL AND t.MonthBilled <> '1/1/2015' THEN 1 ELSE NULL END AS ActiveLastMonth,
CASE WHEN u.ClientCharges IS NULL AND (LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) <> 0 AND LAG(u.ClientCharges,1,0) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NOT NULL) AND t.MonthBilled <> '1/1/2015' THEN 1 ELSE NULL END AS OneMonthChurn,
u2.FeatureKeyCount,
CASE WHEN u.ClientCharges IS NOT NULL AND u.ClientCharges > 0 THEN 1 ELSE 0 END AS OrgCounter
FROM public.contacts_client_organization cog
尝试运行该错误消息
[42601] [500310] Amazon无效操作:“ Delta”位置或附近的语法错误:747; java.lang.RuntimeException:com.amazon.support.exceptions.ErrorException:Amazon无效操作:“ Delta”处或附近的语法错误位置:747;
但是我不明白,怎么了?
更新
这里是用有效的 LAG 重写的零件,但是我仍然有同样的问题
SELECT cog.organizationid,
cog.clientid,
t.MonthBilled,
t.Month,
t.Year,
u.ClientCharges,
cp.supervisorcontactid AS Principal,
cc.fullname AS CSL,
LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) AS PreviosMonthCharges,
CASE WHEN u.ClientCharges IS NULL THEN 0 ELSE u.ClientCharges END -
CASE WHEN LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL THEN 0
ELSE LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month)
END AS Delta,
CASE WHEN (CASE WHEN u.ClientCharges IS NULL THEN 0 ELSE u.ClientCharges END - CASE WHEN LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL THEN 0
ELSE LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) END) > 0 THEN 'Gain'
WHEN CASE WHEN u.ClientCharges IS NULL THEN 0 ELSE u.ClientCharges END - CASE WHEN LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL THEN 0
ELSE LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) END < 0 THEN 'Loss'
ELSE NULL END AS GainsLosses,
CASE WHEN u.ClientCharges >=0 AND LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NULL AND t.MonthBilled <> '1/1/2015' THEN 'New' ELSE 'Existing' END AS ClientType,
CASE WHEN u.ClientCharges IS NULL THEN 'InActive' ELSE 'Active' END AS ActiveStatus,
CASE WHEN LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NOT NULL AND t.MonthBilled <> '1/1/2015' THEN 1 ELSE NULL END AS ActiveLastMonth,
CASE WHEN u.ClientCharges IS NULL AND (LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) <> 0 AND LAG(u.ClientCharges,1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid,t.Year,t.Month) IS NOT NULL) AND t.MonthBilled <> '1/1/2015' THEN 1 ELSE NULL END AS OneMonthChurn,
u2.FeatureKeyCount,
CASE WHEN u.ClientCharges IS NOT NULL AND u.ClientCharges > 0 THEN 1 ELSE 0 END AS OrgCounter
FROM public.contacts_client_organization cog
答案 0 :(得分:1)
我不确定这是否是问题所在,但是lag()
不接受Redshift中默认值的第三个参数。
我会这样写:
(COALESCE(u.ClientCharges, 0) -
COALESCE(LAG(u.ClientCharges, 1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid, t.Year, t.Month), 0)
) as my_Delta,
DELTA
是Amazon Redshift reserved word,所以给它起一个不同的名字。