函数内的函数调用返回类型不匹配postgres

时间:2014-06-30 10:08:50

标签: sql function postgresql stored-procedures

我已经做了尽可能多的研究,我在这里看了大约20篇帖子,还有更多关于谷歌的文章。似乎没有人回答我的问题。所以这里是: 我有一个函数需要来自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"

所以我尝试添加第3列并抱怨最后一列返回记录。我不知道如何解决这个问题。

基本上我希望它返回数据,因此它有两列货币和余额。

这是另一个函数的代码。

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;

1 个答案:

答案 0 :(得分:0)

以下是我对我的问题的回答。

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))      来自cc_getbalancesfordate(theDate)WHERE货币<> ' BTC'和货币<> ' EUR');

$$ LANGUAGE SQL;

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

它非常丑陋且效率低下但是我只需要每天运行一次,所以如果需要几毫秒就不会有问题。 虽然如果有人知道更有效的方式来写这个,我一定会感谢你的帮助。

感谢任何帮助过的人。