首页 > 解决方案 > 在 SQL 中获取过去 12 个月的汇总交易信息

问题描述

ID#     transaction_amount    transaction_yr_mon
12345   20                    202007
23455   200                   202008
12345   34                    202007

我想得到以下信息:

ID#    Number_of_transactions_in last_12_months   total_transaction_amount  
12345  2                                          54                       

我不知道如何开始在 SQL 中执行此操作,请告知可能的查询可能是什么!

谢谢

标签: sqlpostgresql

解决方案


select id,count(id) as Number_of_transactions_in last_12_months,sum(transaction_amount) as total_transaction_amount  
from tablename group by id

或者

select id,count(id) as Number_of_transactions_in last_12_months,sum(transaction_amount) as total_transaction_amount  
from tablename group by id,transaction_yr_mon

推荐阅读