Data between two dates in a 12-month period

Hello Gentlemen
I am facing a problem hope you would solve it.
I have a table
CREATE TABLE MATCHES
(MT_ID     NUMBER(4) CONSTRAINT MT_ID_PK PRIMARY KEY,
START_DATE     DATE,
END_DATE     DATE,
TEAM_A     NUMBER(2),
TEAM_B     NUMBER(2),
SERIES_ID     NUMBER(3))
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B,SERIES_ID) VALUES(1,'1 Apr 1993', '5 Apr 1993',1,2,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B,SERIES_ID) VALUES(2,'21 Apr 1993', '25 Apr 1993',2,1,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B,SERIES_ID) VALUES(3,'30 Mar 1994', '3 Apr 1994',4,2,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B,SERIES_ID) VALUES(4,'1 May 1994', '5 May 1994',8,1,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B,SERIES_ID) VALUES(5,'24 Nov 1994', '28 Nov 1994',5,7,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B,SERIES_ID) VALUES(6,'31 Mar 1999', '4 Apr 1999',3,6,1);
select * from matches order by 1
MT_ID     START_DATE     END_DATE     TEAM_A     TEAM_B     SERIES_ID
1     01-APR-93     05-APR-93     1     2     1
2     21-APR-93     25-APR-93     2     1     1
3     30-MAR-94     03-APR-94     4     2     1
4     01-MAY-94     05-MAY-94     8     1     1
5     24-NOV-94     28-NOV-94     5     7     1
6     31-MAR-99     04-APR-99     3     6     1Where
MT_ID is Match ID which is primary key
stat_date is the start date of the match and end_date is the date on which the match ended.
Each match has two teams i.e Team A and Team B. Each team has its unique ID. So in the above data there are eight teams form
ID 1 to 8.
Duration of match can be taken out if we minus the start date from the end date. For example:
Select END_DATE-START_DATE +1
From MATCHES
END_DATE-START_DATE+1
5
5
5
5
5
5 1 is added to include the start day too.
My requirment is as follows:
For each of the 8 Teams, show
1. How many matches they played in each of the 7 twelve month periods from 1 April 1993 to 31 March 2000? If a match overlaped a 12 month period, then it would be treated as one match each on either side on the 12 month period.
For example, if a match started on 30 March 1994 and ended on 3 Apr 1994 then for the 12-month year starting from 1 Apr 1993 to 31 March 1994 it would be treated as one match and for the 12-month period starting from 1 Apr 1994 to 31 March 1995 it would be treated as another match. Becuase the match was split into 2 + 3 days on either side of the 12-month period.
2. How many days each team was playing a match in each of the 7 twelve month periods from 1 April 1993 to 31 March 2000?
For example, a team played 3 matches between Apr 1, 1993 and Mar 31, 1994. The Duration of each of the matches was 5+5+5 = 15 days. But the third match overlaped the 12-month period which started on 30 Mar 1994 and ended on 3 Apr 1994. So the correct number of playing days for that team would be 5 + 5 + 2 = 12 days from Apr 1, 1993 and Mar 31, 1994.
A want an efficient query that gets me both of the above answers
My desired output is like this:
For Team ID = 1      
Period                       MT   Playing Days
1-Apr-1993 to 31-Mar-1994    2    10    
1-Apr-1994 to 31-Mar-1995    1    5
1-Apr-1995 to 31-Mar-1996    0    0
1-Apr-1996 to 31-Mar-1997    0    0 
1-Apr-1997 to 31-Mar-1998    0    0
1-Apr-1998 to 31-Mar-1999    0    0
1-Apr-1999 to 31-Mar-2000    0    0
For Team ID = 2      
Period                       MT   Playing Days
1-Apr-1993 to 31-Mar-1994    3    12    
1-Apr-1994 to 31-Mar-1995    1    3
1-Apr-1995 to 31-Mar-1996    0    0
1-Apr-1996 to 31-Mar-1997    0    0 
1-Apr-1997 to 31-Mar-1998    0    0
1-Apr-1998 to 31-Mar-1999    0    0
1-Apr-1999 to 31-Mar-2000    0    0
For Team ID = 3      
Period                       MT   Playing Days
1-Apr-1993 to 31-Mar-1994    0    0    
1-Apr-1994 to 31-Mar-1995    0    0
1-Apr-1995 to 31-Mar-1996    0    0
1-Apr-1996 to 31-Mar-1997    0    0 
1-Apr-1997 to 31-Mar-1998    0    0
1-Apr-1998 to 31-Mar-1999    1    1
1-Apr-1999 to 31-Mar-2000    1    4
For Team ID = 4      
Period                       MT   Playing Days
1-Apr-1993 to 31-Mar-1994    1    2    
1-Apr-1994 to 31-Mar-1995    1    3
1-Apr-1995 to 31-Mar-1996    0    0
1-Apr-1996 to 31-Mar-1997    0    0 
1-Apr-1997 to 31-Mar-1998    0    0
1-Apr-1998 to 31-Mar-1999    0    0
1-Apr-1999 to 31-Mar-2000    0    0
For Team ID = 5     
Period                       MT   Playing Days
1-Apr-1993 to 31-Mar-1994    0    0    
1-Apr-1994 to 31-Mar-1995    1    5
1-Apr-1995 to 31-Mar-1996    0    0
1-Apr-1996 to 31-Mar-1997    0    0 
1-Apr-1997 to 31-Mar-1998    0    0
1-Apr-1998 to 31-Mar-1999    0    0
1-Apr-1999 to 31-Mar-2000    0    0
For Team ID = 6     
Period                       MT   Playing Days
1-Apr-1993 to 31-Mar-1994    0    0    
1-Apr-1994 to 31-Mar-1995    0    0
1-Apr-1995 to 31-Mar-1996    0    0
1-Apr-1996 to 31-Mar-1997    0    0 
1-Apr-1997 to 31-Mar-1998    0    0
1-Apr-1998 to 31-Mar-1999    1    1
1-Apr-1999 to 31-Mar-2000    1    4
For Team ID = 7     
Period                       MT   Playing Days
1-Apr-1993 to 31-Mar-1994    0    0    
1-Apr-1994 to 31-Mar-1995    1    5
1-Apr-1995 to 31-Mar-1996    0    0
1-Apr-1996 to 31-Mar-1997    0    0 
1-Apr-1997 to 31-Mar-1998    0    0
1-Apr-1998 to 31-Mar-1999    0    0
1-Apr-1999 to 31-Mar-2000    0    0
For Team ID = 8     
Period                       MT   Playing Days
1-Apr-1993 to 31-Mar-1994    0    0    
1-Apr-1994 to 31-Mar-1995    1    5
1-Apr-1995 to 31-Mar-1996    0    0
1-Apr-1996 to 31-Mar-1997    0    0 
1-Apr-1997 to 31-Mar-1998    0    0
1-Apr-1998 to 31-Mar-1999    0    0
1-Apr-1999 to 31-Mar-2000    0    0I would appreciate a prompt reply in this regard.
Thanks in advance
Ramis Shah

