create table mytable (id integer not null,date_start TEXT,date_end TEXT,wanted_full_month INTEGER);
insert into mytable (id, date_start, date_end, wanted_full_month)
values (1, '1992-09-15', '1992-11-14',1); /* Incomplete second month */
insert into mytable (id, date_start, date_end, wanted_full_month)
values (2, '1992-09-15', '1992-11-15',2); /* Complete second month */
insert into mytable (id, date_start, date_end, wanted_full_month)
values (3, '1992-09-15', '1992-10-14',0); /* Incomplete first month */
insert into mytable (id, date_start, date_end, wanted_full_month)
values (4, '1992-01-31', '1992-02-29',1);
/* It's the end of the month of date_end and the end of the month of date_start,
we take it as a complete month */
insert into mytable (id, date_start, date_end, wanted_full_month)
values (5, '1992-01-30', '1992-02-29',1);
/* It's the end of the month of date_end, it couldn't go longer,
we take it as a complete month */
SELECT *,floor((julianday(date_end) - julianday(date_start))/30) as wrong_full_months from mytable; as wrong_full_months from mytable;
如何使用SQLite从DuckDB(documentation、source code)获得像date_sub
这样的函数?也就是说,像列wanted_full_months
(而不是我的例子中的30天的倍数)一样获得(不规则的)月差。
2条答案
按热度按时间m1m5dgzv1#
我将创建一个函数,通过解析出年、月和日的日期,并假装每个月都有我们计算的31天,将日期字符串转换为绝对日期:
absolute_day
= (year * 12 + month) * 31 + day然后,我们可以使用以下命令计算两个绝对日期的月份差:
month_diff = floor((absolute_day1 - absolute_day2) / 31)`在Python中,这将如下所示:
打印:
disbfnqx2#
如果您需要使用SQLite代码的解决方案:
请参阅demo。