如何在 DB2 中获取每个月的当前最后一天
Posted
技术标签:
【中文标题】如何在 DB2 中获取每个月的当前最后一天【英文标题】:How to get the current last day for every month in DB 2 【发布时间】:2016-08-17 03:55:20 【问题描述】:我在表 sql 中有多个数据日期。在每个日期都有每个月的日期范围。我想得到 每个月的当前最后一天。怎么做?。有人知道吗?
例如
daterange month **current last day this month**
---------------------------------------------------------------
2016-01-04 2016-01-22
2016-01-05 2016-01-22
2016-01-06 2016-01-22
2016-01-07 2016-01-22
2016-01-08 2016-01-22
2016-01-09 2016-01-22
2016-01-10 2016-01-22
2016-01-11 2016-01-22
2016-01-12 2016-01-22
2016-01-13 2016-01-22
2016-01-14 2016-01-22
..........
..........
..........
2016-01-22 2016-01-22
2016-02-02 2016-02-25
2016-02-03 2016-02-25
2016-02-04 2016-02-25
2016-02-05 2016-02-25
2016-02-06 2016-02-25
..........
..........
..........
2016-02-25 2016-02-25
2016-03-01 2016-03-23
2016-03-02 2016-03-23
2016-03-03 2016-03-23
2016-03-04 2016-03-23
2016-03-05 2016-03-23
2016-03-06 2016-03-23
..........
..........
..........
2016-03-23 2016-03-23
【问题讨论】:
请澄清“当前每个月的最后一天”是什么意思。 2016 年 1 月的最后一天是 1 月 22 日吗? 1 月 22 日是表中存在的随机数据。我只想获取该月的最后一天。 【参考方案1】:试试这个;
db2 "select T1.yourColumn as cur_date,
T2.yourColumn as last_day
from yourtable T1,
( SELECT yourColumn FROM (SELECT yourColumn, ROW_NUMBER() OVER (PARTITION BY YEAR(yourColumn), MONTH(yourColumn) ORDER BY DAY(yourColumn) DESC) AS rowNum FROM yourtable) WHERE rowNum = 1
) as T2 where MONTH(T1.yourColumn)=MONTH(T2.yourColumn)"
前;
user@host:/home/db2inst1:>db2 "select * from test"
DATARANGE
----------
01/04/2016
01/04/2016
01/05/2016
01/06/2016
01/07/2016
01/08/2016
01/09/2016
01/10/2016
01/11/2016
01/12/2016
01/13/2016
01/14/2016
01/22/2016
02/02/2016
02/03/2016
02/04/2016
02/05/2016
02/06/2016
02/25/2016
03/01/2016
03/02/2016
03/03/2016
03/04/2016
03/05/2016
03/06/2016
03/23/2016
26 record(s) selected.
user@host:/home/db2inst1:>db2 "select T1.DATARANGE as date,
T2.datarange as last_day
from test T1,
( SELECT datarange FROM (SELECT datarange, ROW_NUMBER() OVER (PARTITION BY YEAR(datarange), MONTH(datarange) ORDER BY DAY(datarange) DESC) AS rowNum FROM test) z WHERE rowNum = 1
) as T2 where MONTH(T1.DATARANGE)=MONTH(T2.datarange)"
DATE LAST_DAY
---------- ----------
01/04/2016 01/22/2016
01/04/2016 01/22/2016
01/05/2016 01/22/2016
01/06/2016 01/22/2016
01/07/2016 01/22/2016
01/08/2016 01/22/2016
01/09/2016 01/22/2016
01/10/2016 01/22/2016
01/11/2016 01/22/2016
01/12/2016 01/22/2016
01/13/2016 01/22/2016
01/14/2016 01/22/2016
01/22/2016 01/22/2016
02/02/2016 02/25/2016
02/03/2016 02/25/2016
02/04/2016 02/25/2016
02/05/2016 02/25/2016
02/06/2016 02/25/2016
02/25/2016 02/25/2016
03/01/2016 03/23/2016
03/02/2016 03/23/2016
03/03/2016 03/23/2016
03/04/2016 03/23/2016
03/05/2016 03/23/2016
03/06/2016 03/23/2016
03/23/2016 03/23/2016
26 record(s) selected.
【讨论】:
对不起。这不是我想要的。我只想显示每个月上个月的当前日期。例如 Jan,数据只存在到 2016-01-22。所以我只想计算每个月的最大日期日期。我尝试再次编辑我的表格 @AminSCO,这对你有帮助吗? 我在 Db2 中尝试过,但条件不起作用。你可以试试我的查询吗?从 int_vify 中选择 ivl_scheduler_proc_date以上是关于如何在 DB2 中获取每个月的当前最后一天的主要内容,如果未能解决你的问题,请参考以下文章