查询获取数据
我是 SQL 新手,需要编写一个看起来很复杂的查询。 我需要编写一个查询来从表中获取回报,如下所示:
id Price_date price
1 1-1-2010 20
1 2-2-2010 21
1 7-2-2010 22
1 27-2-2010 23
1 3-3-2010 23
这是我需要从上表中选择的内容:-
- id,
- Price_date(本月最后价格的日期),
- return((last_price_of_month/last_price_of_previous_month) - 1)、
- last_date_for_return_calculation(下个月的第一天)
示例数据如下:-
id price_date return last_date_for_return_calculation
1 27-2-2010 (23/20 -1) 1-3-2011
有人可以帮我解决这个问题吗?
I am a newbee to SQL and need to write a query which seems to be complex.
I need to write a query for getting the returns from a table which looks like:
id Price_date price
1 1-1-2010 20
1 2-2-2010 21
1 7-2-2010 22
1 27-2-2010 23
1 3-3-2010 23
Here is what I need to select from the above table:-
- id,
- price_date(date of last price of month),
- return((last_price_of_month/last_price_of_previous_month) -1),
- last_date_for_return_calculation(first day of next month)
And the sample data would be like:-
id price_date return last_date_for_return_calculation
1 27-2-2010 (23/20 -1) 1-3-2011
Could somebody help me in solving this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的要求,您的日期似乎是 2011 年,而不是 2010 年。此外,我在一月份添加了一行,以确保获得样本数据上个月的最后一行。
based on your requirement, it seems your date are in 2011, not 2010. Also I added a row in january to be sure to get the last row of the previous month of the sample data.
通过查询就可以实现。
但是,如果您只是像这样查询表并在程序端过滤它,您可能会更好。
It's possible with a query.
But you're probably better of if you just query the table like that and filter it on your program side.