Grouping a list of objects by multiple fields and conditional checks and also finding aggregate value in java 17

I have a list of Trade class which has fields like buy amount, buy currency, sell amount, sell currency, buy sell flag, settlement date, account, execution date time etc. I also have a lookback period of time in minutes (which I’ll get from the application.yml file). Now, I want to aggregate buy amount by grouping the trades into combination of account, settlement date and buy currency for all trades within execution date time minus the lookback period. Similarly, I want to aggregate sell amount by grouping them into combination of account, settlement date and sell currency for all trades within execution date time minus the lookback period.

In the aggregation process, the buy and sell currency are matched as per the buy sell flag.

While aggregating buy amount, if buy sell flag is B, then match buy currency with buy currency or else with sell currency.

While aggregating sell amount if the buy sell flag is B, then match sell currency with buy currency or else with sell currency.

If nothing matches while grouping, then the aggregated buy or sell amount value will be populated as 0.

Buy and sell amounts are in Bigdecimal, settlement date is in Localdate and execution date time is in Zoneddatetime.

This is the SQL query for reference :-

select

f.business_date,

f.trade_key,

f.account key,

f.currency_pair,

f.execution_date_time,

f.buy_sell_flag,

f.buy_currency,

convert( float, f.buy_amount) as buy_amount,

f.sell_currency,

convert( float, f.sell_amount ) as sell_amount,

f.local_currency,

convert( float, f.local_amount ) as local_amount,

f.region_cd,

isnull (

( select convert (float, sum(isnull( g.local_amount, 0.0 ) ) )

from group g

where f.account_key = g.account_key

and g.buy_currency = case

when f.buy_sell_flag = ‘B’ then f.buy_currency

else f.sell_currency

end

and f.settlement_pay_date = g.settlement_pay_date

and g.buy_sell_flag = ‘B’

and dateadd(mi, -convert (int, lookback_time) , f.execution date_time) < g.execution_date_time

and f.execution_date_time >= g.execution_date_time

) , 0

) as sum_buy_amount,

isnull (

(select convert (float, sum(isnull( g.local_amount, 0.0 ) ) )

from group g

where f.account_key = g.account_key

and g.sell_currency = case

when f.buy_sell_flag = ‘B’ then f.buy_currency

else f.sell_currency

end

and f.settlement_pay_date = g.settlement_pay_date

and f.region_cd = g.region_cd

and g.buy_sell_flag = ‘S’

and dateadd(mi, -convert (int, lookback_time ) , f.execution date_time) < g.execution_date_time

and f.execution_date_time >= g.execution_date_time

), 0

) as sum_sell_amount

f refers to the list of trades

So, how should I go about it. I know we need to use 3 level nested map for this. but the currency matching is making it a little complicated.

Thanks in advance.

New contributor

spark_enthusiast is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật