如何在DB 2中获取每月的当前最后一天

时间:2016-08-17 03:55:21

标签: sql date db2

我在表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

1 个答案:

答案 0 :(得分:0)

试试这个;

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.