Dear Oscar
Thanks for your feedback. Yes, this problem has to be resolved becuase SQL from browser is better to work with. The command prompt SQLPlus is not user friendly.
Any way, Oscar I checked your last query on the sample data that I also posted in my question. It was fine. But when I ran the query on my actual base table it did not gave me the correct results for frequency of six weeks gap. It showed 0 for even those gaps where there is actually a six week gap and where there was a gap of atleast six weeks on more than one ocassion in a 12-month period it shows only 1 for those.
THe "Free days" were however generated correct by the query and its further accumulution to next period at cut-off point (31-Mar-YYYY) is also correct..But I missed to tell you one thing which I explained below.
The is no difference in between the base tables and the dummy table. The only difference is ofcourse the data and the number of records.
For this reason I copied here actual base table DDL data (54 records) in order to solve the
problem.
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1615,'03-OCT-2002','07-OCT-2002',1,7);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1617,'11-OCT-2002','15-OCT-2002',7,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1620,'19-OCT-2002','23-OCT-2002',1,7);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1623,'07-NOV-2002','11-NOV-2002',1,2);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1628,'21-NOV-2002','25-NOV-2002',2,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1629,'29-NOV-2002','03-DEC-2002',2,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1634,'26-DEC-2002','30-DEC-2002',1,2);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1636,'02-JAN-2003','06-JAN-2003',2,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1638,'10-APR-2003','14-APR-2003',4,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1639,'19-APR-2003','23-APR-2003',1,4);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1643,'01-MAY-2003','05-MAY-2003',1,4);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1650,'18-JUL-2003','22-JUL-2003',12,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1652,'25-JUL-2003','29-JUL-2003',12,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1661,'09-OCT-2003','13-OCT-2003',1,11);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1663,'17-OCT-2003','21-OCT-2003',11,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1671,'04-DEC-2003','08-DEC-2003',1,6);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1673,'12-DEC-2003','16-DEC-2003',1,6 );
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1678,'26-DEC-2003','30-DEC-2003',6,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1680,'02-JAN-2004','06-JAN-2004',6,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1685,'08-MAR-2004','12-MAR-2004',1,9);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1688,'16-MAR-2004','20-MAR-2004',1,9);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1691,'24-MAR-2004','28-MAR-2004',1,9);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1706,'09-JUL-2004','13-JUL-2004',1,9);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1720,'03-NOV-2004','07-NOV-2004',6,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1721,'18-NOV-2004','22-NOV-2004',5,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1723,'26-NOV-2004','30-NOV-2004',1,5);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1726,'16-DEC-2004','20-DEC-2004',1,7);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1729,'26-DEC-2004','30-DEC-2004',7,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1731,'02-JAN-2005','06-JAN-2005',7,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1739,'10-MAR-2005','14-MAR-2005',5,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1742,'18-MAR-2005','22-MAR-2005',1,5);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1744,'26-MAR-2005','30-MAR-2005',5,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1756,'21-JUL-2005','25-JUL-2005',1,2);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1758,'04-AUG-2005','08-AUG-2005',2,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1760,'11-AUG-2005','15-AUG-2005',2,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1762,'25-AUG-2005','29-AUG-2005',2,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1763,'08-SEP-2005','12-SEP-2005',2,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1768,'14-OCT-2005','19-OCT-2005',1,21);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1769,'03-NOV-2005','07-NOV-2005',1,4);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1771,'17-NOV-2005','21-NOV-2005',4,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1773,'25-NOV-2005','29-NOV-2005',4,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1777,'16-DEC-2005','20-DEC-2005',1,3);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1779,'26-DEC-2005','30-DEC-2005',1,3);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1780,'02-JAN-2006','06-JAN-2006',3,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1789,'16-MAR-2006','20-MAR-2006',3,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1792,'24-MAR-2006','28-MAR-2006',1,3);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1795,'31-MAR-2006','04-APR-2006',3,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1797,'09-APR-2006','13-APR-2006',12,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1799,'16-APR-2006','20-APR-2006',12,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1817,'23-NOV-2006','27-NOV-2006',1,2);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1819,'01-DEC-2006','05-DEC-2006',2,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1821,'14-DEC-2006','18-DEC-2006',1,2);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1824,'26-DEC-2006','30-DEC-2006',2,1);
INSERT INTO MATCHES (MT_ID,START_DATE,END_DATE,TEAM_A,TEAM_B) VALUES (1826,'02-JAN-2007','06-JAN-2007',2,1);Select * from matches
MT_ID    START_DATE      END_DATE   TEAM_A  TEAM_B    
1615     03-OCT-2002     07-OCT-2002     1     7    
1617     11-OCT-2002     15-OCT-2002     7     1    
1620     19-OCT-2002     23-OCT-2002     1     7    
1623     07-NOV-2002     11-NOV-2002     1     2    
1628     21-NOV-2002     25-NOV-2002     2     1    
1629     29-NOV-2002     03-DEC-2002     2     1    
1634     26-DEC-2002     30-DEC-2002     1     2    
1636     02-JAN-2003     06-JAN-2003     2     1    
1638     10-APR-2003     14-APR-2003     4     1    
1639     19-APR-2003     23-APR-2003     1     4    
1643     01-MAY-2003     05-MAY-2003     1     4    
1650     18-JUL-2003     22-JUL-2003     12    1    
1652     25-JUL-2003     29-JUL-2003     12    1    
1661     09-OCT-2003     13-OCT-2003     1     11    
1663     17-OCT-2003     21-OCT-2003     11    1    
1671     04-DEC-2003     08-DEC-2003     1     6    
1673     12-DEC-2003     16-DEC-2003     1     6    
1678     26-DEC-2003     30-DEC-2003     6     1    
1680     02-JAN-2004     06-JAN-2004     6     1    
1685     08-MAR-2004     12-MAR-2004     1     9    
1688     16-MAR-2004     20-MAR-2004     1     9    
1691     24-MAR-2004     28-MAR-2004     1     9    
1706     09-JUL-2004     13-JUL-2004     1     9    
1720     03-NOV-2004     07-NOV-2004     6     1    
1721     18-NOV-2004     22-NOV-2004     5     1    
1723     26-NOV-2004     30-NOV-2004     1     5    
1726     16-DEC-2004     20-DEC-2004     1     7    
1729     26-DEC-2004     30-DEC-2004     7     1    
1731     02-JAN-2005     06-JAN-2005     7     1    
1739     10-MAR-2005     14-MAR-2005     5     1    
1742     18-MAR-2005     22-MAR-2005     1     5    
1744     26-MAR-2005     30-MAR-2005     5     1    
1756     21-JUL-2005     25-JUL-2005     1     2    
1758     04-AUG-2005     08-AUG-2005     2     1    
1760     11-AUG-2005     15-AUG-2005     2     1    
1762     25-AUG-2005     29-AUG-2005     2     1    
1763     08-SEP-2005     12-SEP-2005     2     1    
1768     14-OCT-2005     19-OCT-2005     1     21    
1769     03-NOV-2005     07-NOV-2005     1     4    
1771     17-NOV-2005     21-NOV-2005     4     1    
1773     25-NOV-2005     29-NOV-2005     4     1    
1777     16-DEC-2005     20-DEC-2005     1     3    
1779     26-DEC-2005     30-DEC-2005     1     3    
1780     02-JAN-2006     06-JAN-2006     3     1    
1789     16-MAR-2006     20-MAR-2006     3     1    
1792     24-MAR-2006     28-MAR-2006     1     3    
1795     31-MAR-2006     04-APR-2006     3     1    
1797     09-APR-2006     13-APR-2006     12    1    
1799     16-APR-2006     20-APR-2006     12    1    
1817     23-NOV-2006     27-NOV-2006     1     2    
1819     01-DEC-2006     05-DEC-2006     2     1    
1821     14-DEC-2006     18-DEC-2006     1     2    
1824     26-DEC-2006     30-DEC-2006     2     1    
1826     02-JAN-2007     06-JAN-2007     2     1     Now, I run my query to show the number of free days between each Test for matches played by TEAM = 1
SELECT MT_ID, START_OF_FREE_DAYS,
START_DATE-1 END_OF_FREE_DAYS, START_DATE, END_DATE, TEAM_A,
TEAM_B, FREE_DAYS, to_char( trunc(FREE_DAYS/7)+mod(FREE_DAYS,7)/10,'990.0') WEEKS1
FROM
select MT_ID, lag(END_DATE, 1) over (order by MT_ID)+1 START_OF_FREE_DAYS,
START_DATE,
END_DATE,
TEAM_A, TEAM_B,
CASE
   WHEN DECODE(START_DATE-lag(START_DATE, 1) over (order by    
MT_ID)-1,-1,0,START_DATE-lag(START_DATE, 1) over (order by MT_ID)-1) <= 4 THEN 0
   WHEN DECODE(START_DATE-lag(START_DATE, 1) over (order by    
MT_ID)-1,-1,0,START_DATE-lag(START_DATE, 1) over (order by MT_ID)-1) > 4 THEN    
START_DATE-lag(END_DATE, 1) over (order by MT_ID)-1 END FREE_DAYS
FROM
MATCHES where TEAM_A = 1 OR TEAM_B = 1
MT_ID   START_OF   END_OF_   START_DATE  END_DATE  TEAM_ TEAM FREE_ Weeks1
                             FREEDAYS FREE_DAYS    A    _B DAYS 
