将MySQL函数转换为Redshift

问题描述 投票:0回答:1

我刚刚开始学习Redshift,我正在尝试将MySQL服务器上使用的函数转换为Redshift。到目前为止我还没有运气,希望stackoverflow向导可以提供方向。

CREATE FUNCTION SS_Ans_to_Num(answer VARCHAR(1000))
 RETURNS INT
 BEGIN
declare answer_num int(11);

if answer = 'No' then SET answer_num = 0;
elseif answer IN ('N/A – I did not receive this training', 'N/C - No recibí esta capacitación') then SET answer_num = 0;
elseif answer IN ('Strongly Disagree', 'Totalmente en desacuerdo') then SET answer_num = 1;
elseif answer IN ('Poor','Mala') then SET answer_num = 1;
elseif answer IN ('Disagree','En desacuerdo') then SET answer_num = 2;
elseif answer IN ('Fair','Regular') then SET answer_num = 2;
elseif answer IN ('Neutral','Neutro') then SET answer_num = 3;
elseif answer IN ('Agree','De acuerdo') then SET answer_num = 4;
elseif answer IN ('Very Good','Muy buena') then SET answer_num = 4;
elseif answer IN ('Yes','Sí') then SET answer_num = 5;
elseif answer IN ('Strongly Agree','Totalmente de acuerdo') then SET answer_num = 5;
elseif answer IN ('Excellent','Excelente') then SET answer_num = 5;
else SET answer_num = 0;
end if;

return (answer_num);

END;

我认为沿着这些方向的东西会起作用,但它不会:

CREATE FUNCTION SS_Ans_to_Num(answer VARCHAR(1000))
 RETURNS INTEGER
 STABLE
 AS
  $$
   CASE
    WHEN answer = 'No' THEN SET answer_num = 0;
    WHEN answer IN ('N/A – I did not receive this training', 'N/C - No recibí esta capacitación') then SET answer_num = 0;
    WHEN answer IN ('Strongly Disagree', 'Totalmente en desacuerdo') then SET answer_num = 1;
    WHEN answer IN ('Poor','Mala') then SET answer_num = 1;
    WHEN answer IN ('Disagree','En desacuerdo') then SET answer_num = 2;
    WHEN answer IN ('Fair','Regular') then SET answer_num = 2;
    WHEN answer IN ('Neutral','Neutro') then SET answer_num = 3;
    WHEN answer IN ('Agree','De acuerdo') then SET answer_num = 4;
    WHEN answer IN ('Very Good','Muy buena') then SET answer_num = 4;
    WHEN answer IN ('Yes','Sí') then SET answer_num = 5;
    WHEN answer IN ('Strongly Agree','Totalmente de acuerdo') then SET answer_num = 5;
    WHEN answer IN ('Excellent','Excelente') then SET answer_num = 5;
    ELSE SET answer_num = 0;
  END CASE;

RETURN (answer_num);

$$ LANGUAGE SQL
amazon-web-services amazon-redshift
1个回答
2
投票

正确的语法是:

CREATE OR REPLACE FUNCTION SS_Ans_to_Num(TEXT)
 RETURNS INTEGER
 STABLE
 AS
  $$
   SELECT CASE
    WHEN $1 = 'No' THEN 0
    WHEN $1 IN ('N/A – I did not receive this training', 'N/C - No recibí esta capacitación') THEN 0
    WHEN $1 IN ('Strongly Disagree', 'Totalmente en desacuerdo') THEN 1
    WHEN $1 IN ('Poor','Mala') THEN 1
    WHEN $1 IN ('Disagree','En desacuerdo') THEN 2
    WHEN $1 IN ('Fair','Regular') THEN 2
    WHEN $1 IN ('Neutral','Neutro') THEN 3
    WHEN $1 IN ('Agree','De acuerdo') THEN 4
    WHEN $1 IN ('Very Good','Muy buena') THEN 4
    WHEN $1 IN ('Yes','Sí') THEN 5
    WHEN $1 IN ('Strongly Agree','Totalmente de acuerdo') THEN 5
    WHEN $1 IN ('Excellent','Excelente') THEN 5
    ELSE 0
   END

  $$ LANGUAGE SQL

语法类似于SQL语句而不是过程语言。如果您想使用更程序化的语言,请使用language plpythonu而不是language SQL

请参阅:CREATE FUNCTION文档

© www.soinside.com 2019 - 2024. All rights reserved.