我正在使用DB2,并在一个表上写一个SQL查询,在接下来的每个月我都有一列。像一月,二月,三月,四月,五月等。总共12个。
数据是未来12个月的预测销售量。每晚都会重新计算。
例如,如果当前月份为2019年11月,则11月列包含对2019年11月的预测,12月列包含对2019年12月的预测,等等。但是,一月列包含2020年1月的数据。
如何在SQL中获取列所代表的下个月的日期?
我需要一个类似GetNextJanuary()的函数,如果今年使用该函数将返回20200101,而一个GetNextDecember()则返回20191201等。但是,如果我今天使用GetNextJune(),它应该返回20190601(当前月)。
单独使用SQL是否有可能?
答案 0 :(得分:0)
我们可以说这个问题是在输入日期加上一年,然后截断到该年的开始:
SELECT
DATE_TRUNC('YEAR', ADD_YEARS(CURRENT_DATE, 1))
FROM dual;
答案 1 :(得分:0)
您可以使用 int main(int argc,char** argv){
int fds[2];
int fd_read,fd_write;
if(pipe(fds)==-1){
perror("Error");
exit(1);
}
fd_read=fds[0];
fd_write=fds[1];
close(1);
dup(fd_write);
close(fd_write);
printf("Done");
return 0;
}
逻辑:
case
答案 2 :(得分:0)
9
逻辑是:如果通过的月份数(create or replace function GetNextMonth(p_month int)
returns date
deterministic
no external action
return
date(digits(dec(year(current date), 4))||'-'||digits(dec(p_month, 2))||'-01')
+ (case when p_month < month(current date) then 1 else 0 end) years;
select *
from table(values
(getnextmonth(1), getnextmonth(12), getnextmonth(6))
) t (jan, dec, june);
JAN DEC JUNE
---------- ---------- ----------
2020-01-01 2019-12-01 2019-06-01
)小于当前月份,则我们将1年添加到从通过的月份数的当前年份的第一天开始构造的日期中,否则为0年。