1615   -           02-OCT-02   03-OCT-02   07-OCT-02   1   7   -     -     
1617   08-OCT-02   10-OCT-02   11-OCT-02   15-OCT-02   7   1   3   0.3  
1620   16-OCT-02   18-OCT-02   19-OCT-02   23-OCT-02   1   7   3   0.3  
1623   24-OCT-02   06-NOV-02   07-NOV-02   11-NOV-02   1   2   14  2.0  
1628   12-NOV-02   20-NOV-02   21-NOV-02   25-NOV-02   2   1   9   1.2  
1629   26-NOV-02   28-NOV-02   29-NOV-02   03-DEC-02   2   1   3   0.3  
1634   04-DEC-02   25-DEC-02   26-DEC-02   30-DEC-02   1   2   22  3.1  
1636   31-DEC-02   01-JAN-03   02-JAN-03   06-JAN-03   2   1   2   0.2  
1638   07-JAN-03   09-APR-03   10-APR-03   14-APR-03   4   1   93  13.2  
1639   15-APR-03   18-APR-03   19-APR-03   23-APR-03   1   4   4   0.4  
1643   24-APR-03   30-APR-03   01-MAY-03   05-MAY-03   1   4   7   1.0  
1650   06-MAY-03   17-JUL-03   18-JUL-03   22-JUL-03   12  1   73  10.3  
1652   23-JUL-03   24-JUL-03   25-JUL-03   29-JUL-03   12  1   2   0.2  
1661   30-JUL-03   08-OCT-03   09-OCT-03   13-OCT-03   1   11  71  10.1  
1663   14-OCT-03   16-OCT-03   17-OCT-03   21-OCT-03   11  1   3   0.3  
1671   22-OCT-03   03-DEC-03   04-DEC-03   08-DEC-03   1   6   43  6.1  
1673   09-DEC-03   11-DEC-03   12-DEC-03   16-DEC-03   1   6   3   0.3  
1678   17-DEC-03   25-DEC-03   26-DEC-03   30-DEC-03   6   1   9   1.2  
1680   31-DEC-03   01-JAN-04   02-JAN-04   06-JAN-04   6   1   2   0.2  
1685   07-JAN-04   07-MAR-04   08-MAR-04   12-MAR-04   1   9   61  8.5  
1688   13-MAR-04   15-MAR-04   16-MAR-04   20-MAR-04   1   9   3   0.3  
1691   21-MAR-04   23-MAR-04   24-MAR-04   28-MAR-04   1   9   3   0.3  
1706   29-MAR-04   08-JUL-04   09-JUL-04   13-JUL-04   1   9   102 14.4  
1720   14-JUL-04   02-NOV-04   03-NOV-04   07-NOV-04   6   1   112 16.0  
1721   08-NOV-04   17-NOV-04   18-NOV-04   22-NOV-04   5   1   10  1.3  
1723   23-NOV-04   25-NOV-04   26-NOV-04   30-NOV-04   1   5   3   0.3  
1726   01-DEC-04   15-DEC-04   16-DEC-04   20-DEC-04   1   7   15  2.1  
1729   21-DEC-04   25-DEC-04   26-DEC-04   30-DEC-04   7   1   5   0.5  
1731   31-DEC-04   01-JAN-05   02-JAN-05   06-JAN-05   7   1   2   0.2  
1739   07-JAN-05   09-MAR-05   10-MAR-05   14-MAR-05   5   1   62  8.6  
1742   15-MAR-05   17-MAR-05   18-MAR-05   22-MAR-05   1   5   3   0.3  
1744   23-MAR-05   25-MAR-05   26-MAR-05   30-MAR-05   5   1   3   0.3  
1756   31-MAR-05   20-JUL-05   21-JUL-05   25-JUL-05   1   2   112 16.0  
1758   26-JUL-05   03-AUG-05   04-AUG-05   08-AUG-05   2   1   9   1.2  
1760   09-AUG-05   10-AUG-05   11-AUG-05   15-AUG-05   2   1   2   0.2  
1762   16-AUG-05   24-AUG-05   25-AUG-05   29-AUG-05   2   1   9   1.2  
1763   30-AUG-05   07-SEP-05   08-SEP-05   12-SEP-05   2   1   9   1.2  
1768   13-SEP-05   13-OCT-05   14-OCT-05   19-OCT-05   1   21  31  4.3  
1769   20-OCT-05   02-NOV-05   03-NOV-05   07-NOV-05   1   4   14  2.0  
1771   08-NOV-05   16-NOV-05   17-NOV-05   21-NOV-05   4   1   9   1.2  
1773   22-NOV-05   24-NOV-05   25-NOV-05   29-NOV-05   4   1   3   0.3  
1777   30-NOV-05   15-DEC-05   16-DEC-05   20-DEC-05   1   3   16  2.2  
1779   21-DEC-05   25-DEC-05   26-DEC-05   30-DEC-05   1   3   5   0.5  
1780   31-DEC-05   01-JAN-06   02-JAN-06   06-JAN-06   3   1   2   0.2  
1789   07-JAN-06   15-MAR-06   16-MAR-06   20-MAR-06   3   1   68  9.5  
1792   21-MAR-06   23-MAR-06   24-MAR-06   28-MAR-06   1   3   3   0.3  
1795   29-MAR-06   30-MAR-06   31-MAR-06   04-APR-06   3   1   2   0.2  
1797   05-APR-06   08-APR-06   09-APR-06   13-APR-06   12  1   4   0.4  
1799   14-APR-06   15-APR-06   16-APR-06   20-APR-06   12  1   2   0.2  
1817   21-APR-06   22-NOV-06   23-NOV-06   27-NOV-06   1   2   216 30.6  
1819   28-NOV-06   30-NOV-06   01-DEC-06   05-DEC-06   2   1   3   0.3  
1821   06-DEC-06   13-DEC-06   14-DEC-06   18-DEC-06   1   2   8   1.1  
1824   19-DEC-06   25-DEC-06   26-DEC-06   30-DEC-06   2   1   7   1.0  
1826   31-DEC-06   01-JAN-07   02-JAN-07   06-JAN-07   2   1   2   0.2   As you see there is one 93-day gap (from 07-JAN-03 to 09-APR-03) before MT_ID = 1638 of which 84 days come between period 01-Apr-2002 to 31-Mar-2003 and the remaining nine from April 1 to 9. This one six-week gap is very well showed by the result of your query below. But in the period from 01-Apr-2003 to 31-Mar-2004 your query below does not show any six-week gap. whereas in the same period, above the results show four instances of atleast 42-days gap. i.e. 73, 71, 43 and 61 days.
So your query should show the following for this period:
TOT   TEAM PERIOD                    FREEDAYS EXISTS_SIX_WEEK_GAP  
-      1   01-Apr-2003 to 31-Mar-2004   296   4   Same is true for the next period 01-Apr-2004 to 31-Mar-2005.
For other TEAM ID's the six-week results generated by your query are ok, becuase for each of those teams, except for Team = 21, there was only one instance of 42 days gap. The query fails only when there is more than one 42-days gap.
For Team = 21, The query shows 1 instance of six-week gap but doesn't show the number of free days for that period. This is mainly becuase I told you in my question the other day that Free Days are calculated by taking the diffrence between the end date of the previous match and the start date of the current match. Since team = 21 played only one match so that's why the query showed no free-days becuase it did not found any previous date to take the difference. For this purpose I rephrase my question below the following table.
When I run your query, modifying the start period to 1-Apr-2002 instead of 1992 and connect by
level <= 5,
with periods as ( select add_months(dt,12*(rownum-1)) start_period,
                         add_months(dt,12*rownum)-1 end_period
                  from (select to_date('1-Apr-2002') dt from dual)
                  connect by level <= 5),
