Hello everyone here is the following information
в соммы summa summa_som ntype_operation course_valname datecourse ---------- ------------ --------------- --------- ----------- 100 6900 BUY USD 15/03/2017 100 6990 SELL USD 15/03/2017 100 7400 BUY EUR 15/03/2017 100 7520 SELL EUR 15/03/2017 I want to calculate the profit if a person bought 100 dollars from me at the rate of 69 and the rate is 68 then I will get a profit of 100. Taking the value of purchases and sales from another table
Table t_course
course_valname datecourse buy sell ------------- ---------- ---- ---- USD 15/03/2017 69 69.9 EUR 15/03/2017 74 75.2 I ran this code and got the following result.
select summa_som, max(case when ntype_operation = 'BUY' then summa_som when ntype_operation = 'SELL' then summa_som - summa * get_rate2 (date_operation,course_valname,upper(substr(ntype_operation,1,1))) else 0 end) as income, course_valname,NTYPE_OPERATION from t_operation WHERE DATECOURSE = TO_DATE('3/15/2017','MM/DD/YYYY') and summa = 100 group by summa_som, course_valname,NTYPE_OPERATION; summa_som income course_valname ntype_operation ------------------------------------------------ 7392 7392 EUR BUY 7594 0 EUR SELL Where zero should be the result of the addition of 7594 - 7392 = 202 in income and where buy 7392 income should in principle be zero and not at all display what this purchase is in theory, my exchanger makes a profit from what sells for more. I now need to withdraw the difference.