sulaiman kadkhodaei
sulaiman kadkhodaei

Reputation: 13

how put result of a select query to a function in postgresql

I have a function named get_open_profit that calculated some data.

input of this function does not work properly.

I have a table named results that if we querying on it the result is :

select sum_buy_trades from results order by sum_buy_trades limit 1 : 274

select total_avg_buy from results order by sum_buy_trades limit 1 : 2019746

when I write function like this

select get_open_profit(274, 2019746) result is : 30192700

But if write like this I got error

select get_open_profit(select sum_buy_trades from results order by sum_buy_trades limit 1, select total_avg_buy from results order by sum_buy_trades limit 1

why it does not worked?

Upvotes: 1

Views: 63

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269493

If you want to use scalar subqueries (that is, subqueries that return one value), then each needs their own parentheses:

select get_open_profit( (select sum_buy_trades
                         from results
                         order by sum_buy_trades
                         limit 1
                        ),
                        (select total_avg_buy
                         from results
                         order by sum_buy_trades
                         limit 1
                        )
                       );

In this case, though, the query might be more naturally written as:

select get_open_profit( r.sum_buy_trades, r.total_avg_buy )
from (select sum_buy_trades, total_avg_buy
      from results
      order by sum_buy_trades
      limit 1
     ) r;

Upvotes: 2

Related Questions