最大连胜内的行数?
给定行,
symbol_id profit date
1 100 2009-08-18 01:01:00
1 100 2009-08-18 01:01:01
1 156 2009-08-18 01:01:04
1 -56 2009-08-18 01:01:06
1 18 2009-08-18 01:01:07
我如何最有效地选择涉及最大连续(利润)的行。
最大的连胜是前 3 行,我想要这些行。我想出的查询只是一堆嵌套查询和派生表。我正在寻找一种使用公用表表达式或更高级的方法来执行此操作的有效方法。
Given the Rows
symbol_id profit date
1 100 2009-08-18 01:01:00
1 100 2009-08-18 01:01:01
1 156 2009-08-18 01:01:04
1 -56 2009-08-18 01:01:06
1 18 2009-08-18 01:01:07
How would I most efficiently select the rows that are involved in the greatest streak (of profit).
The greatest streak would be the first 3 rows, and I would want those rows. The query I came up with is just a bunch of nested queries and derived tables. I am looking for an efficient way to do this using common table expressions or something more advanced.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尚未定义如何处理
0
利润,或者如果最长连胜平局会发生什么。但类似...或者 - 如果您正在寻找最有利的连续走势
You haven't defined how
0
profit should be treated or what happens if there is a tie for longest streak. But something like...Or - if you are looking for most profitable streak
结果:
Result:
如果那是 MSSQL 服务器,那么您需要考虑在 select 子句中使用 TOP 3
并按利润描述排序。
如果 mysql/postgres 您可能需要考虑在 select 子句中使用 limit
也同样的顺序。
希望这有帮助。
If that's a MSSQL server then you want to consider using TOP 3 in your select clause
and ORDER BY PROFIT DESC.
If mysql/postgres you might want to consider using limit in your select clause with
the same order by too.
hope this helps.