我的目标:我有一个 stock_ids 列表,并且想要获取最后一个 bids (按日期排序)每个 stock_id 一个。
对于图片来说,这意味着我想要:
| stock_id | 出价 |
|---|---|
| 3 | 663.91953 |
| 1 | 46.44281 |
| 2 | 9.02798 |
一个问题是我们有像俄罗斯天然气工业股份公司这样的股票已停牌,因此最后的报价之一可能是例如 2021-06-06。
在 quote_day = DATE(NOW()) 在这种情况下不起作用。
我还需要与第一个较低日期相同的日期,该日期不在第一个查询中,这可以通过第二个查询来完成。
我当前使用 PHP 的解决方案。这是有效的,但性能并不完美,就像 100 只股票需要 5 秒一样。
我可以使用 Redis,它也可以选择将出价保存在某处。
当前:
select `quote_date`, 'stocks' as `type`, `bid`, `stock_id` as id
from (
select t.*, row_number()
over(partition by stock_id order by `quote_date` desc) as rn
from end_day_quotes_AVG t
where quote_date <= DATE({$date})
AND stock_id in ({$val})
and currency_id = {$c_id}
) x where rn = 1
前一天:
select `quote_date`, 'stocks' as `type`, `bid`, `stock_id` as id
from (
select t.*, row_number()
over(partition by stock_id order by `quote_date` desc) as rn
from end_day_quotes_AVG t
where quote_date < DATE({$date})
AND stock_id in ({$val})
and currency_id = {$c_id}
) x where rn = 1
Stock_id、quote_date 和 currency_id 是唯一的。
我想要使用服务器数据的表:10.9.4-MariaDB-1:10.9.4
编辑:
解释的查询:
id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 220896 Using where 2 DERIVED t ALL stock_id,quote_date NULL NULL NULL 2173105 Using where; Using temporary
创建表:
CREATE TABLE `end_day_quotes_AVG` ( `id` int(11) NOT NULL, `quote_date` date NOT NULL, `bid` decimal(15,5) NOT NULL, `stock_id` int(11) DEFAULT NULL, `etf_id` int(11) DEFAULT NULL, `crypto_id` int(11) DEFAULT NULL, `certificate_id` int(11) DEFAULT NULL, `currency_id` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; INSERT INTO `end_day_quotes_AVG` (`id`, `quote_date`, `bid`, `stock_id`, `etf_id`, `crypto_id`, `certificate_id`, `currency_id`) VALUES (10537515, '2023-01-02', '16.48286', 40581, NULL, NULL, NULL, 2), (10537514, '2023-01-02', '3.66786', 40569, NULL, NULL, NULL, 2), (10537513, '2023-01-02', '9.38013', 40400, NULL, NULL, NULL, 2), (10537512, '2023-01-02', '8.54444', 40396, NULL, NULL, NULL, 2), ALTER TABLE `end_day_quotes_AVG` ADD PRIMARY KEY (`id`), ADD KEY `stock_id` (`stock_id`,`currency_id`), ADD KEY `etf_id` (`etf_id`,`currency_id`), ADD KEY `crypto_id` (`crypto_id`,`currency_id`), ADD KEY `certificate_id` (`certificate_id`,`currency_id`), ADD KEY `quote_date` (`quote_date`); ALTER TABLE `end_day_quotes_AVG` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10570526;
生成的填充查询:
select `quote_date`, 'stocks' as `type`, `bid`, `stock_id` as id from
( select t.*, row_number() over(partition by stock_id order by `quote_date` desc) as rn
from end_day_quotes_AVG t where quote_date <= DATE('2023-01-02') AND stock_id in (2,23,19,41,40,26,9,43,22,
44,28,32,30,34,20,10,13,17,27,35,8,29,39,16,33,5,36589,25,18,6,38,37,3,45,7,21,46,15,4,24,31,36,38423,40313,
22561,36787,35770,36600,35766,42,22567,40581,40569,29528,22896,24760,40369,40396,40400,40374,36799,1,27863,
29659,40367,27821,24912,36654,21125,22569,22201,
23133,40373,36697,36718,26340,36653,47,34019,36847,36694) and currency_id = 2 ) x where rn = 1; Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您是否正在寻找截至给定日期每个出价的两个最新报价?如果是这样,您只需修改第一个查询以允许行号 1 和 2:
select `quote_date`, 'stocks' as `type`, `bid`, `stock_id` as id from ( select t.*, row_number() over(partition by stock_id order by quote_date desc) as rn f from end_day_quotes_AVG t where quote_date <= DATE(?) AND stock_id in (?) and currency_id = ? ) x where rn <= 2 -- the latest two