我需要创建一个函数,该函数根据用户的输入转换单个列的值。我需要一些关于这样做的语法帮助。
这是我当前正在执行以获取行的查询:
SELECT payment_id, rental_id, amount FROM payment
关于我正在尝试做的一些伪代码:
function getReport(String currencyType){
if(currencyType == 'EUR'){
Multiply the values in the amounts column by 1.16 and append Euros to it
Return all the rows in the table
}else if(currencyType == 'RMB'){
Multiple the values in the amounts column by 6.44 and append RMB to it
Return all the rows in the table
}else{
Do nothing because the default column values are in USD
Return all the rows in the table
}
}
我一直在尝试创建一个,但我在语法上苦苦挣扎。
不工作:
CREATE OR REPLACE FUNCTION get_data(currency_type text) RETURNS TABLE payment_info AS $$
CASE currency_type
WHEN 'EUR' THEN
SELECT payment_id, rental_id, amount * 1.16 FROM payment;
WHEN 'RMB' THEN
SELECT payment_id, rental_id, amount * 6.44 FROM payment;
WHEN 'USD' THEN
SELECT payment_id, rental_id, amount FROM payment;
$$ LANGUAGE SQL;
有人可以帮我创建这个函数的语法吗?