If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to display years and months between Jan 1 2008 and current date?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-22-11, 09:56
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
How to display years and months between Jan 1 2008 and current date?

Hi,
I would like to write an SELECT statement that would return two columns: year and month that are between Jan 1, 2008 and current date (current date is variable that is changing each day)

Result should be:
Code:
YEAR  MONTH
----  -----
2008      1
2008      2
2008      3
...
2011     10
2011     11
2011     12   <-- today is Dec 22, 2011 so this should be the last month
I should probably write some kind of recursive SQL, query staring with: "WITH TEMP...", but I don't have enough knowledge to write such a SQL. Any help is appreciated.
Regards
Reply With Quote
  #2 (permalink)  
Old 12-22-11, 19:18
TonyF123 TonyF123 is offline
Registered User
 
Join Date: May 2008
Posts: 17
This might work

create an integers table as per rudy's page

Integer table

Only one field and ten rows, and will always come in handy

then you can do something like this

Code:
select year((current_date -i.ii months)) as "Year" 
        , month((current_date -i.ii months)) as "Month"
          from 
         (select 10*tens.i + units.i ii 
          from integers tens 
          cross join integers units) i 
          where (current_date -i.ii months) > '31/12/2007' 
          order by "Year","Month"
Reply With Quote
  #3 (permalink)  
Old 12-22-11, 19:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
thanks for the reference, tony

nowadays, i don't bother with that cross join nonsense, i've loaded up my table with 937 integers to keep the queries simple

why 937, you ask? because most problems require no more numbers than that

frinstance, the "months since 2008" calculation won't require a cross join for at least 75 years

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 12-22-11, 20:15
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
If you are using DB2 for LUW, VALUES clause can be used instead of the "integer table".
And, repeated expressions can be calculated once by using LATERAL keyword.
(DB2 for z/OS and DB2 for iSeries also support LATERAL(or TABLE) keyword.)

Example 1 :
Code:
------------------------------ Commands Entered ------------------------------
SELECT YEAR ( monthly ) AS year
     , MONTH( monthly ) AS month
 FROM  (VALUES 0,1  ,2  ,3  ,4  ,5  ,6  ,7  ,8  ,9  ) k(k1)
 CROSS JOIN
       (VALUES 0,10 ,20 ,30 ,40 ,50 ,60 ,70 ,80 ,90 ) k(k2)
 CROSS JOIN
       (VALUES 0,100,200,300,400,500,600,700,800.900) k(k3)
 CROSS JOIN
       LATERAL
       (VALUES DATE('2008-01-01') + (k1 + k2 + k3) MONTHs ) f(monthly)
 WHERE monthly <= CURRENT_DATE
 ORDER BY
       monthly
;
------------------------------------------------------------------------------

YEAR        MONTH      
----------- -----------
       2008           1
       2008           2
       2008           3
...
...
...
       2011          10
       2011          11
       2011          12

  48 record(s) selected.
Reply With Quote
  #5 (permalink)  
Old 12-22-11, 20:59
fengsun2 fengsun2 is offline
Registered User
 
Join Date: Nov 2011
Posts: 124
How about This:
with temp (year,month) as ( values(2008,1),(2008,2),(2011,11),(2011,12) )
select * from temp
where DATE(trim(CHAR(year))||'-'||trim(CHAR(month))||'-01')>=DATE('2008-01-01')
and DATE(trim(CHAR(year))||'-'||trim(CHAR(month))||'-01') <= DATE(current_date)
Reply With Quote
  #6 (permalink)  
Old 12-22-11, 22:59
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
Another possibility is a recursive common table expression:
Code:
WITH YY_MM_TAB (CALC_DT, YY, MM)
  AS (
SELECT CAST('2008-01-01' AS DATE), YEAR('2008-01-01'), MONTH('2008-01-01') FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT CALC_DT + 1 MONTH, YEAR(CALC_DT + 1 MONTH), MONTH(CALC_DT + 1 MONTH)
FROM YY_MM_TAB
WHERE CALC_DT + 1 MONTH < CURRENT DATE
)
SELECT *
FROM YY_MM_TAB

CALC_DT    YY          MM         
---------- ----------- -----------
01/01/2008        2008           1
02/01/2008        2008           2
03/01/2008        2008           3
04/01/2008        2008           4
05/01/2008        2008           5
06/01/2008        2008           6
07/01/2008        2008           7
08/01/2008        2008           8
09/01/2008        2008           9
10/01/2008        2008          10
11/01/2008        2008          11
12/01/2008        2008          12
01/01/2009        2009           1
02/01/2009        2009           2
03/01/2009        2009           3
(removed entries to save space)
12/01/2010        2010          12
01/01/2011        2011           1
02/01/2011        2011           2
03/01/2011        2011           3
04/01/2011        2011           4
05/01/2011        2011           5
06/01/2011        2011           6
07/01/2011        2011           7
08/01/2011        2011           8
09/01/2011        2011           9
10/01/2011        2011          10
11/01/2011        2011          11
12/01/2011        2011          12

  48 record(s)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On