0

我正在使用数据库超文件客户端/服务器使用windev 17。

我有一个名为 Operation 的表,其中包含列(accountNumber、date、amount、operationType)。

operationType 可以取两个值:“payment”和“withdrawal”。

我想选择一个帐户中完成的操作列表,我的列表应该显示 5 列:日期、帐户编号、金额、操作类型和余额。

最后一列(余额)应该是在当前日期之前完成的所有类型为“付款”的操作的总和与当前日期之前完成的所有操作的总和之间的差值,类型为“提款”

我尝试以下sql代码

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

但查询不显示任何值。

我也试过这个

 SELECT date as dateop, accountNumber, operationType, deposits - withdrawals AS balance
FROM Operations o INNER JOIN (
     SELECT accountNumber, date, SUM(amount) AS withdrawals
     FROM Operaions
     WHERE operationType = 'withdrawal' AND date<=dateop
     GROUP BY accountNumber
) a ON  o.accountNumber = a.accountNumber
INNER JOIN (
     SELECT accountNumber,date, SUM(amount) AS deposits
     FROM Operations
     WHERE operationType = 'deposit' AND date<=dateop
     GROUP BY accountNumber
)b ON o.accountNumber = b.accountNumber

但我收到一条错误消息,告诉我 dateop 列不存在。

请问我需要帮助

4

1 回答 1

0

给你,我的男人。

select accountNumber, dateField, sum(isnull(deposits,0)) - sum(isnull(withdrawals,0)) from (
select accountNumber, operationType, dateField, 
case when operationType = 'deposit' then sum(sum(amount)) over (partition by accountNumber, operationType order by dateField asc) else null end deposits,
case when operationType = 'withdrawal' then sum(sum(amount)) over (partition by accountNumber, operationType order by dateField asc) else null end withdrawals
from operations a
group by accountNumber, operationType, dateField
) a group by accountNumber, dateField
于 2015-06-24T15:34:35.673 回答