AllianceBernstein needs a daily trading-performance view for accounts managed through its portfolio platforms. Write a PostgreSQL query that aggregates eligible trades by account and trade date, then identifies the highest-performing accounts within each strategy.
Requirements
- Join trades to accounts and strategies.
- Include only active accounts and trades from January 1 through January 3, 2025.
- Aggregate daily trade count, notional volume, and realized P&L per account.
- Calculate each account's total P&L across the date range.
- Use a window function to rank accounts within each strategy by total P&L, retaining the top two ranks. Preserve ties.
- Return daily rows for qualifying accounts, ordered by strategy, performance rank, date, and account.
Output details
- Return columns in this order:
strategy_name, account_id, account_name, trade_date, daily_trade_count, daily_notional, daily_pnl, total_pnl, performance_rank.
- Treat the date range as inclusive of
2025-01-01 and exclusive of 2025-01-04.
- Treat NULL realized P&L as zero before calculating daily and total P&L.
- Sort by
strategy_id ascending, then performance_rank ascending, trade_date ascending, and account_id ascending.