[英]Conditional debit from credits and running balance using SQL
我在表cryptotransactionledger中有如下數據
ID | transaction_typeid | 交易類型 | 數量 | 總幣 |
---|---|---|---|---|
1個 | 1個 | 比特幣信用 | 30 | 30 |
2個 | 2個 | 以太坊信用 | 20 | 50 |
如果我使用比特幣,我會在同一個表中添加一個如下所示的新條目,其中 transaction_typeid 為 3,對於以太坊,transaction_typeid 為 4 同樣
ID | transaction_typeid | 交易類型 | 數量 | 總幣 |
---|---|---|---|---|
1個 | 1個 | 比特幣信用 | 30 | 30 |
2個 | 2個 | 以太信用 | 20 | 50 |
3個 | 3個 | 比特幣借記 | -10 | 40 |
4個 | 4個 | 以太借記卡 | -5 | 35 |
假設如果我在表中的最終數據如下所示,我將有 35 個比特幣和 20 個以太坊剩余。
ID | transaction_typeid | 交易類型 | 數量 | 總幣 |
---|---|---|---|---|
1個 | 1個 | 比特幣信用 | 30 | 30 |
2個 | 2個 | 以太信用 | 20 | 50 |
3個 | 3個 | 比特幣借記 | -10 | 40 |
4個 | 4個 | 以太借記卡 | -5 | 35 |
5個 | 1個 | 比特幣信用 | 15 | 50 |
6個 | 2個 | 以太信用 | 10 | 60 |
7 | 4個 | 以太借記卡 | -5 | 55 |
在減少相應貸方的所有借方后,如何使用 SQL 得出低於余額的摘要
ID | 交易類型 | 數量 | 總幣 |
---|---|---|---|
1個 | 比特幣信用 | 35 | 35 |
2個 | 以太信用 | 20 | 55 |
我會忘記最終結果的“交易類型”,而只關注代幣本身。 您的數據 model 很奇怪,因為您沒有單獨的硬幣類型列。 但是您可以從transaction_type
列中提取它。
此外,您的評論建議totalcoins
應該是列中的最后一個值。 對於“比特幣”,那將是 50 個,而不是 35 個。
在任何情況下,您都可以使用聚合。 我會建議:
select regexp_substr(transaction_type, '^[^-]+') as coin,
sum(amount) as amount,
max(totalcoins) keep (dense_rank first oder by id desc) as totalcoins
from cryptotransactionledger tl
group by regexp_substr(transaction_type, '^[^-]+');
這是一個 db<>fiddle。
假設您有表ledger
和coin_types
:
create table ledger (id varchar2(3) primary key,
coin_type varchar2(2),
amount number(5)
);
create table coin_types (id varchar2(2) primary key,
alias varchar2(100));
這些 DML 語句代表您的“最終數據”:
insert into coin_types values (1, 'bitcoin');
insert into coin_types values (2, 'etherium');
insert into ledger values (1, 1, 30);
insert into ledger values (2, 2, 20);
insert into ledger values (3, 1, -10);
insert into ledger values (4, 2, -5);
insert into ledger values (5, 1, 15);
insert into ledger values (6, 2, 10);
insert into ledger values (7, 2, -5);
你的第一步應該是總結金額,按你的硬幣類型分組:
select l.coin_type,
c.alias,
sum(l.amount) amount_coin
from ledger l, coin_types c
where l.coin_type = c.id
group by l.coin_type, c.alias
然后,您可以使用此 select 將您的“totalcoins”作為累積總和,按您的 coin_type 排序( 請參閱 Oraclee Docs ):
select coin_type,
alias,
amount_coin,
sum(amount_coin) over (order by coin_type) totalcoins
from(
select l.coin_type,
c.alias,
sum(l.amount) amount_coin
from ledger l, coin_types c
where l.coin_type = c.id
group by l.coin_type, c.alias
);
這是一個有效的 SQL-Fiddle: SQL-Fiddle
我會創建一個 tran 類型表
create table tran_types (
id int primary key,
coin_Name varchar2(100),
op_Name varchar2(100)
);
insert into tran_types
select 1, 'bitcoin', 'credit' from dual union all
select 2, 'etherium','credit' from dual union all
select 3, 'bitcoin', 'debit' from dual union all
select 4, 'etherium','debit' from dual;
通過這種方式,您可以對事務進行分組以獲取總計並根據需要可視化總計行標題
select t.id transaction_typeid, t.coin_Name || '-' || t.op_Name transaction_type,
s.amount, s.total_coins
from (
select t.coin_Name, sum(amount) amount
, sum(sum(amount)) over(order by t.coin_Name) total_coins
from ledger r
join tran_types t on r.tran_type = t.id
group by t.coin_Name
) s
join tran_types t on t.op_name = 'credit' and t.coin_Name = s.coin_Name
order by t.coin_Name;
感謝所有那些提出建議和不同解決方案的人。 我終於使用了下面的查詢,它為我提供了預期的結果。
WITH cte1
AS (SELECT a.*,
CASE
WHEN ( transaction_typeid = 1
OR transaction_typeid = 3 ) THEN 0
ELSE 1
END AS category
FROM cryptotransactionledger a
ORDER BY id),
cte2
AS (SELECT CASE
WHEN ( category = 0 ) THEN 'bitcoin-credit'
ELSE 'etherium-credit'
END AS transaction_type,
Sum(amount) AS amount
FROM cte1 o
GROUP BY category),
cte3
AS (SELECT Row_number()
OVER (ORDER BY c.transaction_type) AS id,
c.*
FROM cte2 c) SELECT f.*,
Sum(amount) OVER(ORDER BY f.id) AS total_coins FROM cte3 f;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.