无效操作:“Delta”处或附近的语法错误
Posted
技术标签:
【中文标题】无效操作:“Delta”处或附近的语法错误【英文标题】:Invalid operation: syntax error at or near "Delta" 【发布时间】:2018-09-06 11:50:39 【问题描述】:我有红移脚本
这是部分脚本的代码
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] 亚马逊无效操作:“Delta”位置或附近的语法错误:747; java.lang.RuntimeException:com.amazon.support.exceptions.ErrorException:亚马逊无效操作:“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
【问题讨论】:
这是一个猜测,但我会尝试添加括号:((CASE ... END) - (CASE ... END)) as Delta
。不应该是这样。但也许它很困惑。无论哪种方式,COALESCE
在检查 NULL
时会更好。
【参考方案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,所以给它一个不同的名字。
【讨论】:
什么是 COALESCE?我删除了 3 个参数,仍然有这个错误 是NVL()
的ANSI/ISO标准版本:docs.aws.amazon.com/redshift/latest/dg/r_NVL_function.html。
我认为您在这一行,0
附近有错误COALESCE(CASE WHEN LAG(u.ClientCharges, 1) OVER (PARTITION BY cog.clientid ORDER BY cog.clientid, t.Year, t.Month), 0)
@EugeneSukh 。 . .表达式中根本不应该有CASE
。
是的,但它仍然向我显示此错误。嗯。我的意思是“Delta”附近的语法错误以上是关于无效操作:“Delta”处或附近的语法错误的主要内容,如果未能解决你的问题,请参考以下文章
Redshift 存储过程 - [Amazon](500310) 无效操作:“$1”处或附近的语法错误;
调用 o79.getDynamicFrame 时出错。 [Amazon](500310) 无效操作:“s_next_of_kin”处或附近的语法错误