SQL功能到货币转换

发布于 2025-02-12 09:32:24 字数 1493 浏览 1 评论 0原文

我正在尝试创建一个可以根据表进行货币转换的函数。该表称为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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

又怨 2025-02-19 09:32:24

有一些错误,返回功能的保留词是“返回”
另外,您的选择应返回您的conversion_rate变量,因此看起来像这样:

CREATE OR REPLACE FUNCTION CONVERT_FROM_TO(
    value NUMERIC, 
    from_currency TEXT, 
    to_currency TEXT, 
    reference_date DATE DEFAULT CURRENT_DATE
)
    returns NUMERIC
    LANGUAGE plpgsql
    AS
    $
    DECLARE
        conversion_rate NUMERIC;
    BEGIN
        IF from_currency = 'USD' THEN 
            SELECT rate
            INTO conversion_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 conversion_rate
            FROM ds_exchange_rate 
            WHERE currency = from_currency
            AND date = reference_date;        
            RETURN value * conversion_rate;
        END IF;
    END;
    $;

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:

CREATE OR REPLACE FUNCTION CONVERT_FROM_TO(
    value NUMERIC, 
    from_currency TEXT, 
    to_currency TEXT, 
    reference_date DATE DEFAULT CURRENT_DATE
)
    returns NUMERIC
    LANGUAGE plpgsql
    AS
    $
    DECLARE
        conversion_rate NUMERIC;
    BEGIN
        IF from_currency = 'USD' THEN 
            SELECT rate
            INTO conversion_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 conversion_rate
            FROM ds_exchange_rate 
            WHERE currency = from_currency
            AND date = reference_date;        
            RETURN value * conversion_rate;
        END IF;
    END;
    $;
天煞孤星 2025-02-19 09:32:24

您可以将其简化为单个语句,删除对“ USD”转换的依赖性并创建SQL函数。

create or replace function convert_rate(
    value numeric, 
    from_currency text, 
    to_currency text, 
    reference_date date default current_date
    )
  returns numeric
 language sql
as $
 select case when (base, currency) = (from_currency, to_currency) 
             then round(value * rate,6)
             
             when (base, currency) = (to_currency, from_currency) 
             then round(value / rate,6)
             
             else null 
        end
   from ds_exchange_rate 
  where (base, currency) in ( (from_currency, to_currency), (to_currency, from_currency) )
    and ex_date = reference_date;
$;

这确实取决于只有一组base货币一组一天的值。即任何日期,您都可以拥有(基本,货币)=('usd','mxn')或('mxn','usd'),但是您不想要这两个。可以通过创建订购基础货币的唯一索引来处理这。

create unique index one_way_daily_conversion 
on ds_exchange_rate (date,least(base,currency), greatest(currency,base));

参见

You can reduce this to a single statement, remove the dependency on the conversion of 'USD' and create a sql function.

create or replace function convert_rate(
    value numeric, 
    from_currency text, 
    to_currency text, 
    reference_date date default current_date
    )
  returns numeric
 language sql
as $
 select case when (base, currency) = (from_currency, to_currency) 
             then round(value * rate,6)
             
             when (base, currency) = (to_currency, from_currency) 
             then round(value / rate,6)
             
             else null 
        end
   from ds_exchange_rate 
  where (base, currency) in ( (from_currency, to_currency), (to_currency, from_currency) )
    and ex_date = reference_date;
$;

This does depend on having only a single set of base and currency 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.

create unique index one_way_daily_conversion 
on ds_exchange_rate (date,least(base,currency), greatest(currency,base));

See demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文