SQL功能到货币转换
我正在尝试创建一个可以根据表进行货币转换的函数。该表称为ds_exchange_rate,看起来像这样:
您可以看到基本列包含只有USD和货币可以是BRL或MXN,而速率是该日期从USD到BRL/MXN的转换值。
为了使我的查询更简单,我尝试创建一个可以处理转换的函数。函数陈述看起来像这样:
CREATE OR REPLACE FUNCTION CONVERT_FROM_TO(
value NUMERIC,
from_currency TEXT,
to_currency TEXT,
reference_date DATE DEFAULT CURRENT_DATE
)
RETURN NUMERIC
LANGUAGE plpgsql
AS
$$
DECLARE
conversion_rate NUMERIC;
BEGIN
IF from_currency = 'USD' THEN
SELECT rate
INTO rate
FROM ds_exchange_rate
WHERE currency = to_currency
AND date = reference_date;
RETURN value * conversion_rate;
ELSIF to_currency = 'USD' THEN
SELECT 1/rate
INTO rate
FROM ds_exchange_rate
WHERE currency = from_currency
AND date = reference_date;
RETURN value * conversion_rate;
END IF;
END;
$$;
我只想要USD< - > BRL/MXN转换,因此不必担心BRL< - > mxn。
尝试创建函数时,我在第一个返回或ELSIF附近获得了一些语法误差。我相信这是“;”的东西,但是我无法修复它(我不使用用于创建SQL功能)。
如果有人能提供帮助,我会很感激!
错误信息
SQL Error [42601]: ERROR: syntax error at or near "ELSIF"
Position: 1
I'm trying to create a function that will do currency conversion based on a table. The table is called ds_exchange_rate and looks like this:
As you can see the base column contains only USD and currency can be either BRL or MXN, whereas rate is the conversion value from USD to BRL/MXN for that given date.
With the objective of making my queries simpler I tried to create a function that would handle the conversions. The function statment looks like this:
CREATE OR REPLACE FUNCTION CONVERT_FROM_TO(
value NUMERIC,
from_currency TEXT,
to_currency TEXT,
reference_date DATE DEFAULT CURRENT_DATE
)
RETURN NUMERIC
LANGUAGE plpgsql
AS
$
DECLARE
conversion_rate NUMERIC;
BEGIN
IF from_currency = 'USD' THEN
SELECT rate
INTO rate
FROM ds_exchange_rate
WHERE currency = to_currency
AND date = reference_date;
RETURN value * conversion_rate;
ELSIF to_currency = 'USD' THEN
SELECT 1/rate
INTO rate
FROM ds_exchange_rate
WHERE currency = from_currency
AND date = reference_date;
RETURN value * conversion_rate;
END IF;
END;
$;
I only want USD <-> BRL/MXN conversions so don't worry about BRL <-> MXN.
When trying to create the function I got some syntax erros at or near the first return and the elsif. I believe it's something with the ";", but I wasn't able to fix it (I'm not used to create SQL functions).
If someone could help out I'd appreciate a lot!
Error Message
SQL Error [42601]: ERROR: syntax error at or near "ELSIF"
Position: 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一些错误,返回功能的保留词是“返回”
另外,您的选择应返回您的conversion_rate变量,因此看起来像这样:
There are some errors, the reserved word for returning function is "returns"
also your selects should return into your conversion_rate variable, so corrected looks like this:
您可以将其简化为单个语句,删除对“ USD”转换的依赖性并创建SQL函数。
这确实取决于只有一组
base
和货币
一组一天的值。即任何日期,您都可以拥有(基本,货币)=('usd','mxn')或('mxn','usd'),但是您不想要这两个。可以通过创建订购基础货币的唯一索引来处理这。参见
You can reduce this to a single statement, remove the dependency on the conversion of 'USD' and create a sql function.
This does depend on having only a single set of
base
andcurrency
values for a single day. Ie for any date you could have either (base, currency) = ('USD','MXN') or ('MXN','USD'), however you would not want both. This can be handled by creating a unique index that orders base, currency.See demo.