如何统计汇总一个表,给出每天涨幅前5名的成为一个二维表

如果想统计汇总一个表,给出每天涨幅前5名的成为一个二维表,行为代码,列为日期。除了用每天统计,然后链接,还有什么办法更简洁优雅的吗?

请先 登录 后评论

1 个回答

wale
思路:
1. 使用context + having + rank 过滤出每天涨幅前5的股票
2. 使用pivot by 转换成截面(宽表)模式

date = stretch(2024.01.01..2024.01.10, 100)
sym = take("A"+string(0..9), 100)
incPersent = rand(1.0, 100)
t = table(date, sym, incPersent)
select incPersent 
from (
    select * from t
    context by date
    csort incPersent desc
    having rank(incPersent) < 5
)
pivot by date, sym
请先 登录 后评论