I have the following tables:
Broker (Id, ...)
Desk (Id, BrokerId, ...)
Trader (Id, DeskId, ...)
Orders (Id, TraderId, ...)
Trade (Id, OrderId, Trade Price, Trade Volume, Trade Time, ...)
The query needs to return me the Broker Id, 24hr traded vol, current month traded vol, year to date traded vol. The query will take in 1 parameter (BrokerId), and if this parameter is NULL then it means fetch all the necessary info from all brokers, otherwise only from that specific broker.
Table relationships: Desk has FK with Broker (BrokerId), Trader has FK with Desk (DeskId), Orders has FK with Trader (TraderId), Trade has FK with Orders (OrderId).
I hope that I explained this clearly enough.
Any help is greatly appreciated.
EDIT: Expanding with sample data
Broker table
Desk table
Trader table
Orders table
Trade table
Query examples
BrokerId=1 should return the following result set:
| Broker Id | 24 hr vol | Curr month vol | YTD vol |
|---|---|---|---|
| 1 | 1 | 2 | 3 |
BrokerId=3 should return the following result set:
| Broker Id | 24 hr vol | Curr month vol | YTD vol |
|---|---|---|---|
| 3 | 0 | 0 | 5 |
BrokerId=NULL should return the following result set:
| Broker Id | 24 hr vol | Curr month vol | YTD vol |
|---|---|---|---|
| 1 | 1 | 2 | 3 |
| 3 | 0 | 0 | 5 |




