Write a SQL query to join two tables and calculate aggregated metrics by group.
Use the accounts and transactions tables. Calculate metrics for every account segment, including segments whose accounts have no transactions. Transactions without a matching account should not contribute to any segment.
NULL segment when present.segment, account_count, transaction_count, total_amount, and average_amount.segment ascending, place the NULL segment last, and round monetary metrics to two decimal places.| Column | Type | Description |
|---|---|---|
| account_idPK | INT | Unique account identifier |
| client_name | VARCHAR(100) | Account holder name |
| segment | VARCHAR(30) | Account segment used for reporting |
| Column | Type | Description |
|---|---|---|
| transaction_idPK | INT | Unique transaction identifier |
| account_id | INT | Account associated with the transaction |
| transaction_date | DATE | Transaction settlement date |
| amount | NUMERIC(12,2) | Transaction amount |