Extracting a count of distinct values between two date ranges over months

Hi All,
I am having a bit of difficulty in figuring out the query to build a list of active campaigns over a date range.
i.e. I have a table with campaign IDs and their start and end date details like this
Campaign_id     Start_date     End_date
        10001     1-Jun-09     31-May-11
        10002     1-Jun-09     23-Jun-11
        30041     21-Aug-09     31-Dec-09
        20005     3-Jun-10     31-May-11
        90021     21-Nov-09     30-Nov-10
        54000     1-Jun-11     1-Dec-12
        35600     1-Mar-10     31-Mar-12 What the above data means is, for eg. the campaign 10001 is active from 1-Jun-09 to 31-May-11 i.e. for 24 months (inclusive of the month Jun-09 and May-11)
What I need to figure out is the counts of active campaigns between a date range and display that active count at a month level (for e.g. lets say we want to see all the campaigns that were active
between the date range '01-JUN-2007' and '30-APR-2012' ). So the partial output would be as seen below. The list would continue till december-2012
Month    Year    Count of active campaigns
Jan    2009    0
Feb    2009    0
Mar    2009    0
Apr    2009    0
May    2009    0
Jun    2009    2
Jul    2009    2
Aug    2009    3
Sep    2009    3
Oct    2009    3
Nov    2009    4
Dec    2009    4
Jan    2010    3
Feb    2010    3
Mar    2010    4
Apr    2010    4
Dec    2012    1 Could anybody please help me with the right query for this.
Thanks a lot for help
Regards
Goldi

set pagesize 40
with tab as
                select 1 id, sysdate -100 start_date, sysdate end_date from dual
                union
                select 1 id, sysdate -200 start_date, sysdate -150 end_date from dual
                union
                select 1 id, sysdate -600 start_date, sysdate - 400 end_date from dual
                union
                select 1 id, sysdate -300 start_date, sysdate - 150 end_date from dual
                union
                select 2 id, sysdate -100 start_date, sysdate-50 end_date from dual
      year_tab as
                    select
                             add_months(min_date, level -1) m
                    from
                            select min(trunc(start_date,'YYYY')) min_date, add_months(max(trunc(end_date,'YYYY')), 12) max_date
                            from tab
                    connect by level <= months_between(max_date, min_date)
select to_char(m,'YYYY') year_,
         to_char(m,'Month') month_,
         nvl(act, 0) act
from   year_tab,
            select m date_,count(*)  act
            from tab, year_tab
            where m between trunc(start_date,'MM') and trunc(end_date,'MM')
            group by m
            ) month_tab
where m = date_(+)
order by m;
YEAR_ MONTH_           ACT
2010  January            0
2010  February           0
2010  March              0
2010  April              0
2010  May                0
2010  June               0
2010  July               0
2010  August             0
2010  September          1
2010  October            1
2010  November           1
2010  December           1
2011  January            1
2011  February           1
2011  March              1
2011  April              0
2011  May                0
2011  June               0
2011  July               1
2011  August             1
2011  September          1
2011  October            2
2011  November           2
2011  December           2
2012  January            2
2012  February           2
2012  March              2
2012  April              1
2012  May                1
2012  June               0
2012  July               0
2012  August             0
2012  September          0
2012  October            0
2012  November           0
2012  December           0
36 rows selected.

