0

I am facing an issue where my query is not executing and I am getting the error near the over clause but I want result like see the pic below: enter image description here

I mean I want result like 25000-9000 =16000 ; 16000-5000 =11000; display output like that guys and here is my query

query:
SELECT s.id, s.cust_id, 
s.package_name,s.pending_amount,s.pack_received_amount, s.return_amount, 
s.payment_type,s.total_package_amount, SUM('s.pending_amount') OVER (ORDER 
BY s.id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS s.balance FROM 
payment s WHERE s.cust_id = S1307 and s.package_name= switzerland

My error was here screen shot of images guys:

2 Answers2

0
SELECT Total,
Received,
CASE WHEN @balance = 0 THEN
(@balance := Total - Received) 
ELSE 
@balance := @balance - Received
END AS Balance
FROM
Table1,
(select @balance:=0) as t2

Table1

Total   Received
-----------------
25000   9000
25000   5000

Output

Total   Received    Balance
----------------------------
25000   9000        16000
25000   5000        11000

DEMO

http://sqlfiddle.com/#!9/43cc31/18

Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27
0

This is your query:

select . . .,
       sum(s.pending_amount) over (order by s.id) as balance 
from payment s 
where s.cust_id = S1307 and s.package_name = 'switzerland';

If you don't have too many rows, a simple subquery will suffice:

select . . .,
       (select sum(s2.pending_amount)
        from payment s2
        where s2.id <= s.id and
              s2.cust_id = s.cust_id and
              s2.package_name = s.package_name
       ) as balance 
from payment s 
where s.cust_id = 'S1307' and
      s.package_name = 'switzerland';
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786