Count # weeks in a date range...how?

Greetings:
I've a sql statement to find me the begining & ending dates for a date range that must begin on Sunday and end on a Saturday.
SELECT (NEXT_DAY(TO_DATE(p_start_dt, 'MM/DD/YYYY'), 'SATURDAY')) - 6,
(NEXT_DAY(TO_DATE(p_end_dt, 'MM/DD/YYYY'), 'SATURDAY'))
INTO v_Begining_Week_Dt, v_Ending_Week_Dt
FROM DUAL;
Question: With these two dates, how can one determine how many weeks there are within this date range? (I'm looking for a number type return value.)
Thanks in advance for any and all guidance.
With best regards,
Ed in Tampa

Okay...I didn't realize that by substacting the dates will give me a number type. I'm good to go now.

Similar Messages

  • How to count the days between Date Range using OO ABAP?

    hi experts,
            i want to count the days between Date Range using OO ABAP for that  which class and method  can i use?.
    Thanks,
    Mahesh.

    Not sure I understand the requirement for it to be OO, but you could always write your own (i.e. use this):
    REPORT zz_date_diff.
    CLASS date_diff DEFINITION.
      PUBLIC SECTION.
        METHODS diff IMPORTING     i_date_fm TYPE d
                                   i_date_to TYPE d
                     EXPORTING     e_days    TYPE i.
    ENDCLASS."
    CLASS date_diff IMPLEMENTATION.
      METHOD diff.
        e_days = i_date_to - i_date_fm.
      ENDMETHOD."
    ENDCLASS."
    DATA: g_ref TYPE REF TO date_diff,
          g_days  TYPE i,
          g_date_fm  TYPE d VALUE '20080101',
          g_date_to  TYPE d VALUE '20090101'.
    START-OF-SELECTION.
      CREATE OBJECT g_ref.
      CALL METHOD g_ref->diff
        EXPORTING
          i_date_fm = g_date_fm
          i_date_to = g_date_to
        IMPORTING
          e_days    = g_days.
      WRITE g_days.

  • Calculated Date Range in Lookup Column

    I want to have a lookup column on a list called Report Week.  The report week is a date range between Sun and Sat.  I could easily make a column of ranges in a seperate list and then use it as a lookup but I am hoping for something more
    elegant.  What I envision is the user selecting the Date Range lookup in a list item and the date ranges shown are the previous, current and upcoming weeks.  So the dropdown list would look like this:
    09/20/14 - 09/26/14
    09/27/14 - 10/03/14
    10/04/14 - 10/10/14
    Can this be done w/out using .NET?  Thanks in advance.

    more or less, you need three calculated columns (well, you may be able to do it in just the one, but it'll be obnoxious to write the formulas)...
    - start date
      => DateAdd([date field], 0 - (dayofweek([datefield]))
            basically, subtract the day of week from the reference date... this should give you the sunday that starts the week
    - end date
      => DateAdd([date field], 7 - (dayofweek([datefield]))
            basically the same, except this adds the remaining number of days in the week, to give you the saturday that ends the week
    - range (description)
      => concatenate([start date], " - ", [end date])
    you may need to fiddle with the formulas a little bit... run them in excel to tweak them.
    Scott Brickey
    MCTS, MCPD, MCITP
    www.sbrickey.com
    Strategic Data Systems - for all your SharePoint needs

  • Max date data between the input date range.

    Hi
    I need to get the maximum date data's with in the date range.
    Ex: this is sample date but , total records in DB around 2000.
    NAME DATE Product1 Product2
    aaaa 01/05/2011 5 5
    aaaa 03/05/2011 3 3
    bbbb 04/05/2011 6 6
    bbbb 07/05/2011 2 2
    aaaa 06/05/2011 1 1
    case :1) If the user input date is 01/05/2011 to 05/05/2011
    then the result should be
    aaaa 3 3
    bbbb 6 6
    case :2) If the user input date is 01/05/2011 to 10/05/2011
    then the result should be
    aaaa 1 1
    bbbb 2 2
    so my result is purely based on max date between the input date range.
    how to achieve the result

    Dhiva wrote:
    Hi
    I need to get the maximum date data's with in the date range.
    Ex: this is sample date but , total records in DB around 2000.
    NAME DATE Product1 Product2
    aaaa 01/05/2011 5 5
    aaaa 03/05/2011 3 3
    bbbb 04/05/2011 6 6
    bbbb 07/05/2011 2 2
    aaaa 06/05/2011 1 1
    case :1) If the user input date is 01/05/2011 to 05/05/2011Jan 05 to May 05
    or
    May 01 to May 05
    what date is 07/08/09?

  • Any FM to get count of each week day for the given date range

    Hi guys,
    Any FM to get count of each week day for the given date range?
    eg: If i give 14/07/2008 to 14/08/2008
    I need to find how many Mondays, tuesdays...sundays in this given date range.
    If not single FM is available, any logic that gives above result is also appreciated.
    Thanks,
    Vinod.

    hi Vinod,
    this is not a full solution, I just give you a basic idea:
    DATA : lv_start TYPE sy-datum VALUE '20080714',
           lv_end   TYPE sy-datum VALUE '20080814'.
    WHILE lv_start LE lv_end.
      CALL FUNCTION 'FTR_DAY_GET_TEXT'
        EXPORTING
          pi_date = lv_start.
    * IMPORTING
    *   PE_DAY_TEXT1       =
    *   PE_DAY_TEXT2       =
    *   PE_DAY             =
    * you have to summarize the output here somehow...
      lv_start = lv_start + 1.
    ENDWHILE.
    hope this helps
    ec

  • How get Mailbox Folder Item Count for date range?

    How to make query to Exchange 2013 like this:
    query ItemCount (specified Mailbox, specified Folder (with subfolders), specified Date Range)?
    I find this script for Exchange 2010: http://gsexdev.blogspot.ru/2012/10/item-age-sample-one-reporting-on-item.html
    This script dont work for Exchange 2013 ((

    I believe you are making the change on both the places in code, as this path version would be different in 2013
    C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"
    Cheers,
    Gulab Prasad
    Technology Consultant
    Blog:
    http://www.exchangeranger.com    Twitter:
      LinkedIn:
       Check out CodeTwo’s tools for Exchange admins
    Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

  • How to calculate a week & Month in given date range (not for sele-options)

    Hi ,
      I have defined 2 date parameters in sel-screen (Plz remember that date variable are not a SELECT-OPTIONS).  Now i want ot display week nos & monts in output.
          Ex: date1: 20080101 & date2: 20080229. then
                    weeks : 1, 2, 3,---9.
                    months: jan-08, feb-08.
    Plz help me with block of code or any FM.
    Regards,

    Hi Srikanth,
            The FM HR_99S_INTERVAL_BETWEEN_DATES   is doesn't existing, but there is a fm HR_MX_INTERVAL_BETWEEN_DATES but it returns no of Years & Days.
           But i found some FM which r returns no of months for given date range. but my requirement is, want to display the month no bw 1 to 12. (ex: dat1=15-03-2008 & dat2= 01-06-2008 then in month fields 03,04,05 & 06. ).
    Plz help me on this.
    -Regards.

  • Count days in a month for a date range

    i am trying to find no. of days between 2 Date Ranges for a list of Ids. i used the logic in the below link:
    count days of the month
    My query is giving duplicates since, I have list of Ids.
    Doctor_ID     Patient_ID     ARRIVE_DT_TM     DISCH_DT_TM
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45
    763305     42972232     1/7/2013 20:27     3/15/2013 19:15
    25391509     42972298     2/4/2013 22:45     3/8/2013 22:03
    746779     42972331     1/4/2013 23:00     3/26/2013 21:50
    763305     42972338     3/4/2013 22:19     3/6/2013 19:35
    763305     42972411     11/4/2013 22:32     3/29/2013 17:30
    I am looking for query to give me for Patient_ID = 42972229
    MONTH     COUNT_DAYS
    201210     14
    201211     30
    201212     31
    201301     31
    201302     28
    201303     14
    I am running the following code and it loops through the months and gives duplicates when I remove where Patient_id IN (42972229)
    select
    Doctor_ID
    , Patient_ID
    , AR_DTTM
    , DSC_DTTM
    , TO_CHAR(ADD_MONTHS(TRUNC(date1, 'MONTH'), LEVEL - 1), 'YYYY MM') MONTHS_BET
    , (LEAST(date2, ADD_MONTHS(TRUNC(date1, 'MONTH') - 1, LEVEL)) - GREATEST(date1, ADD_MONTHS(TRUNC(date1, 'MONTH'), LEVEL - 1)))+ 1 AS DAYSCOUNT
    from (select
    Doctor_ID
    , Patient_ID
    , ARRIVE_DT_TM AR_DTTM
    , DISCH_DT_TM DSC_DTTM
    ,TRUNC(ARRIVE_DT_TM,'DDD') AS date1
    ,TRUNC(DISCH_DT_TM,'DDD') AS date2
    from temp where Patient_id IN (42972229)
    CONNECT BY LEVEL <= MONTHS_BETWEEN(TRUNC(date2, 'MONTH'), TRUNC(date1, 'MONTH')) + 1
    Please help!

    Hi,
    ASTRA_007 wrote:
    Results I would like to see are:
    Doctor_ID     Patient_ID     ARRIVE_DT_TM     DISCH_DT_TM     Month     CountofDays
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45     2012 10     14
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45     2012 11     30
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45     2012 12     31
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45     2013 01     31
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45     2013 02     28
    755722     42972229     10/18/2012 7:50     3/14/2013 20:45     2013 03     14
    763305     42972232     1/7/2013 20:27     3/15/2013 19:15     2013 01     25
    763305     42972232     1/7/2013 20:27     3/15/2013 19:15     2013 02     28
    763305     42972232     1/7/2013 20:27     3/15/2013 19:15     2013 03     15
    and so on...So each row represents a patient-month, and you want to display several columns from the temp table on each output row. In that case, include all those columns in both the SELECT and GROUP BY clauses, like this:
    WITH     universe     AS
         SELECT     *
         FROM     temp
    --     WHERE     patient_id     IN (42972229)
    ,     date_range     AS
         SELECT     TRUNC (MIN (arrive_dt_tm))     AS first_date
         ,     TRUNC (MAX (disch_dt_tm))     AS last_date
         FROM     universe
    ,     all_dates     AS
         SELECT     first_date + LEVEL - 1     AS a_date
         FROM     date_range
         CONNECT BY     LEVEL     <= (last_date + 1) - first_date
    SELECT    u.doctor_id
    ,       u.patient_id
    ,       u.arrive_dt_tm
    ,       u.disch_dt_tm
    ,       TO_CHAR ( TRUNC (a.a_date, 'MONTH')
                  , 'YYYY MM'
                )          AS month
    ,       COUNT (*)          AS count_days
    FROM       all_dates  a
    JOIN       universe   u  ON  a.a_date  BETWEEN  TRUNC (u.arrive_dt_tm)
                                         AND      u.disch_dt_tm
    GROUP BY  u.doctor_id
    ,       u.patient_id
    ,       u.arrive_dt_tm
    ,       u.disch_dt_tm
    ,         TRUNC (a.a_date, 'MONTH')
    ORDER BY  u.patient_id
    ,       TRUNC (a.a_date, 'MONTH')
    ;Output from your sample data (with no filtering):
    `DOCTOR_ID PATIENT_ID ARRIVE_DT_TM     DISCH_DT_TM      MONTH   COUNT_DAYS
        755722   42972229 10/18/2012 7:50  3/14/2013 20:45  2012 10         14
        755722   42972229 10/18/2012 7:50  3/14/2013 20:45  2012 11         30
        755722   42972229 10/18/2012 7:50  3/14/2013 20:45  2012 12         31
        755722   42972229 10/18/2012 7:50  3/14/2013 20:45  2013 01         31
        755722   42972229 10/18/2012 7:50  3/14/2013 20:45  2013 02         28
        755722   42972229 10/18/2012 7:50  3/14/2013 20:45  2013 03         14
        763305   42972232 1/7/2013 20:27   3/15/2013 19:15  2013 01         25
        763305   42972232 1/7/2013 20:27   3/15/2013 19:15  2013 02         28
        763305   42972232 1/7/2013 20:27   3/15/2013 19:15  2013 03         15
      25391509   42972298 2/4/2013 22:45   3/8/2013 22:3    2013 02         25
      25391509   42972298 2/4/2013 22:45   3/8/2013 22:3    2013 03          8
        746779   42972331 1/4/2013 23:0    3/26/2013 21:50  2013 01         28
        746779   42972331 1/4/2013 23:0    3/26/2013 21:50  2013 02         28
        746779   42972331 1/4/2013 23:0    3/26/2013 21:50  2013 03         26
        763305   42972338 3/4/2013 22:19   3/6/2013 19:35   2013 03          3
    In the end the objective is to count the no. of days in each month between the arrival and discharge dates by Physician and for his/her patients.Then is the output above really what you want? Say you're interested in physician 763305. That physician had 18-patient days in March, 2013, but the output doesn't make it clear.
    I ran your query, it works great but I have a long list of patients for whom I have to run these counts.the query above includes all patient_ids.
    That's a separate problem, to be solved in the first sub-query, universe. The rest of the query will be unchanged.
    How will you know which patients to include? If you can derive the list from temp itself, just use a WHERE clause in universe. If you need to look at other tables, join them in universe, or use them in sub-queries in universe, or both.
    For exmple, if you decide that the list of patient_ids has no pattern, and that you'll need to store their ids in a separate table (perhaps a global temporary table), then universe might be:
    WITH     universe     AS
         SELECT     t.*     -- or list columns needed
         FROM     temp                        t
         JOIN     patient_ids_to_include  p 
                      ON  p.patient_id = t.patient_id
    ) ...The rest of the query can be the same as above.
    If a same patient is admitted again then Patient_ID will be different no matter when readmitted.Are you saying that patient_id identifies a visit, not a patient, and that the same person is assigned a different patient_id every time that person is admitted?
    For
    INSERT INTO temp (doctor_id, patient_id, arrive_dt_tm, disch_dt_tm)
    VALUES ( 755722
    , 42972229
    , TO_DATE ('03/14/2013 23:00', 'MM/DD/YYYY HH24:MI')
    , TO_DATE ('04/01/2013 12:00', 'MM/DD/YYYY HH24:MI')
    First the Patient ID will be different from the earlier admission. Second the results will show like:
    Doctor_ID     Patient_ID     ARRIVE_DT_TM     DISCH_DT_TM     Month     CountofDays
    755722     42972229     3/14/2013 23:00     4/1/2013 12:00     2012 03     14
    755722     42972229     3/14/2013 23:00     4/1/2013 12:00     2012 04     1Are you saying that temp.patient_id is unique, and so the situation is impossible?
    Edited by: Frank Kulash on May 7, 2013 10:23 AM

  • SQL DISTINCT COUNT IN DATE RANGE

    Dear All
    Thanks for your attention.
    A table is storing the task summary of each order
    e.g.
    Date                            | Order Number    | Task Number
    2015-01-01 00:00:01   | ABC123456        | JOB001
    2015-01-01 00:01:02   | ABC123456        | JOB002
    2015-01-01 00:10:02   | ABC123444        | JOB001
    2015-01-01 10:12:59   | ABC123456        | JOB002   (Since the job002 is not done correctly, need to work again)
    2015-01-01 18:20:05   | ABC888888        | JOB001
    2015-01-01 20:22:42   | ABC789456        | JOB001
    2015-01-01 21:02:11   | BBB121212        | JOB002
    I would like to write a single query that get the distinct count of order number in sepcific date range
    result as expected in three columns:
    2015-01-01
    task_number            | AM | PM
    JOB001         | 2    | 2
    JOB002         | 1    | 1
    explain the figures here:
    JOB001 AM = 2 (ABC123456, ABC123444)
    JOB002 AM = 1  (two records of ABC123456 count as 1 since we would like to know the number of orders)
    JOB001 PM = 2  (ABC888888, ABC789456)
    JOB002 PM = 1  (BBB121212)
    I wrote a query get similar result but the count of order is not distinct
    SELECT task_number,
    AM=SUM(CASE WHEN date >= ('2015-01-01 00:00:00') AND date < ('2015-01-01 12:00:00') THEN 1 ELSE 0 END),
    PM=SUM(CASE WHEN date >= ('2015-01-01 12:00:00') AND date < ('2015-01-02 00:00:00') THEN 1 ELSE 0 END)
    FROM MyTable
    WHERE task_number = 'JOB001'
    Could anyone advise how to enhance this query to let the result become disintct of order number?
    Many Thanks,
    swivan

    Try
    select task_number,
    count(distinct case when datepart(hour, [date]) >=0 and datepart(hour, [date]) < 12 then [OrderNumbers] END) as AM,
    count(distinct case when datepart(hour, [date]) >=12 and datepart(hour, [date]) < 24 then [OrderNumbers] END) as PM FROM myTable
    GROUP BY Task_Number
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles
    Thank you very much,  short and good answer

  • Getting Record Count From Date Range - IDE: PLSQL Developer

    I would like to count the number of member records that fall within a specified date range based on the members effective and expiration dates and their 'elg_code'. I have posted the SQL for some sample data. What I would like to see returned is three columns of counts where the members eff_date exp_date fall within the date range specified by the SQL and have an Elg_code of ' ' (one blank space).
    So, what I would like is all members with elg_code ' ' where there eff_dt and exp_dt range falls within APR 2012, MAY 2012 & JUN 2012. So, based on the sample data I posted, Mark, where his elg_code record is ' ', his eff_dt is 1/1/2011 and his exp_dt is within APR 2012 (4/30/2012). Mark's range falls within APR 2012, but not MAY or JUNE of 2012. Marty would tally for both APR and MAY since his eff_dt is before MAY 2012 and his exp is within MAY 2012. etc..
    Based on the data below, the results should look like:
    APR MAY JUN
    4 3 2
    APR should have FRANK, MARK, MARTY, MARY,
    MAY should have FRANK, MARTY, MARY
    JUN should have FRANK and MARY
    NOAM and JEAN should not show up as their records with elg_code ' ' do not have eff_dt and exp_dt records that fall within APR-JUN 2012.
    So what I tried without success as it appears I have some kind of Cartesian issue (?), is:
    select count(m1.mbr_name) APR,
    count(m2.mbr_name) MAY,
    count(m3.mbr_name) JUN
    from mbr2 m1,
    mbr2 m2,
    mbr2 m3
    where m1.eff_dt < '01-may-2012'
    and m1.exp_dt > '01-apr-2012'
    and m1.elg_code = ' '
    and m2.eff_dt < '01-jun-2012'
    and m2.exp_dt > '01-may-2012'
    and m2.elg_code = ' '
    and m3.eff_dt < '01-jul-2012'
    and m3.exp_dt > '01-jun-2012'
    and m3.elg_code = ' '
    Below is the DML
    Thanks for any assistance!
    create table mbr2 (mbr_name varchar(10), grpid varchar(1), eff_dt date, exp_dt date, elg_code varchar(1))
    commit
    insert into mbr2 values ('MARK', 'A', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('04-30-2012', 'MM-DD-YYYY'), ' ')
    insert into mbr2 values ('MARK', 'A', to_date('05-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
    insert into mbr2 values ('MARTY', 'A', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('05-31-2012', 'MM-DD-YYYY'), ' ')
    insert into mbr2 values ('MARTY', 'A', to_date('06-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
    insert into mbr2 values ('FRANK', 'B', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('06-30-2012', 'MM-DD-YYYY'), ' ')
    insert into mbr2 values ('FRANK', 'B', to_date('07-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
    insert into mbr2 values ('MARY', 'B', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('06-30-2012', 'MM-DD-YYYY'), ' ')
    insert into mbr2 values ('MARY', 'B', to_date('07-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), 'C')
    insert into mbr2 values ('JEAN', 'C', to_date('01-01-2011', 'MM-DD-YYYY'), to_date('07-01-2011', 'MM-DD-YYYY'), ' ')
    insert into mbr2 values ('JEAN', 'C', to_date('07-01-2011', 'MM-DD-YYYY'), to_date('01-01-2012', 'MM-DD-YYYY'), 'C')
    insert into mbr2 values ('NOAM', 'D', to_date('07-01-2012', 'MM-DD-YYYY'), to_date('12-31-2013', 'MM-DD-YYYY'), ' ')
    commit

    Hi,
    Here's one way:
    WITH     all_months     AS
         SELECT     LEVEL                         AS month_num
         ,     ADD_MONTHS (first_month, LEVEL - 1)     AS month_begin
         ,     ADD_MONTHS (first_month, LEVEL)           AS next_month_begin
         FROM     (
                   SELECT  TO_DATE ('04-01-2012', 'MM-DD-YYYY')     AS first_month
                   ,     TO_DATE ('06-01-2012', 'MM-DD-YYYY')     AS last_month
                   FROM     dual
         CONNECT BY     level     <= 1 + MONTHS_BETWEEN (last_month, first_month)
    SELECT    COUNT (CASE WHEN month_num = 1 THEN 1 END)     AS month_1
    ,       COUNT (CASE WHEN month_num = 2 THEN 1 END)     AS month_2
    ,       COUNT (CASE WHEN month_num = 3 THEN 1 END)     AS month_3
    FROM       all_months  a
    JOIN       mbr2        m  ON  a.month_begin       <= m.exp_dt
                            AND a.next_month_begin      >  m.eff_dt
    WHERE     m.elg_code      = ' '
    ;This assumes you know how many months will be in the output. If you want to derive that from the data, or to give the columns meaningful aliases (such as APR-2011 instead of MONTH_1), then you'll need dynamic SQL (to get separate columns for each month) or string aggregation (if you don't mind one big VARCHAR2 column, formatted to look like several columns). See {message:id=3527823}
    If you don't mind modifying the query a little every time you run it, you can hard-code the number of columns and the month names. In the main query, put exactly as many lines as you need, with the alias you want at the end. That is, instead of
    ,       COUNT (CASE WHEN month_num = 2 THEN 1 END)     AS month_2say
    ,       COUNT (CASE WHEN month_num = 2 THEN 1 END)     AS may_2012I'm not sure exactly what the problem was with the query you posted. It looks like this site cut off parts of the query.
    One thing that is definitely a mistake is that you're comparing DATE columns to VARCHAR2 literals. Never compare a DATE to a VARCHAR2; compare DATEs to DATEs (or compare VARCHAR2s to VARCHAR2s). So instead of
    and m1.exp_dt > '01-apr-2012'you should say
    and m1.exp_dt > TO_DATE ('01-apr-2012', 'dd-mon-yyyy')Thanks for posting the CREATE TABLE and INSERT statements; that's very helpful.
    Edited by: Frank Kulash on Jun 26, 2012 3:32 PM

  • Group by week between date range

    Hi,
    i'm having a table for documents. The documents are received from diffeent dates. I've to calculate number of documents received on weekly basis.
    I need an sql to count the number of documents between two given dates grouping by weekly.
    My week starts on Sunday and ends with Saturday.
    I've tried group by with to_char(documentdate,'IW') , but this will take Monday to Sunday as a week & the incomlete weeks i'm not able to calculate. ie : in the below output the date range 1/28/2007 - 1/31/2007 is not a complete week . ( ie saturday to wednesday)
    I need out put should be like this for Date Range: 12/31/2006 to 1/31/2007
    week               count of documents
    12/31/2006 - 1/6/2007 10
    1/7/2007 - 1/13/2007     40
    1/14/2007 - 1/20/2007     30
    1/21/2007 - 1/27/2007     20
    1/28/2007 - 1/31/2007 10
    Please help me to get this.....
    (columns in documents table is documentid and documentdate)

    your're very close with IW. you just need to shift the data to get it to fall within the correct range. btw, you don't need julian dates. it's overkill
    trunc(date+1,'IW') will push the date into the correct week range (IW will put sunday one week earlier than you want, so push it ahead a day before truncating).
    then subtract 1 day from the result to get it to display with sunday's date instead of monday
    trunc(mydate+1, 'IW')-1
    and the week end date is simply trunc(mydate+1, 'IW')_5
    select mydate, to_char(mydate,'Dy') dy,
    trunc(mydate+1,'IW')-1 wk_Start,
    trunc(mydate+1,'IW')+5 wk_end
    from (select sysdate-rownum mydate from dual connect by level < 20)
    order by 1

  • SUM(Case how to use this structure to get average values over date range

    I am using:
    Oracle SQL Developer (3.0.04) Build MAin-04.34 Oracle Database 11g Enterprise Edition 11.2.0.1.0 - 64bit Production
    How do I use the sum function with a case structure inside.
    so I have data that looks like has an ID, date, and value. I am looking to get the 7 day average for the date range of 4/1/2013 thru 4/20/2013
    with t as (
    select 1 ID_Key,to_date('4/1/2013','mm-dd-yyyy') date_val, 10 Value_num from dual union all
    select 1 ID_key,to_date('4/2/2013','mm-dd-yyyy'), 15 from dual union all
    select 1 ID_key,to_date('4/3/2013','mm-dd-yyyy'), 20 from dual union all
    select 1 ID_key,to_date('4/5/2013','mm-dd-yyyy'), 0 from dual union all
    select 1 ID_key,to_date('4/8/2013','mm-dd-yyyy'), 12 from dual union all
    select 1 ID_key,to_date('4/9/2013','mm-dd-yyyy'), 8 from dual union all
    select 1 ID_key,to_date('4/10/2013','mm-dd-yyyy'), 6 from dual union all
    select 1 ID_key,to_date('4/12/2013','mm-dd-yyyy'), 10 from dual union all
    select 1 ID_key,to_date('4/13/2013','mm-dd-yyyy'), 0 from dual union all
    select 1 ID_key,to_date('4/14/2013','mm-dd-yyyy'), 0 from dual union all
    select 1 ID_key,to_date('4/15/2013','mm-dd-yyyy'), 10 from dual union all
    select 1 ID_key,to_date('4/16/2013','mm-dd-yyyy'), 5 from dual union all
    select 1 ID_key,to_date('4/17/2013','mm-dd-yyyy'), 2 from dual union all
    select 1 ID_key,to_date('4/20/2013','mm-dd-yyyy'), 3 from dual union all
    select 2 ID_key,to_date('4/3/2013','mm-dd-yyyy'), 12 from dual union all
    select 2 ID_key,to_date('4/5/2013','mm-dd-yyyy'), 15 from dual union all
    select 2 ID_key,to_date('4/6/2013','mm-dd-yyyy'), 5 from dual union all
    select 2 ID_key,to_date('4/7/2013','mm-dd-yyyy'), 7 from dual union all
    select 2 ID_key,to_date('4/9/2013','mm-dd-yyyy'), 10 from dual union all
    select 2 ID_key,to_date('4/11/2013','mm-dd-yyyy'), 5 from dual union all
    select 2 ID_key,to_date('4/12/2013','mm-dd-yyyy'), 0 from dual union all
    select 2 ID_key,to_date('4/13/2013','mm-dd-yyyy'), 0 from dual union all
    select 2 ID_key,to_date('4/15/2013','mm-dd-yyyy'), 6 from dual union all
    select 2 ID_key,to_date('4/16/2013','mm-dd-yyyy'), 8 from dual union all
    select 2 ID_key,to_date('4/17/2013','mm-dd-yyyy'), 0 from dual union all
    select 2 ID_key,to_date('4/18/2013','mm-dd-yyyy'), 10 from dual union all
    select 2 ID_key,to_date('4/19/2013','mm-dd-yyyy'), 5 from dual
    )**Please let me know if the table does not load.
    I would like to get the 7 day average as long as there is date for that row has enough previous dates, it not then it will return null.
    the results should look like this
    ID_Key      date_val     Value_num     7Day_Avg     7Day_Avg2
    1     4/1/2013     10          null          null
    1     4/2/2013     15          null          null
    1     4/3/2013     20          null          null
    1     4/5/2013     0          null          null
    1     4/8/2013     12          6.71          11.75
    1     4/9/2013     8          5.71          10.00
    1     4/10/2013     6          3.71          6.50
    1     4/12/2013     10          5.14          9.00
    1     4/13/2013     0          5.14          7.20
    1     4/14/2013     0          5.14          6.00
    1     4/15/2013     10          4.86          5.67
    1     4/16/2013     5          4.42          5.17
    1     4/17/2013     2          3.85          4.50
    1     4/20/2013     3          2.86          4.00
    2     4/3/2013     12          null          null
    2     4/5/2013     15          null          null
    2     4/6/2013     5          null          null
    2     4/7/2013     7          5.57          9.75
    2     4/9/2013     10          7.00          9.80
    2     4/11/2013     5          6.00          8.40
    2     4/12/2013     0          3.86          5.40
    2     4/13/2013     0          3.14          4.40
    2     4/15/2013     6          3.00          4.20
    2     4/16/2013     8          2.71          3.80
    2     4/17/2013     0          2.71          3.17
    2     4/18/2013     10          3.43          4.00
    2     4/19/2013     5          4.14          4.83As you may notice, there are gaps in the dates, so the value are then treated as zeros for the 7Day_Avg and then ignored for teh 7Day_Avg2 (not counted as number of days averaged do to no valu_num row)
    I was trying something like this to start, but getting error "missing keyword"
    select
    t.*/,
    sum(
          case
            when date_val between :day2 - 6 and :day2
            then value_num between date_val - 6 and date_val
            else null
            end
            as 7Day_avg
    form tShould I have the case structure outside the sum function?
    Any thoughts??
    Edited by: 1004407 on Jun 7, 2013 11:06 AM

    Hi,
    If you want the average of the last 7 days, including the current day, then then RANGE should be 6 PRECEDING, not 7.
    Try this:
    WITH     got_min_date_val AS
            SELECT  id_key, date_val, value_num
            ,       MIN (date_val) OVER () AS min_date_val
            FROM    t
            WHERE  date_val BETWEEN TO_DATE ('04-01-2013', 'mm-dd-yyyy')
                             AND   TO_DATE ('04-20-2013', 'mm-dd-yyyy')
    SELECT    id_key, date_val, value_num
    ,         CASE
                  WHEN  date_val >= min_date_val + 6
                  THEN  SUM (value_num) OVER ( PARTITION BY  id_key
                                               ORDER BY      date_val
                                               RANGE         6 PRECEDING
                        / 7
              END  AS avg_7_day
    ,         CASE
                  WHEN  date_val >= min_date_val + 6
                  THEN  AVG (value_num) OVER ( PARTITION BY  id_key
                                               ORDER BY      date_val
                                               RANGE         6 PRECEDING
              END   AS avg_7_day_2
    FROM      got_min_date_val
    ORDER BY  id_key
    ,         date_val
    Output:
       ID_KEY DATE_VAL   VALUE_NUM  AVG_7_DAY  AVG_7_DAY_2
             1 01-APR-13         10
             1 02-APR-13         15
             1 03-APR-13         20
             1 05-APR-13          0
             1 08-APR-13         12       6.71        11.75
             1 09-APR-13          8       5.71        10.00
             1 10-APR-13          6       3.71         6.50
             1 12-APR-13         10       5.14         9.00
             1 13-APR-13          0       5.14         7.20
             1 14-APR-13          0       5.14         6.00
             1 15-APR-13         10       4.86         5.67
             1 16-APR-13          5       4.43         5.17
             1 17-APR-13          2       3.86         4.50
             1 20-APR-13          3       2.86         4.00
             2 03-APR-13         12
             2 05-APR-13         15
             2 06-APR-13          5
             2 07-APR-13          7       5.57         9.75
             2 09-APR-13         10       7.00         9.80
             2 11-APR-13          5       6.00         8.40
             2 12-APR-13          0       3.86         5.40
             2 13-APR-13          0       3.14         4.40
             2 15-APR-13          6       3.00         4.20
             2 16-APR-13          8       2.71         3.80
             2 17-APR-13          0       2.71         3.17
             2 18-APR-13         10       3.43         4.00
             2 19-APR-13          5       4.14         4.83
    Message was edited by: FrankKulash
    Sorry; I meant to reply to OP, not to Greg

  • If I give 27 th week of 2007, can I get date range ( any FM is there ?)

    In my selection screen I am giving 27 th week and year 2007,
    I need to know know the date range ( monday to sunday ) for that week .
    For eg. if I give <b>1st</b> week and year <b>2007</b>,
    It should display 01/01/2007 to 07/01/2007 ( like DD/MM/YYYY ).
    is there any function module available to know this ?

    Hi sam ,
    just execute the code by giving date and  see if this works for u ,,
    check with input as 01/01/2007 i hope this will do ..
    parameters : P_DATE LIKE SCAL-DATE.
    DATA : HDATE LIKE SY-DATUM,
           LDATE LIKE SY-DATUM.
    data: WEEKno  LIKE  SCAL-WEEK,
          refmonDAY  LIKE  SY-DATUM,
          REFSUNDAY  LIKE  SY-DATUM.
       CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'
        EXPORTING
          DATE          = p_date
        IMPORTING
          WEEK          = weekno
          MONDAY        = refmonday
          SUNDAY        = refsunday.
          write:/ 'weeknumber', weekno, 
          write:/ 'monday' ,refmonday.
          write:/  'sunday', refsunday.
    see if this works for u ..
    can you can tell me ur input criteria as how u r passing the date if u r query is not completed ..
    regards,
    vijay

  • Getting last-day-of-week dates within certain date range

    Hi all,
    I have the following Challenge:
    I want to know the dates of the last day of all the weeks within a certain range of dates.
    For instance if my range would be 01-jun-2002 - 31-jun-2002
    then the returned days should be (lastday of the week is sunday):
    . 02-jun-2002
    . 09-jun-2002
    . 16-jun-2002
    . 23-jun-2002
    . 30-jun-2002
    I want to accomplish this by only using sql (no pl/sql) in a ora 8.0.x rdbms
    How would I do this? (if it's possible)
    Tia,
    Martin

    Christian's solution returns
    01-JUN-02
    08-JUN-02
    15-JUN-02
    22-JUN-02
    on my system. The first day of the week is dependent on NLS settings.
    SELECT MAX(realdate)
    FROM (
    SELECT TO_CHAR(TO_DATE('31-may-2002','dd-mon-yyyy') + ROWNUM,'IW') weekno,
           TO_DATE('31-may-2002','dd-mon-yyyy') + ROWNUM realdate
    FROM all_objects
    WHERE rownum <= TO_DATE('30-Jun-2002','dd-mon-yyyy') - TO_DATE('01-Jun-2002','dd-mon-yyyy')
    GROUP BY weeknoThe IW format model give ISO standard week numbers and a week always starts on Monday.

  • Display date range with given week no

    Can anyone please suggest on how to display Date range when wek no is given in APEX 4.0.2?
    Example: given week no as 20 then I need to show week_start_date as 7/18/2011 and week_end_date as 7/25/2011 .
    Thanks

    maybe this could help you on the way to a solution:
    with dates as
        ( select dat, to_char(dat+1, 'iw') week
        from (select to_date('01-01-2011', 'DD-MM-YYYY') + rownum dat
        from all_objects)
        where rownum < 1000)
    select week, min(dat), max(dat)
    from dates
    where week = 20
    group by week
    order by week;You have to check which format for week you want to use: WW (first week starts at the 1st of January) IW (ISO week number)
    see also: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:7482139067917

Maybe you are looking for