Similar Messages

  • Select Between two date ranges from xml file

    Hi ,
    I have a column date_ with datatype VARCHAR2(150) it stores
    data as
    05/19/2010 11:23 AM
    05/20/2010 12:23 PM
    05/22/2010 11:23 AM
    05/25/2010 11:23 AM
    i have to select all the rows between 05/19/2010 and 05/22/2010 how to do that this column is in xml file

    I have a table wit two fields
    Field1 is integer and field2 is xmltype
    in the xmltype i store an xml file
    <ParentNode>
    <Node>
    <Cat>1</Cat>
    <Date>05/19/2010 11:23 AM </Date>
    </Node>
    <Node>
    <Cat>2</Cat>
    <Date>05/20/2010 12:23 PM </Date>
    </Node>
    <Node>
    <Cat>3</Cat>
    <Date>05/22/2010 11:23 AM </Date>
    </Node>
    </Parentnode>
    I am using teh below query to retrive teh result
    SELECT T.Feild1, XML.* FROM Tablename T,
    XMLTable( 'Parentnod/Node' PASSING T.Feild2 COLUMNS Cat NUMBER PATH 'Cat' ,
    DATE_ VARCHAR2(100) PATH 'Date'
    )XML where cat >1;
    now i have to do teh same to select the rows between two date range 05/19/2010 and 05/21/2010
    hope i am able to make teh question simple

  • How to count number of sundays between two dates

    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.

    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}

  • Function module to calculate no of days between two date ranges

    hi experts,
    can some one please suggest a function module that can calculate no of days between specified date range.
    for example : if i enter date range between 26.02.2011 to 20.05.2011, then it should calculate no of days between these dates.
    Moderator message : Basic date questions not allowed. Read forum rules before posting. Thread locked.
    Edited by: Vinod Kumar on May 25, 2011 10:57 AM

    Hi,
    Please search SDN.. there are lots of posts for teh same.
    [http://wiki.sdn.sap.com/wiki/display/ABAP/FunctionModulerelatedonDate+calculations]

  • Count Distinct employees between two dates

    I'm having a headache trying to figure this out and I can't seem to find the correct way of doing it. Any help would be greatly appreciated.
    I'm needing to get a distinct number count of weekly employees by month that have worked or received pay for the pay period that includes the 12th of the month. The employees should only be counted once per month even though they might have worked and gotten paid during the pay period that includes the 12th of the month. I will give below the column names an example of the data.
    emp no_
    00001
    pay type_
    week
    pay period start date_
    01-09-12
    pay period end date_
    01-15-12
    pay date_
    01-12-12
    work date_
    01-11-12

    Hi,
    housetiger77 wrote:
    Thank you for your response. I'm working with Oracle Report Builder 10g. Your front-end tool can be significant, but it's much more important to know which database versions you have.
    There is no version 10f or 10h, so it's kind of silly to say you have 10g. Give your real, full version, e.g. 10.2.0.4.0.
    To find your database version, run
    SELECT  *
    FROM    v$version;The first line of output will have the database version.
    The report will be grouped by month, state, and work location. For each work location it will give me the distinct number of employees who worked or received pay for the pay period which includes the 12th of the month.
    The columns are as follows:
    SELECT
    phy_emp_no,
    phy_date,
    phy_actual_pay_date,
    phy_ppr_period,
    ppr_start_date,
    ppr_end_date
    FROM pyemppayhist, pycompayprd
    where phy_prn_code = ppr_prn_code
    and ppr_year = '2012'
    and phy_tran_code = 'NWHR'
    Below is my data example. I do not know how to put it in here without the rows and columns being all jumbled up. Sorry!That means you haven't read the forum FAQ yet. {message:id=9360002}
    Take a couple of minutes to read it now; it will be a very good use of your time. What you need to do, including how to use \ tags to preserve formatting, is all explained in that message.  (Actually, it's not so inportant that your CREATE TABLE and INSERT statements be formatted, but it's esy to do, and you have to learn how to do it anyway to post your results.)
    When you're ready to continue, post CREATE TABLE and INSERT statements for your sample data, the exact results you want from that data, and an explanation of how you get those results from that data.
    What's wrong with the solutions you've already received?  Point out where they're getting the wrong results from your sample data, and explain how you might get the right results in those places.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How to fetch values between two days of a month

    Hi,
    I have fetch the values which are greater inserted on or after of 16th day of one and any month.
    I am trying with following query for a month of dec :
    select *
    from tablename
    where SUBSTR(TO_DATE(TO_CHAR(colname, 'DD-MON'),'DDMM'),1,6) >= '16-Dec'
    and SUBSTR(TO_DATE(TO_CHAR(colname, 'DD-MON'),'DDMM'),1,6) < '31-Dec'
    but its not giving me the desired result.
    I have to pick the resultset of everyyear.
    Please help me in this matter.

    Thanks a lot guys!!
    My problem is solved.That's good news, but be aware that the solutions presented are not all good:
    SQL> select * from tablename order by colname
      2  /
    COLNAME
    23-12-2005 11:03:12
    28-12-2005 11:03:12
    02-01-2006 11:03:12
    13-11-2006 11:03:12
    13-12-2006 11:03:12
    23-12-2006 11:03:12
    24-12-2006 11:03:12
    25-12-2006 11:03:12
    31-12-2006 11:03:12
    9 rijen zijn geselecteerd.
    SQL> select colname
      2    from tablename
      3   where to_date(to_char(colname,'ddmm') || '2006','ddmmyyyy')
      4         between to_date('16122006','ddmmyyyy')
      5             and to_date('01012007','ddmmyyyy') - 1/24/60/60
      6  /
    COLNAME
    23-12-2006 11:03:12
    24-12-2006 11:03:12
    25-12-2006 11:03:12
    31-12-2006 11:03:12
    23-12-2005 11:03:12
    28-12-2005 11:03:12
    6 rijen zijn geselecteerd.
    SQL> remark Mohana's solution
    SQL> select *
      2  from tablename
      3  where TO_CHAR(colname, 'DD-MON') >= '16-DEC'
      4  and TO_CHAR(colname, 'DD-MON') < '31-DEC'
      5  /
    COLNAME
    23-12-2006 11:03:12
    24-12-2006 11:03:12
    25-12-2006 11:03:12
    23-12-2005 11:03:12
    28-12-2005 11:03:12
    5 rijen zijn geselecteerd.
    SQL> remark user503699's solution
    SQL> select *
      2  from tablename
      3  where TO_CHAR(colname, 'MON') = 'DEC'
      4  and ( TO_NUMBER(TO_CHAR(colname, 'DD')) >= 16
      5  and TO_NUMBER(TO_CHAR(colname, 'DD')) <= 31 )
      6  /
    COLNAME
    23-12-2006 11:03:12
    24-12-2006 11:03:12
    25-12-2006 11:03:12
    31-12-2006 11:03:12
    23-12-2005 11:03:12
    28-12-2005 11:03:12
    6 rijen zijn geselecteerd.
    SQL> remark Rup's solution
    SQL> select *
      2  from tablename
      3  where TO_CHAR(colname, 'DD-MON-yyyy') >= to_date('16-DEC','dd-mon')
      4  and TO_CHAR(colname, 'DD-MON-yyyy') < to_date('31-DEC','dd-MON')
      5  /
    COLNAME
    23-12-2006 11:03:12
    24-12-2006 11:03:12
    25-12-2006 11:03:12
    3 rijen zijn geselecteerd.So use user503699's solution or mine ;-)
    Hope this helps.
    Regards,
    Rob.

  • Calculate business day between two dates

    Hi Guys
    I need the count of business days between two  dates
    Date1, Date2 i need the count only business day (exclude sartuday&sunday)
    If date1 is null or nothing i need to pass 0
    If date2 is null or nothing i need to pass 0
    help on this

    Hi,
    To achive this within SSRS, go to the report code window and add the below
    Function getBusinessDaysCount(ByVal tFrom As Date, ByVal tTo As Date) As Integer
    Dim tCount As Integer
    Dim tProcessDate As Date = tFrom
    For x as Integer= 1 To DateDiff(DateInterval.Day, tFrom, tTo) + 1
    If Not (tProcessDate.DayOfWeek = DayOfWeek.Saturday Or tProcessDate.DayOfWeek = DayOfWeek.Sunday) Then
    tCount = tCount + 1
    End If
    tProcessDate = DateAdd(DateInterval.Day, 1, tProcessDate)
    Next
    Return tCount
    End Function
    In the textbox where you need to display the value, add the below expression
    =Code.getBusinessDaysCount(parameters!StartDate.Value,parameters!EndDate.Value)
    It is assumed the you want to pass the two days from parameters named Startdate and EndDate. If not, modify the expression with required values.
    Regards
    Please click "Mark as Answer" if this resolves your problem or "Vote as Helpful" if you find it helpful. BH

  • Query between two dates

    Hi,
    I have table like this.
    Id           in_date               value
    2          05-Jun-08          5.3
    3          08-Jun-08          5.2
    4          08-Jun-08          5.3
    5          08-Jun-08          5.8
    6          10-Jun-08          7
    7          10-Jun-08          5.6
    8          11-Jun-08          2.6
    When I query for average value between two dates, it should return average and if there is no entry for any dates it should return 0 for that date. Here is sample output for dates between 05-Jun-08 and 12-Jun-08.
    In_date           value
    05-Jun-08     5.3
    06-Jun-08     0.0
    07-Jun-08     0.0
    08-Jun-08     5.43
    09-Jun-08     0
    10-Jun-08     6.3
    11-Jun-08     2.6
    12-Jun-08     0
    Please help me to write query on thisThanks,
    Sujnan

    SQL> with t as (
      2             select 2 id,to_date('05-Jun-08','dd-mon-rr') in_date,5.3 val from dual union all
      3             select 3,to_date('08-Jun-08','dd-mon-rr'),5.2 from dual union all
      4             select 4,to_date('08-Jun-08','dd-mon-rr'),5.3 from dual union all
      5             select 5,to_date('08-Jun-08','dd-mon-rr'),5.8 from dual union all
      6             select 6,to_date('10-Jun-08','dd-mon-rr'),7 from dual union all
      7             select 7,to_date('10-Jun-08','dd-mon-rr'),5.6 from dual union all
      8             select 8,to_date('11-Jun-08','dd-mon-rr'),2.6 from dual
      9            )
    10  select  t2.in_date,
    11          nvl(t1.val,0) + t2.val val
    12    from  (
    13           select  in_date,
    14                   avg(val) val
    15             from  t
    16             group by in_date
    17          ) t1,
    18          (
    19           select  min_in_date + level - 1 in_date,
    20                   0 val
    21             from  (
    22                    select  min(in_date) min_in_date,
    23                            max(in_date) max_in_date
    24                      from  t
    25                   )
    26             connect by level <= max_in_date - min_in_date + 1
    27          ) t2
    28    where t2.in_date = t1.in_date(+)
    29    order by t2.in_date
    30  /
    IN_DATE      VAL
    05-JUN-08   5.30
    06-JUN-08    .00
    07-JUN-08    .00
    08-JUN-08   5.43
    09-JUN-08    .00
    10-JUN-08   6.30
    11-JUN-08   2.60
    7 rows selected.
    SQL> SY.

  • Awk - printing a value between two matching regex [SOLVED]

    I'm trying to write a script to parse a single line output, but to then only display the value between two matching regex.
    I have got it to only display the value between the matching regex, but then it carries on and displays the rest of the line. How do I tell it to stop once the matching value has been printed?
    ARTIST=`awk '/Artist/,/Artist/ {gsub(/Artist/, Artist); print}' /home/sapphire/.foobar2000/track_info`
    This produces 'Akira Yamaoka Title She Title Album Silent Hill 1 OST Album CurrentTime 0:03 CurrentTime TotalTime 2:01 TotalTime' rather then the desired 'Akira Yamaoka'.
    So I guess I'm asking how one would go about terminating the above awk command? Thanks - I know, it'll be something simple - but I'm not experienced with scripting
    Last edited by wyvern (2008-04-10 11:37:30)

    ibendiben wrote:Could you share the file you are extracting the data from?
    No problem, and thank you for the help so far
    This is what's written each time I update my playing track, no more, no less:
    Artist Akira Yamaoka Artist Title Silent Hill Title Album Silent Hill 1 OST Album CurrentTime 0:04 CurrentTime TotalTime 2:51 TotalTime
    Single line only, as the plugin won't break up the text into anything more than the one line

  • Count between two dates.

    dear friends
    i want fetch count between two dates.
    my query is like below.
    SELECT (to_date('19-mar-2012','dd-mm-yyyy')-to_date('01-apr-2012','dd-mm-yyyy')) FROM dual
    its given out like -13 but i want in positive mod like 13 only not '-' Sign.

    just use abs() function to your query to get the positive value
    SELECT abs(to_date('19-mar-2012','dd-mm-yyyy')-to_date('01-apr-2012','dd-mm-yyyy')) diff FROM dual

  • In BADi , How to pass the values between two Method

    Hi Experts,
    We have two methods in BADis. How to pass the value  between two Methods. Can you guys explain me out with one example...
    Thanks & Regards,
    Sivakumar S

    Hi Sivakumar!
    Create a function group.
    Define global data (there is a similiar menu point to jump to the top include).
    Create one or two function modules, with which you can read and write the global data.
    In your BADI methods you can access the global data with help of your function modules. It will stay in memory through the whole transaction.
    Regards,
    Christian

  • Need of SQL query in selecting distinct values from two tables

    hi,
    I need a query for selecting distinct values from two tables with one condition.
    for eg:
    there are two tables a & b.
    in table a there are values like age,sex,name,empno and in table b valuses are such as age,salary,DOJ,empno.
    here what i need is with the help of empno as unique field,i need to select distinct values from two tables (ie) except age.
    can anybody please help me.
    Thanks in advance,
    Ratheesh

    Not sure what you mean either, but perhaps this will start a dialog:
    SELECT DISTINCT a.empno,
                    a.name,
                    a.sex,
                    b.salary,
                    b.doj
    FROM    a,
            b
    WHERE   a.empno = b.empno;Greg

  • Count days between two dates without weekend

    Hi,
    I need a solution in query or another thread, that returns the count of days between two dates without consider weekend (saturday and sunday) , I have the columns of type Date, and the return need in format of hours in one column hh:mm:ss and days in another column.
    Regards
    Jonas

    Hi and welcome to the forum.
    Keep in mind that you can do a search on this forum.
    Your question has been asked before.
    Some other pointers:
    http://asktom.oracle.com/pls/asktom/ASKTOM.download_file?p_file=6551242712657900129
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:185012348071

  • Javascript anomoly on day count between two dates

    Using ApEx 4.0, I have found an anomoly in some javascript code that calculates the number of days between two dates, the current_date and the past_date. If the past-date is on or before March 10, 2013, and the current_date is between March 10, 2013, and November 3, 2013, the day count will be 1 day less than the actual count. Between November 3, 2013, and November 4, 2013, the count increments by 2, and then the count will be accurate from this date forward.
    Here are examples:
    Mar 10, 2013 = 69 days from 31-DEC-2012
    Mar 11, 2013 = 69 days from 31-DEC-2012
    Mar 12, 2013 = 70 days from 31-DEC-2012
    Nov. 3 2013 = 306 days from 31-DEC-2012
    Nov. 4 2013 = 308 days from 31-DEC-2012
    March 11 should be 70, and March 12 should be 71. November 3 should be 307, and November 4 corrects the wrong count which began March 11.
    Changing the past_date to March 10, 2013 produces the following:
    10-Mar-2013 = 0 days from 10-Mar-2013
    11-Mar-2013 = 0 days from 10-Mar-2013
    12-Mar-2013 = 1 days from 10-Mar-2013
    But changing the past_date to March 11, 2013, produces correct numbers:
    11-Mar-2013 = 0 days from 11-Mar-2013
    12-Mar-2013 = 1 days from 11-Mar-2013
    13-Mar-2013 = 2 days from 11-Mar-2013
    I would certainly appreciate anyone's help on identifying the cause of this anomoly. Here is the javascript code:
    var w1= ($v("P48_PAST_DATE"));
    w1 = (w1.toString());
    var vmon = (w1.substr(3,3));
    var vyr = (w1.substr(7));
    var r = (vyr.length);
    if (r == 2)
    vyr = (parseFloat(vyr) + 2000);
    var vday = (w1.substr(0,2));
    var y = (vmon.concat(" ",vday,", ",vyr));
    y = Date.parse(y);
    var w2 = ($v("P48_CURRENT_DATE"));
    var vmon2 = (w2.substr(3,3));
    var vyr2 = (w2.substr(7));
    var vday2 = (w2.substr(0,2));
    var x = (vmon2.concat(" ",vday2,", ",vyr2));
    x = Date.parse(x);
    var numdays = (x - y);
    numdays = (Math.floor(numdays / 86400000));
    $s("P48_NUMBEROFDAYS",numdays);

    Did you google for something like "javascript number of days between two dates". I think you will find the explanation in this observation:
    This method doesn't work properly if there's a daylight savings jump between the two dates.
    There are examples to be found to calculate the difference between two dates.

  • How to count the number of Fridays and Saturdays between two dates

    Hi every one ... If we want to count the number of Fridays and Saturdays between two dates, how would we do that ? !
    Dates are ( 11-Feb-2010) to (19-May-2010)
    how to do it in SQL
    Edited by: khalidoracleit on Jul 28, 2010 5:51 AM

    some nice coding here, I'm still amazed with what some people can do with "connect by". But I agree with some statements here that this can take "time", and to be honest, it's funny to see it working, but if you do not have a computer, just a calendar and some paper, would you go for "counting" so there must be a better solution?
    The best working math in here is done by Aketi Jyuuzou, who writes so good English that I wonder why he still insists that he doesn't ;-)
    Anyhow I "translated" that code to English, and I really like that math. Math is math and data is data.
    ALTER SESSION SET NLS_DATE_LANGUAGE='ENGLISH';
    WITH my_dates AS (
    SELECT to_date('20100211','yyyymmdd') start_date,to_date('20100519','yyyymmdd') end_date FROM DUAL
    UNION ALL
    SELECT to_date('20100211','yyyymmdd') start_date,to_date('20100214','yyyymmdd') end_date FROM DUAL
    UNION ALL
    SELECT to_date('20100211','yyyymmdd') start_date,to_date('20100213','yyyymmdd') end_date FROM DUAL
    UNION ALL
    SELECT to_date('20100211','yyyymmdd') start_date,to_date('20100212','yyyymmdd') end_date FROM DUAL
    SELECT to_char(start_date,'DD.MM.YYYY') start_date,to_char(end_date,'DD.MM.YYYY') end_date,
           to_char(start_date,'DAY') start_weekday,to_char(end_date,'DAY') end_weekday,
           end_date-start_date day_difference,
           (next_day(end_date,'FRIDAY')-7
           -next_day(start_date -1,'FRIDAY'))/7+1
           +(next_day(end_date,'SATURDAY')-7
           -next_day(start_date -1,'SATURDAY'))/7+1 as count_of_fr_and_sat
    FROM my_dates;
    START_DATE END_DATE   START_WEEKDAY                        END_WEEKDAY                          DAY_DIFFERENCE         COUNT_OF_FR_AND_SAT   
    11.02.2010 19.05.2010 THURSDAY                             WEDNESDAY                            97                     28                    
    11.02.2010 14.02.2010 THURSDAY                             SUNDAY                               3                      2                     
    11.02.2010 13.02.2010 THURSDAY                             SATURDAY                             2                      2                     
    11.02.2010 12.02.2010 THURSDAY                             FRIDAY                               1                      1                      -- andy

Maybe you are looking for