SQL中如何根据范围对记录进行分组
你好我想创建一个SQL来根据范围对记录进行分组
例如,假设我
Number Time Price
100 20100810 10.0
100 20100812 15.0
160 20100810 10.0
200 20100810 12.0
210 20100811 13.0
300 20100811 14.0
350 20100810 16.0
现在需要根据“Number”的范围获取记录:[100,200),[200,300), [300,400) 和 [0,400]
。对于每个范围,我需要最新“时间”的“价格”,
因此结果应该是
NumberRange Time Price
1 20100812 15.0
2 20100811 13.0
3 20100811 14.0
4 20100812 15.0
如何构建 SQL 语句来生成此结果?
我没有在特定的数据库上工作。所以我正在寻找没有特定数据库的SQL语句
Hi I would like to create a SQL to group the records according to the range
For example, suppose I have
Number Time Price
100 20100810 10.0
100 20100812 15.0
160 20100810 10.0
200 20100810 12.0
210 20100811 13.0
300 20100811 14.0
350 20100810 16.0
Now I need to get the records according to the range of the "Number": [100,200),[200,300),[300,400) and [0,400]
. For each range, I need the "Price" of the lastest "Time"
So the results should be
NumberRange Time Price
1 20100812 15.0
2 20100811 13.0
3 20100811 14.0
4 20100812 15.0
How can I construct a SQL statement to produce this?
I am not working on a specific Database. So I am looking for no specific database SQL statement
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用:
Use:
家庭作业?
您可以使用 if 语句选择附加列来确定相关的范围 ID。然后按这个新列进行分组。
Homework?
You could select an additional column with an if statement to determine what range id relevant. Then group by this new column.
尽管以下一些要求不属于核心标准 SQL 的范围,但它们是完整的标准 SQL 功能(例如,行构造函数是完整的 SQL-92 功能,CTE 是完整的 SQL-99 功能等),并且确实会在某些SQL Server 和 Oracle 等产品:
Although some of the following requires fall outside of core Standard SQL, they are full Standard SQL features (e.g. row constructors are a full SQL-92 feature, CTEs are a full SQL-99 feature, etc), and will indeed be found in some products such as SQL Server and Oracle: