I have a database that contains transactions made in client investments. For each quarter (four months), there is one opening balance, one closing balance, and N transactions in between.
The table is structured as follows:
transaction_id (primary key)
investment_id (index, foreign key)
amount (decimal(11,2))
type (ENUM:'Opening','Closing','Transfer')
transaction_date (DATE)
I need to create a view that will, for each investment_id, retrieve the first transaction of type 'Opening', the last transaction of type 'Closing', and all N transactions of type 'Transfer' in between for a particular date range. However, no transactions of type 'Opening' or 'Closing' should be included other than the endpoints in that range. For example:
SELECT * FROM view_transactions
WHERE transaction_date > '2000-01-01' AND transaction_date < '2006-06-01'
AND investment_id = 6578734
Thanks!