Redshift 函数语法
Redshift function syntax
我研究了一遍又一遍,没有弄清楚哪里不对,我觉得这个函数的语法是正确的
我曾尝试从 plpgsql 中取出单引号,对逗号进行三次检查,将正文添加到美元符号,但没有成功,我不明白为什么我会出错。 returns table 应该是正确的语法吧?
CREATE OR REPLACE FUNCTION fn_ytd_costs_of_business (param1 date)
RETURNS TABLE (year INTEGER, month TEXT , revenue FLOAT8 ,transactiondate TIMESTAMP , Flag varchar(28),)
AS
$$
BEGIN
select
extract(year from transactiondate) as year
, to_char(transactiondate, 'Mon') as month
--, extract(month from transactiondate) as month_number
, sum(netamount) as revenue
,transactiondate
,Flag
from
vw_costs_of_businesss_copy a
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(transactiondate, 'YYYY-MM-DD') ->= concat(to_char(extract(year from '2019-01-01'), 'YYYY'),'01-01') --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(extract(year from to_date('2019-01-01', 'YYYY-MM-DD'))),'01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cas(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as text ),'-01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cast(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as Text),'0000'),'-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
where to_date(transactiondate, 'YYYY-MM-DD') <= and to_date(transactiondate, 'YYYY-MM-DD')>= to_date(concat(to_char(Cast(extract(year from to_date(, 'YYYY-MM-DD')-1) as Text),'0000'),'-01') , 'YYYY-MM-DD')
group by
year
, month
,transactiondate
, Flag;
on a.
--order by year;
END;
$$
LANGUAGE plpgsql;
这是我收到的错误:
RETURNS TABLE (year INTEGER, month TEXT , revenue FLOAT8 ,transactiondate TIMESTAMP , Flag varchar(28),)
错误可能是因为 TABLE
不是有效的 RETURN 类型。
但是,这并不重要,因为 Amazon Redshift User-Defined 函数不允许 SELECT
来自 tables 的数据。它只能对传递给函数的数据进行操作。
参见:UDF Constraints - Amazon Redshift
根据您的 use-case,Stored Procedure 可能是 suitable。它不会"return"一个table,但它可以CREATE INTO
一个table。
我研究了一遍又一遍,没有弄清楚哪里不对,我觉得这个函数的语法是正确的
我曾尝试从 plpgsql 中取出单引号,对逗号进行三次检查,将正文添加到美元符号,但没有成功,我不明白为什么我会出错。 returns table 应该是正确的语法吧?
CREATE OR REPLACE FUNCTION fn_ytd_costs_of_business (param1 date)
RETURNS TABLE (year INTEGER, month TEXT , revenue FLOAT8 ,transactiondate TIMESTAMP , Flag varchar(28),)
AS
$$
BEGIN
select
extract(year from transactiondate) as year
, to_char(transactiondate, 'Mon') as month
--, extract(month from transactiondate) as month_number
, sum(netamount) as revenue
,transactiondate
,Flag
from
vw_costs_of_businesss_copy a
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(transactiondate, 'YYYY-MM-DD') ->= concat(to_char(extract(year from '2019-01-01'), 'YYYY'),'01-01') --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(extract(year from to_date('2019-01-01', 'YYYY-MM-DD'))),'01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cas(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as text ),'-01-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
--where to_date(transactiondate, 'YYYY-MM-DD') <= '2019-06-07' and to_date(concat(to_char(Cast(extract(year from to_date('2019-01-01', 'YYYY-MM-DD')) as Text),'0000'),'-01') , 'YYYY-MM-DD') >= '2019-01-01' --Convert the date param to year and concatenate with '01/01'
where to_date(transactiondate, 'YYYY-MM-DD') <= and to_date(transactiondate, 'YYYY-MM-DD')>= to_date(concat(to_char(Cast(extract(year from to_date(, 'YYYY-MM-DD')-1) as Text),'0000'),'-01') , 'YYYY-MM-DD')
group by
year
, month
,transactiondate
, Flag;
on a.
--order by year;
END;
$$
LANGUAGE plpgsql;
这是我收到的错误:
RETURNS TABLE (year INTEGER, month TEXT , revenue FLOAT8 ,transactiondate TIMESTAMP , Flag varchar(28),)
错误可能是因为 TABLE
不是有效的 RETURN 类型。
但是,这并不重要,因为 Amazon Redshift User-Defined 函数不允许 SELECT
来自 tables 的数据。它只能对传递给函数的数据进行操作。
参见:UDF Constraints - Amazon Redshift
根据您的 use-case,Stored Procedure 可能是 suitable。它不会"return"一个table,但它可以CREATE INTO
一个table。