....,on my actual data it gives me the following result:
TOT   TEAM PERIOD                    FREEDAYS EXISTS_SIX_WEEK_GAP  
-      1   01-Apr-2002 to 31-Mar-2003   140   1  
-      1   01-Apr-2003 to 31-Mar-2004   296   0  
-      1   01-Apr-2004 to 31-Mar-2005   315   0  
-      1   01-Apr-2005 to 31-Mar-2006   293   0  
-      1   01-Apr-2006 to 31-Mar-2007   242   0  
TOTAL  1                         -      1286  1  
-      2   01-Apr-2002 to 31-Mar-2003   120   1  
-      2   01-Apr-2003 to 31-Mar-2004   366   0  
-      2   01-Apr-2004 to 31-Mar-2005   365   0  
-      2   01-Apr-2005 to 31-Mar-2006   340   0  
-      2   01-Apr-2006 to 31-Mar-2007   256   0  
TOTAL  2                         -      1447  1  
-      3   01-Apr-2002 to 31-Mar-2003   0     0  
-      3   01-Apr-2003 to 31-Mar-2004   0     0  
-      3   01-Apr-2004 to 31-Mar-2005   0     0  
-      3   01-Apr-2005 to 31-Mar-2006   80    1  
-      3   01-Apr-2006 to 31-Mar-2007   0     0  
TOTAL  3                         -      80    1  
-      4   01-Apr-2002 to 31-Mar-2003   0     0  
-      4   01-Apr-2003 to 31-Mar-2004   342   1  
-      4   01-Apr-2004 to 31-Mar-2005   365   0  
-      4   01-Apr-2005 to 31-Mar-2006   228   0  
-      4   01-Apr-2006 to 31-Mar-2007   0     0  
TOTAL  4                         -      935   1  
-      5   01-Apr-2002 to 31-Mar-2003   0     0  
-      5   01-Apr-2003 to 31-Mar-2004   0     0  
-      5   01-Apr-2004 to 31-Mar-2005   108   1  
-      5   01-Apr-2005 to 31-Mar-2006   0     0  
-      5   01-Apr-2006 to 31-Mar-2007   0     0  
TOTAL  5                         -      108   1  
-      6   01-Apr-2002 to 31-Mar-2003   0     0  
-      6   01-Apr-2003 to 31-Mar-2004   99    1  
-      6   01-Apr-2004 to 31-Mar-2005   216   0  
-      6   01-Apr-2005 to 31-Mar-2006   0     0  
-      6   01-Apr-2006 to 31-Mar-2007   0     0  
TOTAL  6                         -      315   1  
-      7   01-Apr-2002 to 31-Mar-2003   165   1  
-      7   01-Apr-2003 to 31-Mar-2004   366   0  
-      7   01-Apr-2004 to 31-Mar-2005   266   0  
-      7   01-Apr-2005 to 31-Mar-2006   0     0  
-      7   01-Apr-2006 to 31-Mar-2007   0     0  
TOTAL  7                         -      797   1  
-      9   01-Apr-2002 to 31-Mar-2003   0     0  
-      9   01-Apr-2003 to 31-Mar-2004   9     1  
-      9   01-Apr-2004 to 31-Mar-2005   99    0  
-      9   01-Apr-2005 to 31-Mar-2006   0     0  
-      9   01-Apr-2006 to 31-Mar-2007   0     0  
TOTAL  9                         -      108   1  
-      11   01-Apr-2002 to 31-Mar-2003  0     0  
-      11   01-Apr-2003 to 31-Mar-2004  3     1  
-      11   01-Apr-2004 to 31-Mar-2005  0     0  
-      11   01-Apr-2005 to 31-Mar-2006  0     0  
-      11   01-Apr-2006 to 31-Mar-2007  0     0  
TOTAL  11                        -      3     1  
-      12   01-Apr-2002 to 31-Mar-2003  0     0  
-      12   01-Apr-2003 to 31-Mar-2004  248   1  
-      12   01-Apr-2004 to 31-Mar-2005  365   0  
-      12   01-Apr-2005 to 31-Mar-2006  365   0  
-      12   01-Apr-2006 to 31-Mar-2007  10    0  
TOTAL  12                         -     988   1  
-      21   01-Apr-2002 to 31-Mar-2003   0    0  
-      21   01-Apr-2003 to 31-Mar-2004   0    0  
-      21   01-Apr-2004 to 31-Mar-2005   0    0  
-      21   01-Apr-2005 to 31-Mar-2006   0    1  
-      21   01-Apr-2006 to 31-Mar-2007   0    0  
TOTAL  21                         -      0    1   I want that Free-days should be calculated in the following way:
1. The number of free-days for each team would be calculated by the taking the difference between the end-date of the previous match and the start date of the current match.
2.If there is no previous match or the current match is the first match of the respective team in that 12-month period then the number of free-days would be calculated by taking the difference between the start date of that respective period (say 1-Apr-2002) and the start date of the current match. For example, Team = 21 played its first and only match in the 12-month period (01-Apr-2005 to 31-Mar-2006) from 14-19 OCT-,2005. So for this team the number of free days in this period would be 196 days (from 1-Apr-05 to 13-Oct-05) before this match and since no further match is played by this team in that period so the free days (163) from 20-Oct-2005 till 31-Mar-2006 (period end) would also come into this period. Hence the total number of free days by team = 21 in this period would be 196 + 163 = 359 days. The six-week gaps were two for this team in this period. Similary, for Team = 1 in the period from 1-April-2002 to 31-mar 2003, it played it first match from 03-07 OCT, 2002. Its free days for that period currently shown are 140 but
they should be 325 (140 + 185 days ==> from 1 April 2002 to OCt 2, 2002).
3. If a team plays its last match in any period well before end period say by Jan 6, 2007, then the period from Jan 7 to March 31, would also be counted in the free days. If the current date or SYSDATE is less then March 31, 2007, then the free-days would be counted from Jan 7 to current date or SYSdate.
4. If a team played no match in any period then the total number of days in that period would come as free days and this would count as only 1 six-week gap in that period.
5. If the free days overlaped a 12-month period then the number of free days that came till the end of the period break point would come into the preceding period and the remaining number of free days would come in the next 12-month period. For exmaple, a team played a match from 01-MAY-94 to 05-MAY-1994 and then played its next match on 01-APR-1999 to 05-APR-1999. So the number of free days between 06-MAY-1994 TO 31-MAR-1999 are 1791 days. (This condition is already well done by your current query). Lastly, for an overlaping gap (over 2 or more periods), the frequency of atleast six-weeks would only come in the period from where the six-week gap started.
I would prefer results in the format below. The free-days gaps in each period are breaked-up into actual gaps, showing the start and end date of the gap and the number of days and the "1" if the gap is of atleast 42 days. the first line of each period shows the sum of the total number of playing days between that period, sum of free days for each period and the sum of six-weeks for that period.
TOT   TEAM PERIOD                     Playing_days  FREEDAYS   EXISTS_SIX_WEEK_GAP  
-      1   01-Apr-2002 to 31-Mar-2003    40             325        2
              01-Apr-02 to 02-OCT-02                    185        1
              08-OCT-02 to 10-OCT-02                      3        -
              16-OCT-02 to 18-OCT-02                      3        -
              24-OCT-02 to 06-NOV-02                     14        -
              12-NOV-02 to 20-NOV-02                      9        -
              26-NOV-02 to 28-NOV-02                      3        -
              04-DEC-02 to 25-DEC-02                     22        -
              31-DEC-02 to 01-JAN-03                      2        -
              07-JAN-03 to 31-Mar-03                     84        1
          01-Apr-2003 to 31-Mar-2004     70             296        4
              01-Apr-03 to 09-Apr-03                      9        -
              15-APR-03 to 18-APR-03                      4        -    
              24-APR-03 to 30-APR-03                      7        -    
              06-MAY-03 to 17-JUL-03                     73        1    
              23-JUL-03 to 24-JUL-03                      2        -    
              30-JUL-03 to 08-OCT-03                     71        1    
              14-OCT-03 to 16-OCT-03                      3        -    
              22-OCT-03 to 03-DEC-03                     43        1    
              09-DEC-03 to 11-DEC-03                      3        -    
              17-DEC-03 to 25-DEC-03                      9        -    
              31-DEC-03 to 01-JAN-04                      2        -    
              07-JAN-04 to 07-MAR-04                     61        1    
              13-MAR-04 to 15-MAR-04                      3        -    
              21-MAR-04 to 23-MAR-04                      3        -    
              29-MAR-04 to 31-Mar-04                      3        -
          01-Apr-2004 to 31-Mar-2005     
          01-Apr-2005 to 31-Mar-2006  
          01-Apr-2006 to 31-Mar-2007     
