简体   繁体   中英

Converting MySQL function to Redshift

I'm just starting to learn Redshift and I'm trying to convert a function used on a MySQL server to work in Redshift. I haven't had any luck so far, hoping the stackoverflow wizards can provide direction.

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;

I thought something along these lines would work, but it doesn't:

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

The correct syntax would be:

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

The syntax is similar to an SQL statement rather than a procedural language. If you wish to use a more programmatic language, use language plpythonu instead of language SQL .

See: CREATE FUNCTION documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM