[英]Last active row by a column and aggregate it to another column
So, I have this kind of transaction data;所以,我有这种交易数据; a cust_id can have one or more acc_no. cust_id 可以有一个或多个 acc_no。 I want to pull the latest data of cust_id based on the latest data it has by DATE, and aggregate sum it by acc_no.我想根据 DATE 拥有的最新数据提取 cust_id 的最新数据,并按 acc_no 汇总总和。
Sample Data样本数据
+-----------+-------------+--------------+-------------------+
| DATE | acc_no | cust_id | total_balance |
+-----------+-------------+--------------+-------------------+
| 1/1/2021 | 1 | A201 | 300 |
| 1/1/2021 | 2 | A201 | 20 |
| 1/1/2021 | 3 | A202 | 40 |
| 1/1/2021 | 4 | A203 | 200 |
| 1/1/2021 | 5 | A203 | 150 |
| 2/1/2021 | 2 | A201 | 50 |
| 3/1/2021 | 2 | A201 | 100 |
| 4/1/2021 | 1 | A201 | 400 |
| 4/1/2021 | 2 | A201 | 125 |
| 5/1/2021 | 3 | A202 | 50 |
| 5/1/2021 | 5 | A203 | 165 |
+-----------+-------------+--------------+-------------------+
Desired output所需 output
+-----------+--------------+-------------------+
| DATE | cust_id | total_balance |
+-----------+--------------+-------------------+
| 4/1/2021 | A201 | 525 |
| 5/1/2021 | A202 | 50 |
| 5/1/2021 | A203 | 365 |
+-----------+--------------+-------------------+
I tried to use:我尝试使用:
SELECT *
FROM (
SELECT DATE
,cust_id
,total_balance
,ROW_NUMBER() OVER (
PARTITION BY customer_id ORDER BY DATE DESC
) rn
FROM `a.table`
) q
WHERE rn = 1
It will return only one of the acc_no not the aggregate of it它只会返回一个 acc_no 而不是它的聚合
+-----------+--------------+----------------------------------------+
| DATE | cust_id | total_balance |
+-----------+--------------+----------------------------------------+
| 4/1/2021 | A201 | sometimes 400 sometimes 125 (random) |
| 5/1/2021 | A202 | 50 |
| 5/1/2021 | A203 | 165 |
+-----------+--------------+----------------------------------------+
If you have any suggestions on how to create the right query, please let me know.如果您对如何创建正确的查询有任何建议,请告诉我。 Any suggestions are appreciated.任何建议表示赞赏。
----Edit---- - - 编辑 - -
The answer below by meysam asadi is great, but what if the case of acc_no of the same cust_id is on a different date? meysam asadi 下面的答案很好,但是如果同一个 cust_id 的 acc_no 的情况是在不同的日期呢?
----Edit2---- ----编辑2----
Adding sample to my questions for more clarity在我的问题中添加示例以更清楚
You can use the following query.您可以使用以下查询。 I first grouped the records according to cust_id
and then joined it to its own table and again grouped the records according to cust_id
so that I could use the sum
function.我首先根据cust_id
对记录进行分组,然后将其加入到自己的表中,然后再次根据cust_id
对记录进行分组,这样我就可以使用sum
function。
SELECT t2.date,t2.cust_id,SUM(t2.total_balance) AS total_balance
FROM
(SELECT MAX(date) AS date,cust_id FROM yourTable GROUP BY cust_id) t1
INNER JOIN yourTable t2 ON t1.cust_id = t2.cust_id AND t1.date = t2.date
GROUP BY t2.cust_id,t2.date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.