gpt4 book ai didi

mysql - mysql中如何分组、排序和取出前N个?

转载 作者:搜寻专家 更新时间:2023-10-30 20:22:54 24 4
gpt4 key购买 nike

可以拿到组里排序好的数据,最后取出DolphinDB中的前N个。

但是在mysql中如何分组排序取出前N呢?

先谢谢你

DolphinDB代码如下:

sym = `C`MS`MS`MS`IBM`IBM`C`C`C$symbol;
price= 49.6 29.46 29.52 30.02 174.97 175.23 50.76 50.32 51.29;
qty = 2200 1900 2100 3200 6800 5400 1300 2500 8800;
timestamp = [09:34:07,09:36:42,09:36:51,09:36:59,09:32:47,09:35:26,09:34:16,09:34:26,09:38:12];
t1 = table(timestamp, sym, qty, price);
select top 2 * from t1 context by sym csort timestamp;

timestamp sym qty price
--------- --- ---- ------
09:34:07 C 2200 49.6
09:34:16 C 1300 50.76
09:32:47 IBM 6800 174.97
09:35:26 IBM 5400 175.23
09:36:42 MS 1900 29.46
09:36:51 MS 2100 29.52

最佳答案

如果你有支持 ROW_NUMBER() 的 MySQLv8.0,你可以使用这样的东西:

 SELECT `timestamp`,sym,qty,price
FROM
(
SELECT
`timestamp`,sym,qty,price,ROW_NUMBER() OVER (PARTITION BY sym ORDER BY timestamp ASC) AS ranking
FROM `table`
) t
WHERE ranking<3

https://www.db-fiddle.com/f/8fY2DJmCCv7kCnEAPBAU3B/0

关于mysql - mysql中如何分组、排序和取出前N个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56395189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com