简体   繁体   English

如何使用 sum 计算 sql 中的总余额?

[英]How to calculate total balance in sql using sum?

updated question --更新的问题——

I have a table that contains the following columns:我有一个包含以下列的表:

DROP TABLE TABLE_1;
CREATE TABLE TABLE_1(
TRANSACTION_ID number, USER_KEY number,AMOUNT number,CREATED_DATE DATE, UPDATE_DATE DATE
);

insert into TABLE_1
values ('001','1001',75,'2022-12-02','2022-12-03'),
('001','1001',-74.98,'2022-12-02','2022-12-03'),
('001','1001',74.98,'2022-12-03','2022-12-04'),
('001','1001',-75,'2022-12-03','2022-12-04')

I need to calculate the balance based on the update date.我需要根据更新日期计算余额。 In some cases there can be the same update_date for two different records.在某些情况下,两条不同的记录可能有相同的 update_date。 When I have this, I want to grab the lower value of the balance.当我有这个时,我想抓住余额的较低值。

This is the query I have so far:这是我到目前为止的查询:

select * from (
select TRANSACTION_ID,USER_KEY,AMOUNT,CREATED_DATE,UPDATE_DATE,
sum(AMOUNT) over(partition by USER_KEY order by UPDATE_DATE rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as TOTAL_BALANCE_AMOUNT
from TABLE_1
) qualify row_number() over (partition by USER_KEY order by UPDATE_DATE DESC, UPDATE_DATE DESC) = 1

In the query above, it's is grabbing the 75, rather than the 0 after I try to only grab the LAST balance.在上面的查询中,在我尝试仅获取 LAST 余额之后,它正在获取 75,而不是 0。

Is there a way to include in the qualify query to grab the last balance but if the dates are the same, to grab the lowest balance?有没有办法在限定查询中包含最后一个余额,但如果日期相同,则获取最低余额?

why is the second query, showing 4 different record balances?为什么第二个查询显示 4 个不同的记录余额?

That is the point of "running total".这就是“总计”的意义所在。 If the goal is to have a single value per entire window then order by should be skipped:如果目标是在整个 window 中拥有一个值,则应跳过 order by:

select USER_KEY, 
       sum(AMOUNT) over(partition by USER_KEY) as TOTAL_BALANCE_AMOUNT
from TABLE1;

The partition by clause could be futher expanded with date to produce output per user_key/date: partition by 子句可以进一步扩展为每个 user_key/date 生成 output:

select USER_KEY, 
       sum(AMOUNT) over(partition by USER_KEY,date) as TOTAL_BALANCE_AMOUNT
from TABLE1;

I think you're looking for something like this, aggregate by USER_ID, DATE, and then calculate a running sum.我认为您正在寻找类似的东西,按 USER_ID、DATE 聚合,然后计算运行总和。 If this is not what you're looking for nor is Lukasz Szozda's answer, please edit the question to show the intended output.如果这不是您要查找的内容,也不是 Lukasz Szozda 的答案,请编辑问题以显示预期的 output。

    create or replace table T1(USER_KEY int, AMOUNT number(38,2), "DATE" date);
    
    insert into T1(USER_KEY, AMOUNT, "DATE") values
    (1001, 75,  '2022-12-02'),
    (1001, -75, '2022-12-02'),
    (1001, 75,  '2022-12-03'),
    (1001, -75, '2022-12-03');
    
-- Option 1, aggregate after window
select USER_KEY, "DATE", min(TOTAL_BALANCE_AMOUNT) as MINIMUM_BALANCE from 
(
select USER_KEY, "DATE", sum(AMOUNT)
over(partition by USER_KEY order by DATE, AMOUNT desc rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as TOTAL_BALANCE_AMOUNT from
T1
)
group by USER_KEY, "DATE"
;

--Option 2, qualify by partitioning by user and day, reversing the order of transactions
select USER_KEY, "DATE", sum(AMOUNT)
over(partition by USER_KEY order by DATE, AMOUNT desc rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as TOTAL_BALANCE_AMOUNT
from
T1
qualify row_number() over (partition by USER_KEY, DATE order by DATE, AMOUNT asc) = 1
;
USER_KEY用户密钥 DATE日期 TOTAL_BALANCE_AMOUNT TOTAL_BALANCE_AMOUNT
1001 1001 2022-12-02 00:00:00 2022-12-02 00:00:00 0 0
1001 1001 2022-12-03 00:00:00 2022-12-03 00:00:00 0 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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