【问题标题】:function call within a function return type mismatch postgres函数中的函数调用返回类型不匹配 postgres
【发布时间】:2014-08-20 16:20:22
【问题描述】:

我已经对此进行了尽可能多的研究,我在这里查看了大约 20 个帖子,还有更多关于 Google 的帖子。似乎没有人回答我的问题。所以这里是: 我有一个函数需要来自cc_getbalancesfordate(theDate date) 的数据,它返回如下表:

货币余额

1.25 欧元

0.98 美元 ....

货币是字符,余额是数字。此函数返回当前表中的所有费率,如 33 行。

我试图这样调用这个函数:

SELECT  currency , balance, cc_ratecalculation(theDate,balance,currency) 
FROM cc_getbalancesfordate(theDate);

它的输出是相同的,但是它将余额乘以另一个表中的比率。 这两个功能独立工作。如果我在诸如

之类的函数中这样称呼它
CREATE OR REPLACE FUNCTION cc_getbalancesfordatewitheurs(theDate date) 
RETURNS TABLE(currency char, balance numeric(20)) AS $$

SELECT currency , balance, cc_ratecalculation(theDate,balance,currency) 
FROM cc_getbalancesfordate(theDate) ORDER BY currency;

$$ LANGUAGE SQL;

SELECT * 
FROM cc_getbalancesfordatewitheurs('2014-02-15'::date);

我得到了错误:

ERROR:  return type mismatch in function declared to return record
DETAIL:  Final statement returns too many columns.
CONTEXT:  SQL function "cc_getbalancesfordatewitheurs"
********** Error **********

ERROR: return type mismatch in function declared to return record
SQL state: 42P13
Detail: Final statement returns too many columns.
Context: SQL function "cc_getbalancesfordatewitheurs"

所以我尝试添加第三列,但它抱怨最后一列返回了一条记录。我不知道如何解决这个问题。

基本上我希望它返回数据,所以它是货币和余额两列。

这是另一个函数的代码。

CREATE OR REPLACE FUNCTION cc_ratecalculation(
    thedate date, balance numeric(15), fromcurrency char
) 
RETURNS TABLE(Curency char, Balance numeric(20)) AS $$

SELECT * 
FROM (
    SELECT src as Exchange, rate*balance
    FROM cc_rates
    WHERE date=thedate AND src='KRA' AND "from"=fromcurrency AND "to"='BTC' 
    UNION
    SELECT src as Exchange, rate*balance
    FROM cc_rates
    WHERE date=thedate AND src='BTE' AND "from"=fromcurrency AND "to"='BTC'
    UNION
    SELECT src as Exchange, rate*balance
    FROM cc_rates
    WHERE date=thedate AND src='CRY' AND "from"=fromcurrency AND "to"='BTC'
) a;

$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION cc_getbalancesfordate(theDate date) 
RETURNS TABLE(currency char, balance numeric(20)) AS $$

SELECT currency,sum(amount) 
FROM cc_getbalancesfordate_withexchange(theDate) GROUP BY currency ;

$$ LANGUAGE SQL;

【问题讨论】:

  • 为了更清楚一点,计算函数需要来自 cc_getbalancesfordate(theDate) 的货币和余额结果。

标签: sql function postgresql stored-procedures


【解决方案1】:

这是我想出的问题的答案。

DROP FUNCTION cc_getbalancesfordatewitheurs (date);

CREATE OR REPLACE FUNCTION cc_getbalancesfordatewitheurs(theDate date) RETURNS TABLE(from_currency char, to_currency char, Exchange char, rate numeric(20,10),current_balance numeric(20,10), converted_amount numeric(20,10)) AS $$

 (SELECT currency AS from_currency,
(SELECT to_currency FROM  cc_getratesfordate(theDate,balance,currency)),
(SELECT exchange  FROM  cc_getratesfordate(theDate,balance,currency)),
(SELECT rate  FROM  cc_getratesfordate(theDate,balance,currency)),

平衡 AS current_balance, (SELECT final_balance FROM cc_getratesfordate(theDate,balance,currency)) FROM cc_getbalancesfordate(theDate) WHERE currency 'BTC' and currency 'EUR') ;

$$ LANGUAGE SQL;

SELECT * FROM cc_getbalancesfordatewitheurs('2014-02-15'::date);

它非常丑陋且效率低下,但我只需要每天运行一次,所以如果它需要额外的几毫秒它不是问题。 不过,如果有人知道更有效的方法来写这篇文章,我一定会很感激你的帮助。

感谢任何提供帮助的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    相关资源
    最近更新 更多