如何从 oracle sql 获取下个月的条目?
假设我有一个包含“user_id、日期、分数”的表,每个用户每个月都有一个分数,但并不总是在同一天。
我想要一个包含“user_id,date,score_delta”的查询,其中score_delta是“日期”和下个月之间分数会发生多少变化?我是否必须做一些蹩脚的事情,比如 to_date(to_char(date, ... )?
Let's say I have a table that has "user_id, date, score", and every user has exactly one score every month, but not always on the same day.
I want a query that has "user_id, date, score_delta" where score_delta is how much the score will change between "date" and the next month? Am I going to have to do something lame like to_date(to_char(date, ... ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种方法(跛行商计算留给读者作为练习):
编辑:这是一个缓慢的下午,所以我决定研究 10g 提供的分析函数内容(进一步将自己拖入当前的世纪;-),并使用 LAG 函数重写了上面的内容:
产生:
看,马!不准自行加入!现在这很光滑;-)(顺便说一句,解释计划表明自连接效率不高)。
作为跳板,我从这个 询问tom.com问题。
Here's one way (lameness quotient calculation left as an exercise to the reader):
EDIT: It's a slow afternoon, so I decided to look into this analytic function stuff that 10g offers (further dragging myself into the current century ;-), and rewrote the above using the LAG function:
Which produces:
Look, Ma! No self-join! Now THAT's slick ;-) (As an aside, the explain plans indicate the self-join is not as efficient).
As a springboard, I started with this asktom.com question.
像这样的东西应该返回下个月输入的 user_id、日期和分数。
Something like this should return the user_id, date and score entered next month.