我有用于redshift的sql脚本
这是代码的一部分
SELECT clo.name AS SalesManager,
prospect.id AS ProspectId,
prospect.fullname AS ProspectName,
prospect.company,
Prospect.Email,
prospect.phonehome,
prospect.phonecell,
prospect.phonework,
prospect.phoneworkext,
pct.PercentClosing,
pct.PercentClosingLearn,
pct.PercentClosingLMS,
COALESCE(CAST(paying.Paying AS VARCHAR), 'NO') AS Paying,
COALESCE(CAST(paying.Paying AS VARCHAR), 'NO') AS PayingLearn,
--CONVERT(DATE, metaData.ClosingDatePM) AS CloseDatePM,
CAST(metaData.ClosingDatePM AS DATE) AS CloseDatePM,
DATE_PART(YEAR, metaData.ClosingDatePM) AS CloseYear,
DATE_PART(MONTH, metaData.ClosingDatePM) AS CloseMonth,
CASE WHEN DATE_PART('MONTH', metaData.ClosingDatePM)||'/1/'||DATE_PART('YEAR', metaData.ClosingDatePM) = '/1/' THEN NULL ELSE DATE_PART('MONTH', metaData.ClosingDatePM)||'/1/'||DATE_PART(YEAR, metaData.ClosingDatePM) END AS MonthClose,
metaData.*
当我尝试运行脚本时,出现错误。
这一个
[42883] [500310] Amazon无效的操作:函数pg_catalog.pgdate_part(“未知”,文本)不存在; java.lang.RuntimeException:com.amazon.support.exceptions.ErrorException:Amazon无效操作:函数pg_catalog.pgdate_part(“未知”,文本)不存在;
但是,如果我评论这些行
DATE_PART(YEAR, metaData.ClosingDatePM) AS CloseYear,
DATE_PART(MONTH, metaData.ClosingDatePM) AS CloseMonth,
CASE WHEN DATE_PART('MONTH', metaData.ClosingDatePM)||'/1/'||DATE_PART('YEAR', metaData.ClosingDatePM) = '/1/' THEN NULL ELSE DATE_PART('MONTH', metaData.ClosingDatePM)||'/1/'||DATE_PART(YEAR, metaData.ClosingDatePM) END AS MonthClose,
一切正常。
样本表数据(我只添加了一列,因为问题,我认为与之相关):
closingdatepm
-------------
9/16/2013
12/01/2017
3/1/2014
5/1/2015
01/01/2018
4/1/2014
8/1/2014
10/31/2017
01/01/2018
5/1/2015
哪里有麻烦?
答案 0 :(得分:0)
所以。我想我找到了答案,如何解决
就这样使用它
CAST(metaData.ClosingDatePM AS DATE) AS CloseDatePM,
DATE_PART(YEAR, CloseDatePM) AS CloseYear,
其他类似的
答案 1 :(得分:0)
这是因为date_part仅适用于列的日期类型。 以下是您的答案:
SELECT clo.name AS SalesManager,
prospect.id AS ProspectId,
prospect.fullname AS ProspectName,
prospect.company,
Prospect.Email,
prospect.phonehome,
prospect.phonecell,
prospect.phonework,
prospect.phoneworkext,
pct.PercentClosing,
pct.PercentClosingLearn,
pct.PercentClosingLMS,
COALESCE(CAST(paying.Paying AS VARCHAR), 'NO') AS Paying,
COALESCE(CAST(paying.Paying AS VARCHAR), 'NO') AS PayingLearn,
--CONVERT(DATE, metaData.ClosingDatePM) AS CloseDatePM,
CAST(metaData.ClosingDatePM AS DATE) AS CloseDatePM,
DATE_PART(YEAR, cast(metaData.ClosingDatePM) as date) AS CloseYear,
DATE_PART(MONTH, cast(metaData.ClosingDatePM) as date) AS CloseMonth,
CASE WHEN DATE_PART('MONTH', cast(metaData.ClosingDatePM) as date)||'/1/'||DATE_PART('YEAR', cast(metaData.ClosingDatePM) as date) = '/1/' THEN NULL ELSE DATE_PART('MONTH', cast(metaData.ClosingDatePM) as date)||'/1/'||DATE_PART(YEAR, cast(metaData.ClosingDatePM) as date) END AS MonthClose,
metaData.*