简体   繁体   English

如果我必须每个月运行 sql 脚本,我如何才能在 ORACLE 中获取上个月的数据?

[英]How can I get the previous month data in ORACLE, if I have to run the sql script every month?

Here is my version of SQL code:这是我的 SQL 代码版本:

select HISTORY_ID,START_DTM FROM LEM.LEM_ALERT_HISTORY 
where MONTH from START_DTM = add_months (sysdate, -1) ;

Also, what if the current month is Jan, how can I get the data from last year's Dec?另外,如果当前月份是一月,我怎样才能得到去年十二月的数据?

Thanks a lot非常感谢

You can try this one:你可以试试这个:

select HISTORY_ID
     , START_DTM
  from LEM.LEM_ALERT_HISTORY
 where START_DTM between TRUNC(ADD_MONTHS(SYSDATE, -1),'MM') AND  LAST_DAY(ADD_MONTHS(TRUNC(SYSDATE,'mm'),-1))

If the current month is January it will return the data from December last year如果当前月份是 1 月,它将返回去年 12 月的数据

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

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