Grand TOTAL                                       I hope everything would be clear this time too.
Oscar, I am sorry for taking alot of your precious time. But I badly need to have this query in order to have the results for my huge data analysis.
Kind regards
Ramis

Similar Messages

  • Findig dates between two dates

    Hi everybody,
    I have one table (TEST) with 2 columns having only one record
    i need list of dates between from_date and to_date
    the sample data...
    from_date to_date
    ====== ======
    02-JUL-09 17-JUL-2009
    I found below query to retrieve the dates between two dates.
    select to_date('02-jul-2009','dd-mon-yyyy')+level-1 dt from dual
    connect by level<=to_date('17-jul-2009','dd-mon-yyyy')-to_date('02-jul-2009','dd-mon-yyyy')+1
    It is working properly.
    i have changed the above query with my table column names
    select to_date(from_date,'dd-mon-yyyy')+level-1 dt from Test
    connect by level<=to_date(to_date,'dd-mon-yyyy')-to_date(from_date,'dd-mon-yyyy')+1
    In this case it is not working...
    i am unable to find the reason...
    Thank you.

    Hi,
    It is working fine for me, what is the data type for from_date and to_date.
    select to_date(from_date,'dd-mon-yyyy')+level-1 dt from Test
    connect by level<=to_date(to_date,'dd-mon-yyyy')-to_date(from_date,'dd-mon-yyyy')+1;
    DT
    02-JUL-09
    03-JUL-09
    04-JUL-09
    05-JUL-09
    06-JUL-09
    07-JUL-09
    08-JUL-09
    09-JUL-09
    10-JUL-09
    11-JUL-09
    12-JUL-09
    13-JUL-09
    14-JUL-09
    15-JUL-09
    16-JUL-09
    17-JUL-09Regards
    Anurag Tibrewal
    PS: Also post your output with the second query.

  • Dates between two dates

    HI everyone!
    I'm trying to obtain the dates between two given dates....
    example:
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date a = sdf.parse("12/10/2005");
    Date b = sdf.parse("22/12/2005");My desired result its a ArrayList of all dates between that period...
    I already used the GregorianCalendar adding one day in a while condition until its equals the second date but it dosn't work for large periods of time...
    Can somebody help me with this task?
    thanks in advance !

    while(gc.getTime().compareTo(finaldate ) != 0){No, that's still fragile. You're still comparing EXACT values, instead of checking for when one equals OR EXCEEDS the other.
    If you're not going to use DrLaszlo's example to use the Calendar class for a better way, then at least do this instead:
    while(gc.getTime().compareTo(finaldate ) < 0){                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • How to get the data between two dates?

    Hi all,
    I am facing a problem of getting the data betwwen two dates.
    I have written a query as
    "select * from table1 a where a.dt between trunc(sysdate) and trunc(sysdate)-30"
    It is not showing any errors or warnings.But the data is not coming though the data is in the table1 between these two dates.
    Regards,
    Kumar

    Your ordering is probably wrong:
    SELECT *
      FROM table1 a
    WHERE a.dt BETWEEN TRUNC (SYSDATE) - 30 AND TRUNC (SYSDATE)

  • Occurrence of a date between two dates

    I want to find occurence of a particular date between two given dates.
    for example if I have 01/03/2006 and 01/01/2007 and I want to find
    if an 06th April exist between them, how do i find it?Pls help.. it's very urgent.
    Thnx
    RK

    Pls help.. it's very urgent.I hate this. If you started your homework a couple days before it was due, you wouldn't have this problem
    Anyway see
    Date.getTime()
    and
    Calendar.getTimeInMillis()
    If you've only got a String date, see SimpleDateFormat
    Once you have them all in long values, just compare the numbers to make sure the new date is > one of them and < the other

  • SQL query to get dates between two dates

    Hi,
    We have one table with start date and end date...Now i need to get all dates between start date and end date...
    Table looks like below..
    Create table date_table (start_date date,end_date date);
    Data in the table will be like below:
    start_date            end_date
    01-Jan-2013       04-Jan-2013
    01-Feb-2013       03-Feb-2013
    01-May-2013        03-May-2013
    I want a result as below..
    holiday_dates
    01-Jan-2013
    02-Jan-2013
    03-Jan-2013
    04-Jan-2013
    01-Feb-2013
    02-Feb-2013
    03-Feb-2013
    01-May-2013
    02-May-2013
    03-May-2013
    Can any one helps..?

    select * from date_table;
    START_DAT END_DATE
    01-JAN-13 04-JAN-13
    01-FEB-13 07-FEB-13
    07-MAR-13 12-MAR-13
    select start_date-1+level output from
    date_table connect by level<=end_date-start_date+1
    and prior start_date=start_date and prior sys_guid() is not null
    OUTPUT
    01-JAN-13
    02-JAN-13
    03-JAN-13
    04-JAN-13
    01-FEB-13
    02-FEB-13
    03-FEB-13
    04-FEB-13
    05-FEB-13
    06-FEB-13
    07-FEB-13
    07-MAR-13
    08-MAR-13
    09-MAR-13
    10-MAR-13
    11-MAR-13
    12-MAR-13

  • How to get the dates between Two dates excluding Saturaday and Sunday

    Dear All,
    select to_date('25-04-2012', 'DD-MM-YYYY') + rownum -1 dt
        from dual
        connect by level <= to_date('05-05-2012', 'DD-MM-YYYY') - to_date('25-04-2012', 'DD-MM-YYYY') + 1;The above query returning the following output,
    DT
    DT
    04/25/2012
    04/26/2012
    04/27/2012
    04/28/2012
    04/29/2012
    04/30/2012
    05/01/2012
    05/02/2012
    05/03/2012
    05/04/2012
    05/05/2012here I need to exclude the Dates which comes on 'saturday' and 'sunday' and also the common holiday
    Here it is '01-May-2012' and I need the output like the following,
    04/25/2012
    04/26/2012
    04/27/2012
    04/30/2012
    05/02/2012
    05/03/2012
    05/04/2012I need the common query to calculate between any two dates.
    Can anyone suggest me?
    Thank you,
    Regsrds,
    gurujothi

    Hi Frank,
    Sorry for my fault,
    The following is my table description,
    CREATE TABLE  "DATES"
       (     "FROMDATE" DATE,
         "TODATE" DATE,
                       "LEAVE_ID" Number(5)
    Insert into dates values('05-02-2012','05-05-2012',1);
    Create table holiday_dates(holidays date);
    insert into holiday_dates values('01-05-2012');Now when I used this query,
    select count(*) from (select dt
    from(
        select to_date(fromdate, 'DD-MM-YYYY') + rownum -1 dt
            from dates
            connect by level <= to_date(todate, 'DD-MM-YYYY') - to_date(fromdate, 'DD-MM-YYYY') + 1
    where to_char(dt,'fmday') not in ('sunday','saturday') minus (select holidays from holiday_dates)) dual;
    Count(*)
       64
    Insert into dates values('01/05/2012','05/05/2012',2);
    /Now my table has 2 rows,
    select  *  from dates;
    FROMDATE     TODATE         LEAVE_ID
    01/05/2012     05/05/2012            1
    05/02/2012     05/05/2012            2Now when I used this query,
    select count(*) from (select dt
    from(
        select to_date(fromdate, 'DD-MM-YYYY') + rownum -1 dt
            from dates
            connect by level <= to_date(todate, 'DD-MM-YYYY') - to_date(fromdate, 'DD-MM-YYYY') + 1
    where to_char(dt,'fmday') not in ('sunday','saturday') minus (select holidays from holiday_dates)) dual;The output is ,
    COUNT(*)
    1987How to get the output using the max(leave_id) like the following,
    select..........from... where leave_id=(select max(leave_id) from dates);where to add "WHERE" clause in the above query?
    Thank you,
    Regards,
    Gurujothi.
    Edited by: Gurujothi on May 3, 2012 8:43 PM
    Edited by: Gurujothi on May 3, 2012 8:44 PM

  • Sql Query for getting data between two dates

    Dear Sir,
    I have one problem, i have date stored in varchar datatype of (X) table in mysql as it was not supporting the date format of (dd/mm/yyyy) so know the problem is,
    When i going to get the data between the the two dates it is giving the un reliable answers such that
    when i am asking the query as data between 1st April to 31st December 2009 it is showing the data from the 2008 .
    is there any way to solve this problem,please help this is an urgent requirement, your replay will be helpful to me.
    With Regards
    Ramakrishna Y

    You either have a SQL problem and this is the incorrect place to ask.
    Or you have a Java problem in code we cannot see. When posting code use the code tags. Click the CODE button and paste your code between the tags that appear.

  • How to get list of all the dates between two dates

    Hi, Can anybody please help me ..
    I have two dates in string format ("dd/MM/yyyy).I need to get all the dates in between these two dates.How can I do thin in java
    Thanks in advance

    Look at classes Calendar and SimpleDateFormat.
    And get your abstraction straight: you don't have two dates. You have two Strings that represent a date. To use them, you have to convert them to Date objects first with SDF.

  • SQL Query between two dates

    Hi,
    Please could someone help me on how to write a query to retrieve the data between two dates.
    created_date format stored in the database column: 9/18/2007 11:34:03 AM
    I tried below but it didn't work
    select * from work_table where created_date beween '9/18/2007' and '03/29/2008'
    I get 'literal doesn't match format string'
    Thanks,

    date datatype is nls dependent -> folllows the nls_date_format database parameter setting inherited by your session.
    Making it short you'll be always safe if you
    select * from work_table where created_date beween to_date('9/18/2007','MM/DD/YYYY') and to_date('03/29/2008','MM/DD/YYYY')Having the time component included you must add + 1 - 1 / 24 / 60 / 60 (plus one day minus one second) to the upper limit if you want to include the last day as a whole
    *** not tested
    Regards
    Etbin

  • How to calculate the month difference between two date char. in Query?

    Customers would like to see how many months passed between two date type of characteristics (e.g., the month difference between the current date and the scheduled delivery date in the record) and put the result into the column as KF. 
    We would have to grab the fiscal year/period kind of value and then do the subtraction, e.g., if the current date value is 2/28/2008 and the scheduled delivery date value in the record is 12/01/2007, the correct result should be 2 month difference between these two date values, but could someone here give us the technical light on how to make this happen in query design?
    Thanks and we will give you reward points for the correct anwsers!

    Hi Kevin,
    The Badi is RSR_OLAP_BADI.
    You can create an implementation using Transaction  SE18.
    The implementation is per cube and is defined in the filters.
    In the Implementation you have the following methods :
    1. Define : Here you will provide the Keyfigure you need as a virtual one.
    2. Initilialize : Any Init Function you want to do.
    3. Compute. This is called per datarecord and here you can cimpute your value.
    Hope this helps.
    Pralay Ahluwalia

  • Find the difference between two dates for the specific month and year

    Hi,
    I have two dates, start date is 30/12/2012 and end date is 04/01/2013. Using datediff I found the difference of days between two dates. But I find the no of days in January 2013. ie output is 4 instead of 6. I input month and year to find the no of days
    for that date. In this case I input Jan 2013. How can I sql this ?

    I don't understand how most of the answers provided here not analytically solving the problem with many cases possible.
    First let me understand you:
    You have 2 dates range and you want to calculate day range for specific month and year between the original date range.
    declare @for_month int = 1 --January
    declare @for_year int = 2013
    declare @StartDate date = '2012-12-20'
    declare @EndDate date = '2013-01-04'
    SELECT
    CASE
    WHEN (DATEPART(MONTH, @StartDate) = @for_month and DATEPART(MONTH, @EndDate) = @for_month) and ((DATEPART(YEAR, @StartDate) = @for_year or DATEPART(YEAR, @EndDate) = @for_year)) THEN
    DATEDIFF(DAY, @StartDate,@EndDate)
    WHEN (@StartDate < cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (@EndDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
    DATEDIFF(DAY, DATEADD(MONTH, DATEDIFF(MONTH, -1, @EndDate)-1, 0),@EndDate)
    WHEN (@EndDate > cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) and (@StartDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
    DATEDIFF(DAY, @StartDate,DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @StartDate) + 1, 0))) + 1
    WHEN ((DATEDIFF(DAY, @StartDate, cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) >= 0) and (DATEDIFF(DAY, cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date), @EndDate) >= 0)) THEN
    DATEDIFF(DAY, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime), DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime)) + 1, 0))) + 1
    ELSE
    0
    END as [DD]
    I don't know how you calculate day range between 01/01/2013 and 04/01/2013
    is 4, it is actually is 3 but if that is the case, you can add 1 from the condition.

  • How to Calculate number of months between two dates

    Hi All,
       In one of the fomr developments, I have to calculate the
    Number of Days
    Number of Months ( Considering Leap Year) provided by the dates, end user enters in the form,
    After going thorugh some forum discussion, I have come to know about so many things which were not clear till now.
    I have gone through various forums too,  some one suggets to make use of FORM CALC and some other JAVA SCRIPT. But the logic i want to build in java script.
    The most interesting point is the DATE object is not getting created when i write  the below code
      var startDate = new DATE(oYear, oMonth, oDay);
    I am still not clear, that really the date object gets created in Adobe form If so the why the alert box is getting populated when i write below lines
    var oTemp = startDate.getFullYear();
    xfa.host.messagebox(oTemp);
    So, there are so many unclear things,
    If any one can help me by suggesting the approach and how to build the logic in the JavaScript I would be really thankful
    Regards
    PavanChand

    Hi,
    ChakravarthyDBA wrote:
    Hi
    I want number of Sundays between two dates
    example
    number of Sundays count between '01-04-2013' and '30-04-2013' in one select query I have to include this as sub query in my select statement.Here's one way:
    SELECT       early_date
    ,       late_date
    ,       ( TRUNC (late_date + 1, 'IW')
           - TRUNC (early_date,        'IW')
           ) / 7       AS sundays
    FROM       table_x
    ;This does not depend on your NLS settings.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data.
    Point out where the statment above is getting the wrong results, and explain, using specific examples, how you get the right results from the given data in those places.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}

  • Calculation of MONTHS , YEARS , & DAYS between two dates

    Hi Experts,
      Currently i am using this functional Module. which calculates the number of months between two dates.
       CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
             EXPORTING
                  I_DATE_FROM = ITAB_PER-BEGDA
                  I_DATE_TO   = ITAB_PER-ENDDA
             IMPORTING
                  E_MONTHS    = CMONTHS.
      BUT this functional module Rounding off the Month...Say ..if the total numbr of days between from & to date is 105 it is showing as 4 months ...
      <b>Is there any other Functional Module</b> ????which Calculates the EXACT MONTH between two dates...(in the above case the month is expected to be 3 only since it is 105 days only)..
      Kindly help me regarding this....

    lakshimiraja,
    You can use this function module.
    report zrich_0003
           no standard page heading.
    data: duration_in_months type i.
    parameters: s_date type sy-datum,
                e_date type sy-datum.
    call function 'DURATION_DETERMINE'
    exporting
       unit                             = 'MON'
       factory_calendar                 = 'P6'    "  Your factory calendar
    importing
       duration                         = duration_in_months
    changing
       start_date                       = s_date
       end_date                         = e_date
    exceptions
       factory_calendar_not_found       = 1
       date_out_of_calendar_range       = 2
       date_not_valid                   = 3
       unit_conversion_error            = 4
       si_unit_missing                  = 5
       parameters_not_valid             = 6
       others                           = 7.
       write:/ duration_in_months.
    Don't forget to reward if useful...

  • Months between two date

    There two ways of finding months between two dates
    ROUND((Date1 – Date2) / 365.25 * 12,2) and
    ROUND(months_between(date1,date2),2) There is a slight difference between output from these two.
    I think the result from the second statement should be more accurate, confirm?
    Abhishek

    AbSHeik wrote:
    There two ways of finding months between two dates
    ROUND((Date1 – Date2) / 365.25 * 12,2) and
    ROUND(months_between(date1,date2),2) There is a slight difference between output from these two.
    I think the result from the second statement should be more accurate, confirm?
    AbhishekHi Abhishek,
    I also agree with this.
    Coz 365.25 = (365 + (1/4))
    This 0.25 actually the extra year of a Leap Year, distributed equally among 4 years. But, when the 1st formula is evaluated, the calculation might cause difference...
    So as far as Oracle is concerned, MONTHS_BETWEEN should be used.
    Please rectify me if i'm wrong.
    Ranit B.

Maybe you are looking for

  • Tab order of the JTable cell

    Hello there: I noticed on a JTable component, the default transversing order of the cells when pressing tab key is from left to right, row by row. Now if I want the order to be from top to bottom, column by column. How can I do it? Thanks, Sway

  • CC 5.2 questions

    Hi, 1.)We planned to create a local rule set for each project so that their reports would run against both the Global and their Local rule set.  Someone recommended using only 1 Global rule set. Why the 1 Global rule set recommended? 2.) Do you recom

  • IPod Classic synchronises and works in car but otherwise is dead

    I have an iPod Classic 30GB which synchronises with my Mac's iTunes perfectly well, and when I plug it into my car (bmw 1-series with iPod control via satnav) I can play my music. However, when the iPod is not connected to the Mac or car it is unusea

  • How to configure this material type We will treat it as  consumable goods?

    Dear Experts,       How to create new material type which we will treat as  consumable goods? Also, for all purchase, we want to have the GR Non-valuated checked so that the actual cost will not have effect after goods receipt but will be effective w

  • Why is one of column on numbers went missing?

    Hi, I have Numbers 09' on my mac. Recently I have lost one of the column that is in the spread sheet. I want to know how to recover it. Thanks