MySQL 每天按价值排名
我正在尝试按每天 700 个交易品种的回报百分比进行排名。
例如:
date symbol pct_return
-----------------------------
1100101 IBM 1.2
1100101 AAPL 2.1
1100101 HPQ -0.5
日期 1100101 大约有 700 个这样的条目
1100102 IBM -.02
1100102 AAPL -.6
1100102 HPQ 1.9
日期 1100102 大约有 700 个这样的
条目 我想要做的是创建一个查询或存储过程来循环每一天,然后排名并插入排名值每天每个品种的回报百分比。
我想插入百分比回报的升序和降序排名值。
排名后仅包含 3 个交易品种的示例表如下所示:
date symbol pct_return rank_asc rank_desc
------------------------------------------------------
1100101 IBM 1.2 2 2
1100101 AAPL 2.1 3 1
1100101 HPQ -0.5 1 3
1100102 IBM -.02 2 2
1100102 AAPL -.6 1 3
1100102 HPQ 1.9 3 1
I am attempting to rank by % return for each day across 700 symbols.
For Example:
date symbol pct_return
-----------------------------
1100101 IBM 1.2
1100101 AAPL 2.1
1100101 HPQ -0.5
approx 700 more entries like this for date 1100101
1100102 IBM -.02
1100102 AAPL -.6
1100102 HPQ 1.9
approx 700 more entries like this for date 1100102
What I am trying to do is create a query or stored procedure to loop through each day, and then rank and insert the rank value for the percent return for each symbol within each day.
I would like to insert the rank values for both ascending and descending ranking of percent return.
Sample table for just 3 symbols after ranking would look like:
date symbol pct_return rank_asc rank_desc
------------------------------------------------------
1100101 IBM 1.2 2 2
1100101 AAPL 2.1 3 1
1100101 HPQ -0.5 1 3
1100102 IBM -.02 2 2
1100102 AAPL -.6 1 3
1100102 HPQ 1.9 3 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用此语法来选择选择中的行号:
然后您可以按每天的
ORDER BY
升序和降序选择所有值,并将它们插入到表中。来源:http://snippets.dzone.com/posts/show/6831
示例:
要获取升序值,然后使用类似的查询更新同一个表以获取降序值。
You can use this syntax to select the row number in your select:
You can then select all values with
ORDER BY
ascending and descending for each day, and insert them into your table.Source: http://snippets.dzone.com/posts/show/6831
Example:
To get the ascending values, then an update on the same table with a similar query to get the descending values.
这是典型的组内聚合问题,可以通过左自排除连接解决。
您不需要任何存储过程来获取所需的结果,只需一个简单的
INSERT INTO ... SELECT ...
查询即可实现。以下是包含所提供数据的示例脚本:
这是计算排名的查询(抱歉格式错误,我无法使其在
插入排序符号 (
dat、符号、pct_return、rank_asc、rank_desc
) 选择 ars.dat、ars.symbol、ars.pct_return、ars.rank_asc、COUNT(ss3.dat)+1rank_desc
从 (
选择 ss1.dat、ss1.symbol、ss1.pct_return、COUNT(ss2.dat)+1rank_asc
来自 shuffled_symbols ss1
左连接 shuffled_symbols ss2
ON ss2.dat = ss1.dat
AND ss2.pct_return < ss1.pct_return
按 ss1.dat、ss1.symbol 分组
) ars
左连接 shuffled_symbols ss3
ON ss3.dat = ars.dat
和 ss3.pct_return > ars.pct_return
按 ars.dat、ars.symbol 分组
;
请注意,如果给定日期没有重复的符号,则此查询只会返回有效的排名。这就是我使用
PRIMARY KEY (dat ,symbol)
创建shuffled_symbols
表的原因。在ranked_symbols 表中,您将得到以下结果:
This is the typical problem of within-group aggregates that is solved with a left self exclusion join.
You don't need any stored procedure to get the results you want, just a simple
INSERT INTO ... SELECT ...
query will do the trick.Here is an example script with the provided data:
Here is the query to compute ranks (sorry for the bad formatting, I couldn't make it display correctly inside
<pre>
tags):INSERT INTO ranked_symbols (
dat, symbol, pct_return, rank_asc, rank_desc
) SELECT ars.dat, ars.symbol, ars.pct_return, ars.rank_asc, COUNT(ss3.dat)+1 rank_desc
FROM (
SELECT ss1.dat, ss1.symbol, ss1.pct_return, COUNT(ss2.dat)+1 rank_asc
FROM shuffled_symbols ss1
LEFT JOIN shuffled_symbols ss2
ON ss2.dat = ss1.dat
AND ss2.pct_return < ss1.pct_return
GROUP BY ss1.dat, ss1.symbol
) ars
LEFT JOIN shuffled_symbols ss3
ON ss3.dat = ars.dat
AND ss3.pct_return > ars.pct_return
GROUP BY ars.dat, ars.symbol
;
Please note that this query will only return valid ranks if you don't have duplicates of symbols for a given date. This is why I created the
shuffled_symbols
table with aPRIMARY KEY (dat ,symbol)
.In ranked_symbols table you get the following results:
要从命令行运行代码:
rank.pl FromTableNoRank ToTableWithRank pct_return DESC
To run the code from command line:
rank.pl FromTableNoRank ToTableWithRank pct_return DESC