简体   繁体   English

我如何在下个月从oracle sql获取条目?

[英]How do I get entries in the next month from oracle sql?

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. 假设我有一个包含“ user_id,日期,分数”的表,每个用户每个月都有一个分数,但并非总是在同一天。

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? 我想查询一个具有“ user_id,date,score_delta”的查询,其中score_delta是“ date”和下个月之间分数的变化? Am I going to have to do something lame like to_date(to_char(date, ... ? 我是否需要像to_date(to_char(date,...?

Here's one way (lameness quotient calculation left as an exercise to the reader): 这是一种方法(留给读者练习的余量商):

CREATE TABLE scores (user_id VARCHAR2(32), test_date DATE, score NUMBER);

INSERT INTO scores VALUES('U1',SYSDATE-61, 85);
INSERT INTO scores VALUES('U1',SYSDATE-31, 89);
INSERT INTO scores VALUES('U1',SYSDATE, 92);
INSERT INTO scores VALUES('U2',SYSDATE-61, 65);
INSERT INTO scores VALUES('U2',SYSDATE-31, 89);
INSERT INTO scores VALUES('U2',SYSDATE, 84);

COMMIT;

SELECT s1.user_id, s1.test_date, s2.score-s1.score delta
  FROM scores s1 
       JOIN (SELECT user_id, trunc(test_date,'MM') test_date, score FROM scores) s2
         ON (s1.user_id = s2.user_id AND
             trunc(add_months(s1.test_date,1),'MM') = s2.test_date);

USER_ID                          TEST_DATE        DELTA
-------------------------------- ---------   ----------
U1                               9/15/2009            3
U1                               8/16/2009            4
U2                               9/18/2009           -5
U2                               8/19/2009           24

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: 编辑 :今天下午很慢,所以我决定研究10g提供的这种分析功能(进一步将自己拖入当前世纪;-),并使用LAG函数重写以上内容:

SELECT user_id, test_date, score
     , LAG(score, 1, NULL) OVER (PARTITION BY user_id ORDER BY test_date DESC) - score delta
     , LAG(score, 1, NULL) OVER (PARTITION BY user_id ORDER BY test_date DESC) AS next_score
  FROM scores
 ORDER BY 1, 2 DESC;

Which produces: 产生:

USER_ID                          TEST_DATE        SCORE      DELTA NEXT_SCORE
-------------------------------- ----------- ---------- ---------- ----------
U1                               10/19/2009          92            
U1                               9/18/2009           89          3         92
U1                               8/19/2009           85          4         89
U2                               10/19/2009          84            
U2                               9/18/2009           89         -5         84
U2                               8/19/2009           65         24         89

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 . 作为跳板,我从这个asktom.com问题开始。

Something like this should return the user_id, date and score entered next month. 这样的事情应该返回下个月输入的user_id,日期和分数。

select user_id, date, score  from table where
date between ((select sysdate from dual)
and (select add_months(sysdate, 1) FROM dual));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM