azzazzello
Technical User
I have the following query:
mysql> desc tradesTable;
+--------------+-------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+-------------------+-------+
| date | date | NO | PRI | NULL | |
| account_id | int(16) | NO | PRI | NULL | |
| stock_id | varchar(32) | NO | PRI | NULL | |
| symbol | varchar(32) | NO | | NULL | |
| trades | int(40) | NO | | 0 | |
+--------------+-------------+------+-----+-------------------+-------+
I wish to aggregate trades across a date range and present it by account_id and stock_id. Stock_id uniquely identifies a stock, whereas "symbol" is just what it is called now. So symbol can change, but stock_id never will. I would also like to have the LATEST symbol name (by date) in this rollup. I am doing the following:
But whether I get the latest symbol or not seems arbitrary. How can I force it to be the latest, whenever that might be. This table is rather large (40M+ rows)
mysql> desc tradesTable;
+--------------+-------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+-------------------+-------+
| date | date | NO | PRI | NULL | |
| account_id | int(16) | NO | PRI | NULL | |
| stock_id | varchar(32) | NO | PRI | NULL | |
| symbol | varchar(32) | NO | | NULL | |
| trades | int(40) | NO | | 0 | |
+--------------+-------------+------+-----+-------------------+-------+
I wish to aggregate trades across a date range and present it by account_id and stock_id. Stock_id uniquely identifies a stock, whereas "symbol" is just what it is called now. So symbol can change, but stock_id never will. I would also like to have the LATEST symbol name (by date) in this rollup. I am doing the following:
Code:
select account_id,stock_id,symbol,sum(trades)
from tradesTable
where date between 20110101 and 20110301
group by account_id,stock_id
But whether I get the latest symbol or not seems arbitrary. How can I force it to be the latest, whenever that might be. This table is rather large (40M+ rows)