我正在尝试从3个表格中选择一个银行帐户交易报告:个人,帐户和交易。我想要的结果是一个包含三列的表: 帐户所有者名称|交易编号|交易后帐户的其余部分。 这些是我的原始表格:
--Person ===>>>
CREATE TABLE "BANK"."ACCOUNTS"
"ID" NUMBER(*,0) NOT NULL ENABLE,
"PERSONID" NUMBER(*,0)
--Accounts ===>>>
CREATE TABLE "BANK"."PERSON"
"ID" NUMBER(*,0) NOT NULL ENABLE,
"NAME" VARCHAR2
--Transactions ===>>>
CREATE TABLE "BANK"."TRANSACTION"
"ID" NUMBER(*,0) NOT NULL ENABLE,
"OAID" NUMBER, -- Origin Account ID
"DAID" NUMBER, -- Destination Account ID
"VALUE" FLOAT(126),
"TIMESTAMP" TIMESTAMP (6)
我为此一直在苦苦挣扎,但我还没有想到逻辑。我将不胜感激。
答案 0 :(得分:0)
以下查询可以为您提供帮助:
select p.name as account_owner, t.id as transaction_id,
SUM(case when t.oaid = a.id then -t.value else t.value end)
OVER(PARTITION BY a.id ORDER BY t.timestamp RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS remainder
from bank.person p
inner join bank.accounts a on a.personid = p.id
inner join bank.transaction t on t.oaid = a.id or t.daid = a.id
对于任何澄清,请随时发表评论。