Make a select with one of the column based on other select

112 views Asked by At

I'm working with windev using the database hyperfile client/serveur.

I have a table named Operation with colums (accountNumber, date, amount, operationType ).

operationType can take two values: "payment" and "withdrawal".

I want to select the List of operations done in an account and, my List should display 5 colums: Date, accountNumber, amount, operationType and balance.

The last column (balance) should be the difference between the sum of all operations done before the current date with type "payment" and the sum of all operation done before the current date with type "withdrawal"

I try the following sql code

SELECT accountNumber, date as dateOpe, amount, operationType, (SUM (SELECT Operation.amount 
                                                                   FROM Operation 
                                                                   WHERE Operation.date<=dateOpe AND Operation.operationType='payment')
                                                             -SUM (SELECT Operation.amount
                                                                   FROM Operation
                                                                   WHERE Operation.date<=dateOpe AND Operation.operationType='withdrawal')) as balance
 FROM Operation

But I always have an error telling me that i do not have the right to put a select in the SUM

Please can somebody help me. pease how can i write a such sql query.

thanks in advance

3

There are 3 answers

1
Dan On

Try something like the following, using a subquery to find the SUMs. There may be a more elegant solution, but this should work.

SELECT date, accountNumber,
    operationType, deposits - withdrawals AS balance
FROM Operations o INNER JOIN (
    SELECT accountNumber, SUM(amount) AS withdrawals
    FROM Operaions
    WHERE operationType = 'withdrawal'
    GROUP BY accountNumber
) a ON  o.accountNumber = a.accountNumber
INNER JOIN (
    SELECT accountNumber, SUM(amount) AS deposits
    FROM Operations
    WHERE operationType = 'deposit'
    GROUP BY accountNumber
)b ON o.accountNumber = b.accountNumber
0
Lajos Arpad On

I would use the fact that a logical expression yields 0 if false and 1 if true. Therefore, Operation.amount * <logical expression> will yield 0 if the expression is false and Operation.amount if the expression is true. As a result, the following query should be close to a solution (untested):

        SELECT accountNumber, date as dateOpe, amount, operationType, 
               SUM (Operation.amount * (Operation.date<=dateOpe AND Operation.operationType='payment')) -
SUM (Operation.amount * (Operation.date<=dateOpe AND Operation.operationType='withdrawal')) as balance
         FROM Operation
0
Saedawke On

Please try this:

SELECT accountNumber, date, amount, operationType, 
       (SELECT SUM(amount) AS balance  
          FROM operation WHERE operationType='payment' and date=date) 
      -(SELECT SUM(amount) AS balance  
          FROM operation WHERE operationType='withdrawal' and date=date) 
       as balance  
       FROM operation