简体   繁体   中英

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM