Nachigo
Nachigo

Reputation: 23

How to use function result twice in the same select

I have a stored procedure where I need to return both function results and the average from both, how can I call them only once to get the result?

Select 
    get_valueX(jobId, c.id), 
    get_valueY(jobId, c.id), 
    (get_valueX(jobId, c.id) + get_valueY(jobId, c.id)) / 2, 
    c.name 
From candidate c

(Reduced the procedure code to only required code to see the problem, also jobId is an input variable from procedure)

Upvotes: 2

Views: 315

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562368

Select x, y, name, (x + y)/2 As xy_avg
From (
  Select 
    get_valueX(jobId, c.id) As x, 
    get_valueY(jobId, c.id) As y, 
    c.name
  From candidate c
) As t;

Upvotes: 1

Related Questions