Assign Month within a date range (by most days in a given month)

I have a begin and end date, sample data as such
select to_date('01-13-12','mm-dd-yy') from_dt,
to_date('02-23-12','mm-dd-yy') to_dt
from dual
union all
select to_date('03-15-2012','mm-dd-yy') from_dt,
to_date('04-16-2012','mm-dd-yy') to_dt
from dual
union all
select to_date('05-13-2012','mm-dd-yy') from_dt,
to_date('07-23-2012','mm-dd-yy') to_dt
from dual
How do I assign a month by the most days in a month within that date range? Sometimes the date range might have the exact same amount of days in a month (like 3/15/2012 has 16 days and 4/16/2012 has 16 days). In this case, I want the earlier month (march).
So from the sample data:
01/13/2012, 02/23/2012, February
03/15/2012, 04/16/2012, March
05/13/2012, 07/23/2012, June
Thanks
Edited by: user4422426 on Mar 1, 2012 5:15 PM

Hi,
Here's one way:
WITH     cntr          AS
     SELECT     LEVEL - 1     AS n
     FROM     (
               SELECT      1 + MAX (to_dt - from_dt)     AS max_day_cnt
               FROM     table_x
     CONNECT BY     LEVEL     <= max_day_cnt
,     got_r_num     AS
     SELECT     x.from_dt, x.to_dt
     ,     TRUNC (x.from_dt + c.n, 'MONTH')     AS month
     ,     count (*)                    AS cnt
     ,     ROW_NUMBER () OVER ( PARTITION BY  from_dt, to_dt
                         ORDER BY        COUNT (*)     DESC
                         ,             TRUNC (x.from_dt + c.n, 'MONTH')
                       )     AS r_num
     FROM       cntr     c
     JOIN       table_x  x  ON  c.n  <= x.to_dt - x.from_dt
     GROUP BY  x.from_dt, x.to_dt
     ,       TRUNC (x.from_dt + c.n, 'MONTH')
SELECT     from_dt, to_dt
,     TO_CHAR (month, 'Mon YYYY')     AS mon
,     cnt
FROM     got_r_num
WHERE     r_num     = 1
;Thanks for posting code to create the same data. Please test your code before you post it: you got the order of arguments to TO_DATE reversed.

Similar Messages

  • Assign values based on data range

    Hi
    I am trying to fix a query that I ahve been left with from a previous developer.
    The basis of it are that I have 2 views, one which is effectively a date calendar with users per date, and the 2nd being values assigned between a 2 date ranges.
    here is sample data
    create table #hours (
    Fee_earner_Code int not null
    ,startdate date not null
    ,enddate date not null
    ,hours decimal (17,2) not null
    insert into #hours values (1132,'2011-06-01','2020-01-01',6.00)
    insert into #hours values (1132,'2011-06-07','2011-06-01',4.00)
    insert into #hours values (1132,'2012-06-09','2011-06-07',12.00)
    insert into #hours values (2345,'2011-06-01','2020-01-01',3.00)
    insert into #hours values (2345,'2011-06-06','2011-06-01',9.00)
    insert into #hours values (2345,'2012-06-10','2011-06-06',5.00)
    create table #dates (Fee_earner_Code int not null
    ,Primary_date date not null
    insert into #dates values (1132,'2011-06-01')
    insert into #dates values (1132,'2011-06-02')
    insert into #dates values (1132,'2011-06-03')
    insert into #dates values (1132,'2011-06-04')
    insert into #dates values (1132,'2011-06-05')
    insert into #dates values (1132,'2011-06-06')
    insert into #dates values (1132,'2011-06-07')
    insert into #dates values (1132,'2011-06-08')
    insert into #dates values (1132,'2011-06-09')
    insert into #dates values (1132,'2011-06-10')
    insert into #dates values (1132,'2011-06-11')
    insert into #dates values (1132,'2011-06-12')
    insert into #dates values (2345,'2011-06-01')
    insert into #dates values (2345,'2011-06-02')
    insert into #dates values (2345,'2011-06-03')
    insert into #dates values (2345,'2011-06-04')
    insert into #dates values (2345,'2011-06-05')
    insert into #dates values (2345,'2011-06-06')
    insert into #dates values (2345,'2011-06-07')
    insert into #dates values (2345,'2011-06-08')
    insert into #dates values (2345,'2011-06-09')
    insert into #dates values (2345,'2011-06-10')
    insert into #dates values (2345,'2011-06-11')
    insert into #dates values (2345,'2011-06-12')
    for the table #hours the end date is alway the previous rows startdate unless it is the 1st row per fee_earner_Code then it is assigned '2020-01-01' (this date can be change as it is just another view)
    I would like to apply the #hours hours value to the appropriate date range in #dates per fee-earner-code
    my expected ouput would be
    1132,2011-06-01,6.00
    1132,2011-06-02,6.00
    1132,2011-06-03,6.00
    1132,2011-06-04,6.00
    1132,2011-06-05,6.00
    1132,2011-06-06,6.00
    1132,2011-06-07,4.00
    1132,2011-06-08,6.00
    1132,2011-06-09,12.00
    1132,2011-06-10,12.00
    1132,2011-06-11,12.00
    1132,2011-06-12,12.00
    2345,2011-06-01,3.00
    2345,2011-06-02,3.00
    2345,2011-06-03,3.00
    2345,2011-06-04,3.00
    2345,2011-06-05,3.00
    2345,2011-06-06,9.00
    2345,2011-06-07,9.00
    2345,2011-06-08,9.00
    2345,2011-06-09,9.00
    2345,2011-06-10,5.00
    2345,2011-06-11,5.00
    2345,2011-06-12,5.00
    Any help would be great

    With SQL Server 2012's Windows functions, this becomes easy. We can use it to fix the data in your hours table:
    DECLARE @hours TABLE (Fee_earner_Code int not null, startdate date not null, enddate date not null, hours decimal (17,2) not null)
    INSERT INTO @hours (Fee_earner_Code, startdate, enddate, hours) VALUES (1132,'2011-06-01','2020-01-01',6.00),(1132,'2011-06-07','2011-06-01',4.00),(1132,'2012-06-09','2011-06-07',12.00),(2345,'2011-06-01','2020-01-01',3.00),(2345,'2011-06-06','2011-06-01',9.00),(2345,'2012-06-10','2011-06-06',5.00)
    DECLARE @dates TABLE (Fee_earner_Code int not null, Primary_date date not null)
    INSERT INTO @dates (Fee_earner_Code, Primary_date) VALUES (1132,'2011-06-01'),(1132,'2011-06-02'),(1132,'2011-06-03'),(1132,'2011-06-04'),(1132,'2011-06-05'),(1132,'2011-06-06'),(1132,'2011-06-07'),(1132,'2011-06-08'),
    (1132,'2011-06-09'),(1132,'2011-06-10'),(1132,'2011-06-11'),(1132,'2011-06-12'),(2345,'2011-06-01'),(2345,'2011-06-02'),(2345,'2011-06-03'),(2345,'2011-06-04'),
    (2345,'2011-06-05'),(2345,'2011-06-06'),(2345,'2011-06-07'),(2345,'2011-06-08'),(2345,'2011-06-09'),(2345,'2011-06-10'),(2345,'2011-06-11'),(2345,'2011-06-12')
    ;WITH fixHours AS (
    SELECT *, DATEADD(DAY,-1,LEAD(startDate) OVER (PARTITION BY Fee_earner_code ORDER BY startDate)) AS realEndDate
    FROM @hours h
    SELECT h.Fee_earner_Code, d.Primary_date, h.hours
    FROM fixHours h
    LEFT OUTER JOIN @dates d
    ON d.Primary_date BETWEEN h.startDate AND realEndDate
    Without it, we need to do a self join to acheive the same end:
    DECLARE @hours TABLE (Fee_earner_Code int not null, startdate date not null, enddate date not null, hours decimal (17,2) not null)
    INSERT INTO @hours (Fee_earner_Code, startdate, enddate, hours) VALUES (1132,'2011-06-01','2020-01-01',6.00),(1132,'2011-06-07','2011-06-01',4.00),(1132,'2012-06-09','2011-06-07',12.00),(2345,'2011-06-01','2020-01-01',3.00),(2345,'2011-06-06','2011-06-01',9.00),(2345,'2012-06-10','2011-06-06',5.00)
    DECLARE @dates TABLE (Fee_earner_Code int not null, Primary_date date not null)
    INSERT INTO @dates (Fee_earner_Code, Primary_date) VALUES (1132,'2011-06-01'),(1132,'2011-06-02'),(1132,'2011-06-03'),(1132,'2011-06-04'),(1132,'2011-06-05'),(1132,'2011-06-06'),(1132,'2011-06-07'),(1132,'2011-06-08'),
    (1132,'2011-06-09'),(1132,'2011-06-10'),(1132,'2011-06-11'),(1132,'2011-06-12'),(2345,'2011-06-01'),(2345,'2011-06-02'),(2345,'2011-06-03'),(2345,'2011-06-04'),
    (2345,'2011-06-05'),(2345,'2011-06-06'),(2345,'2011-06-07'),(2345,'2011-06-08'),(2345,'2011-06-09'),(2345,'2011-06-10'),(2345,'2011-06-11'),(2345,'2011-06-12')
    ;WITH fixHours AS (
    SELECT h.*, DATEADD(DAY,-1,h1.startdate) as realEndDate
    FROM @hours h
    INNER JOIN @hours h1
    ON h.Fee_earner_Code = h1.Fee_earner_Code
    AND h1.startdate = (SELECT MIN(startDate) FROM @hours WHERE h1.Fee_earner_Code = Fee_earner_Code AND h.startdate < startDate)
    SELECT h.Fee_earner_Code, d.Primary_date, h.hours
    FROM fixHours h
    LEFT OUTER JOIN @dates d
    ON d.Primary_date BETWEEN h.startDate AND realEndDate

  • Data between Date Range with Business Days only

    Hi All,
    We have a requirement that we need to retrieve data between a data range, providing From date and To date as input.
    We also have the option to Include Business Days Only through a check box which will be passed to CR 2008 through a report frame work.
    Can some one help me how to display the report data within the Date Range entered and that includes only Business Days.
    Thanks in advance for the help.
    Regards,
    Naresh.

    try this formula. Lets if your date range parameter is {?date} then try this formula
    @startdate:
    if datepart('w',minimum({?date}))=7 then
    minimum({?date})+2
    else if datepart('w',minimum({?date}))=1 then
    minimum({?date})+1
    else
    minimum({?date})
    @enddate
    if datepart('w',maximum({?date}))=7 then
    maximum({?date})+2
    else if datepart('w',maximum({?date}))=1 then
    maximum({?date})+1
    else
    maximum({?date})
    regards,
    Raghavendra

  • How to get next date or date after n th day of a given date ?

    I want to get the date after n th day of a given date.
    Here a date and an integer will be the input.
    suppose 15/12/2007 and 3 are the inputs and the output will be 18/12/2007
    Is there any method to solve this purpose ?
    Plz its urgent !!

    javagalaxy.com wrote:
    I didn't feel this is a home work for them. I feel the OP is trying to learn.Well, that is a part of what homework is. Now, tell me how doing it for them helps them learn? They might run it once, to make sure it does what it is suppossed to do, then simply turn it in. In the process, they haven't learned a thing. Only a very small fraction of posters will study the posted code to figure it out, as evidenced by the fact, that if the code is not 100% cut-n-paste capable for their assignment, they will come back and complain that this or that does not work, or was not done.
    >
    This will be good learning for the OP, why don't you point out what is done badly.
    Did you mean using
    now.add(Calendar.DAY_OF_MONTH, daysToAdd);
    instead of
    now.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH)+daysToAdd);?
    That is the main crux of it, yes, but homework or not, you should actually attempt to catch and handle the exceptions, rather than simply declaring "throws Exception". Handling exceptions in the homework assignments will cause that to become a habit and you will actually do it correctly without thinking about it. Just like constantly declaring "throws Exception" in your homework will also make that a habit, and guarnatee that you will someday also do that when it counts, thereby causing all sorts of problems (usually for coworkers).
    if its something else then please do let us know.
    I can also learn how to do it good :)

  • Sum amount within a date range

    I have 2 tables where I Need to sum up sales amount within a booking date range.
    Below an example to help illustrate this task.
    Table1:
    CustomerID BookingDate (YYYY-MM-DD)
    1 2014-04-29
    1 2014-07-30
    2 2014-03-31
    2 2014-06-30
    Table2:
    CustomerID SalesDate (YYYY-MM-DD) Amount
    1 2014-01-30 20
    1 2014-02-25 30
    1 2014-05-20 100
    1 2014-07-30 200
    1 2014-09-30 80
    2 2014-03-20 50
    Result:
    CustomerID BookingDate (YYYY-MM-DD) Sum(Amount From Table2)
    1 2014-04-29 50 -- SalesDate between 2014-01-01 and 2014-04-29
    1 2014-07-30 300 -- SalesDate between 2014-04-30 and 2014-07-30
    2 2014-03-31 50 -- SalesDate between 2014-01-01 and 2014-03-31
    2 2014-06-30 0 -- SalesDate between 2014-04-01 and 2014-06-30

    Please try this code:
    declare @Table1 table
    (CustomerID int,BookingDate date );
    insert @Table1
    values
    ( 1, '2014-04-29' ),
    (1, '2014-07-30' ),
    (2, '2014-03-31' ),
    (2, '2014-06-30') ;
    declare @Table2 table
    (CustomerID int, SalesDate date, Amount int);
    insert @Table2
    values
    (1,'2014-01-30',20) ,
    (1,'2014-02-25',30) ,
    (1,'2014-05-20',100) ,
    (1,'2014-07-30',200) ,
    (1,'2014-09-30',80) ,
    (2,'2014-03-20',50) ;
    with cte as
    select
    CustomerID,
    BookingDate ,
    row_number() over (partition by CustomerID order by BookingDate ) as rn
    from @Table1 )
    , cte2 as
    select
    T2.CustomerID ,
    isnull(T1.BookingDate, '2014-01-01') as FromDate,
    T2.BookingDate as ToDate
    from cte as T1
    right join cte as T2
    on T1.rn = T2.rn - 1
    and T1.CustomerID = T2.CustomerID
    select
    b.CustomerID ,
    b.ToDate as BookingDate,
    isnull(sum(a.Amount), 0) as [Sum(Amount From Table2)]
    from @Table2 as a
    right join cte2 as b
    on a.CustomerID = b.CustomerID
    and a.SalesDate > b.FromDate
    and a.SalesDate <= b.ToDate
    group by
    b.CustomerID ,
    b.ToDate;
    T-SQL Articles
    T-SQL e-book by TechNet Wiki Community
    T-SQL blog

  • Ms Project 2013 report for resource name, tasks less than 100% complete within a date range I can set each time

    I have seen I can create a report for a resource name with a specified date range, and one that can show me for a resource name any incomplete tasks, but I want to do the following and cant work out how to state it in the report constructor:
    resource name equals - {I want to set a given resource in here}
    % complete is less than 100%
    start - date range is between [today -7 days] and [today +7 days]
    Please can someone show me how this is possible, I know it will be and it is me that has been defeated!!

    workspacedesign,
    Oops, my bad. I missed the point that the Name field for assignment rows will be the task name and that's not what you want. There is also a glitch in the date range filter. So, here's how to fix that.
    First, in the Resource Usage view, copy the Name field to Text1. Now select the resource line and do a fill down for all assignments under that resource. Do that for each resource. This could be automated with VBA, but for now, a manual setup should work
    fine.
    Second, instead of testing for the Name field in the filter, test for the Text1 field.
    Third, change the Finish test to be "less than or equal to".
    Now when you apply that filter, enter beginning date of your range and then the ending date of your range followed by the resource name. You should get the data you want.
    Unfortunately formulas in custom fields are not as flexible in Project as they are in Excel. For example, in Project a formula can only operate on data for that row. Further, even if you use the formula Today() +7 for your filter criteria, the first time
    the filter is used, Project will hard code today's date into the filter. The normal way around both of these shortcomings is to use VBA.
    Hope this helps.
    John

  • 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.

  • Finding duplicates within a date range. SQL help please!!

    I have a table of records and I am trying to query the duplicate emails that appear within a given date range but cant figure it out.
    There records that it returns are not all duplicates withing the given date range.  HELP!!
    Here is my query.
    Thanks in advance.
    SELECT cybTrans.email, cybTrans.trans_id, cybTrans.product_number, cybTrans.*
    FROM cybTrans
    WHERE (((cybTrans.email) In (SELECT [email] FROM [cybTrans] As Tmp GROUP BY [email] HAVING Count(*)>1 ))
    AND ((cybTrans.product_number)='27')
    AND ((cybTrans.appsystemtime)>'03-01-2010')
    AND ((cybTrans.appsystemtime)<'03-05-2010')
    ORDER BY cybTrans.email;

    Yet another method...
    <cfset start_date = DateFormat('01/01/2007',
    'mm/dd/yyyy')>
    <cfset end_date = DateFormat('09/30/2009',
    'mm/dd/yyyy')>
    <cfset start_year = DatePart('yyyy', start_date)>
    <cfset end_year = DatePart('yyyy', end_date)>
    <cfset schoolyear_start = '09/01/'>
    <cfset schoolyear_end = '06/30/'>
    <cfset count = 0>
    <cfloop index="rec" from="#start_year#"
    to="#end_year#">
    <cfset tmp_start = DateFormat('#schoolyear_start##rec#',
    'mm/dd/yyyy')>
    <cfset tmp_end = DateFormat('#schoolyear_end##rec + 1#',
    'mm/dd/yyyy')>
    <cfif DateCompare(tmp_start,start_date) gt -1 and
    DateCompare(tmp_end, end_date) eq -1>
    <cfset count = count + 1>
    </cfif>
    </cfloop>
    <cfoutput>
    <br>There are #count# school year periods between
    #start_date# and #end_date#
    </cfoutput>

  • Obtaining data within a date range in XL Reporter

    Dear All,
    I'm currently working on BS in XL reporter.
    I was trying to extracting data in a date range, setting PER( Code >= @MthFrom Or Code <= @MthTo ) in report default tab.
    However, the result is shown as all data as of current date. E.g. Selection criteria: >=200808, <=200809, the result is shown as from Begining to 200810.
    Please kindly advise.
    Thank you.
    Regards,
    Julie
    Edited by: Julie Wan on Oct 1, 2008 1:10 PM

    Dear Gordon,
    Thank you for your reply.
    However my case is different to your given link.
    In my case, there is a result coming out, but not within the correct range i selected. It shows all instead.
    I tried to set the period range in report default tab,  in column tab of a expanding column, and in cell tab of an individual cell, but none have worked. E.g. PER( Code >= @MthFrom or Code <= @MthTo )
    Regards,
    Julie

  • Report for open invoice value against GR done within a date range

    Dear experts ,
    I need a report  between date ranges for which there is a GR done , but invoice is pending .
    Where can i get this ? 
    Regards
    Anis

    hi,
    You can  check few default PO reports wid proper paramater in it
    or
    Can check table EKBE
    or
    Check PO history in the PO doc
    Or
    Check the ME80FN
    Regards
    Priyanka.P

  • Calculate number of times a name appears within a date range.

    Hello,
    I am actually using the iPad version but I assume the formula will be the same. Say I have a date in column A and a list of names in column B. What would the formula be to calculate the number of times a specific name appears in a date range.
    Thank you

    If C2 = the minimum date of the range
    C3= the maximum date of the range
    and you are looking for John as the name
    =COUNTIFS($A,">="&C2,$A,"<="&C3,B,"john")
    which counts cells where A>=the min date and A<=the max date and B="john"

  • Date Range Within a Date Range

    I'm suffering from a mental block on this one and I'm hoping
    someone here can help. What I need to do is determine how many date
    ranges are between a broader date range. For example a school year
    runs from 9/1 to 6/30 or 180 work days. What I need to do is
    compute the number of 9/1 to 6/30 periods between say 1/1/2007 and
    9/30/2009. The result of this find is used in othere calculations
    in a rather complex report. Any help is appreciated.

    Yet another method...
    <cfset start_date = DateFormat('01/01/2007',
    'mm/dd/yyyy')>
    <cfset end_date = DateFormat('09/30/2009',
    'mm/dd/yyyy')>
    <cfset start_year = DatePart('yyyy', start_date)>
    <cfset end_year = DatePart('yyyy', end_date)>
    <cfset schoolyear_start = '09/01/'>
    <cfset schoolyear_end = '06/30/'>
    <cfset count = 0>
    <cfloop index="rec" from="#start_year#"
    to="#end_year#">
    <cfset tmp_start = DateFormat('#schoolyear_start##rec#',
    'mm/dd/yyyy')>
    <cfset tmp_end = DateFormat('#schoolyear_end##rec + 1#',
    'mm/dd/yyyy')>
    <cfif DateCompare(tmp_start,start_date) gt -1 and
    DateCompare(tmp_end, end_date) eq -1>
    <cfset count = count + 1>
    </cfif>
    </cfloop>
    <cfoutput>
    <br>There are #count# school year periods between
    #start_date# and #end_date#
    </cfoutput>

  • Tax not calculating in MIRO within a date range.

    Hi Experts ,
    We have created a tax code and assign its values in condition types under two separate Key Combination.
    1) Tax Classification : 4% ( Non Validity) , in access sequence exclusive indicator is set aganst it.
    2)  Tax Code : 4% (Valdity - Sept - till 9999), Exclusive indicator is not set against it.
    Issue is that when i create a PO ( at any date) system is picking up the taxes.
    Nom at the time of MIRO , if i post the MIRO before sept. system is allowing me to do so and calculate taxes correctly.
    But when i change the date to Sept the system does not calculate taxes.
    Even though the Tax Classification has more priority in access sequence compared to tax code , i am failed to understand why system is not calculating taxes.
    I have tried deleting conditions in tax code key combinations but still system is not calculating taxes in MIRO,
    Pls guide.
    Regards
    Honey

    Hi Ramesh ,
    i have done the same . Maintained he condition record via FV11. but system is not calculating tax for sept. month in MIRO.
    In PO tax calculation is perfect but in MIRO it is not calculating tax.( ticked calculate tax option also).

  • Populate dates within the date range columns in a table

    Hi ,
    I have a Students table with StudentID, StartDate and EndDate. For one of the requirements, I need to populate all the dates within and includeing the StartDate and EndDate for all the students. Please find the DDL for the source table samples and also below
    samples of Source table columns and how the output should be.
    create table #Students (ID int,startdate date,Enddate date)
    insert into #Students values (1000, '2014-01-01',
    '2014-01-10')
    insert into #Students values (1000, '2014-02-22',
    '2014-02-28')
    insert into #Students values (1001, '2013-07-01',
    '2013-07-12')
    insert into #Students values (1001, '2013-08-01',
    '2013-08-03')
    insert into #Students values (1001, '2014-04-01',
    '2014-04-05')
    --select * from #Students order by id,startdate
    --drop table #students
    Thanks in advance  for your help!

    Hi vskindia,
    A recursive way to achieve the expected output.
    create table #Students (ID int,startdate date,Enddate date)
    insert into #Students values (1000, '2014-01-01',
    '2014-01-10')
    insert into #Students values (1000, '2014-02-22',
    '2014-02-28')
    insert into #Students values (1001, '2013-07-01',
    '2013-07-12')
    insert into #Students values (1001, '2013-08-01',
    '2013-08-03')
    insert into #Students values (1001, '2014-04-01',
    '2014-04-05')
    ;WITH cte AS
    SELECT ID,startdate,Enddate FROM #Students
    UNION ALL
    SELECT ID,DATEADD(DAY,1,startdate) AS startdate,Enddate FROM cte
    WHERE startdate<Enddate
    SELECT id,startdate FROM CTE ORDER BY ID,startdate
    If you have any question, feel free to let me know.
    Eric Zhang
    TechNet Community Support

  • Average for Unit Price within a date range????

    Howdy BI Gurus,
    We're on BI 7.0.
    The client has the following report that I neeed to create in BI:
    It's Determines the Average Costs for Raw Material Every Quarter:
    <b>Item #  |   Receipt Date|   Qty      |    Unit Cost  |   Extended Cost  |   Vendor</b>
    200TU      | 01/23/07        | 47,349   |      .9950      |   47,112.26         |      XYZ
    200TU      | 02/19/07        | 48,796   |      .9870      |   48,552.02         |      XYZ
    200TU      | 04/16/07        | 43,978   |      .9340      |   43,758.11         |      XYZ
    Total Sum  For Qty =  140,123
    Total Sum For Extended Cost = 139,422.39      
    Average  For Unit Cost =   .9720
    1. How do you calculate the <b>Average</b> for the <b>Unit Cost</b>?
    2. How do display the Total Sum ONLY for the <b>Qty</b> &&<b> Extended Cost</b>?
    I tried the following document:
    <b>"How to...Count the Occurances of a Characteristic"</b>
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/7e58e690-0201-0010-fd85-a2f29a41c7af
    ...that was posted on another forum that shows step by step how to create a Counter infoobject and update the transfer rules with a Constant "1" and after creating a CKF in Query Designer and setting up the Exception Aggregation for "Average of All Values" for Ref. Characteristic "Material" to get the average...Unfortunately, it didn't work for me as I just get $0.0. 
    I'm thinking maybe the directions were for the old BW version.
    Any help would be greatly appreciated.
    Best Regards,
    Osman

    This is what I get now when I do an Exception Aggregation on Ref. Char - Material...Look at the last Column titled "Average Unit Price"
    Calendar Day     Material     Individual price in invoice     Invoiced quantity     Extended Cost     Average Unit Price
    10/9/2007     17887          $ 14.00 /EA               7,128 EA          $ 99,792.00     $ 14.00 /EA
    10/22/2007     53093          $ 318.00 /EA               1,920 EA          $ 610,560.00     $ 318.00 /EA
    10/8/2007     4750MT          $ 0.51 /LB               252,000 LB          $ 128,088.00     $ 0.51 /LB
    10/9/2007     4750MT          $ 0.51 /LB               252,000 LB          $ 127,638.00     $ 0.51 /LB
    10/17/2007     G202          $ 59.40 /EA               6 EA               $ 356.40     $ 59.40 /EA
    What I want is grup by Material:
    Calendar Day     Material     Individual price in invoice     Invoiced quantity     Extended Cost     
    10/9/2007     17887          $ 14.00 /EA               7,128 EA          $ 99,792.00     
    Average Individual price in invoice = $14 (Since there is only 1)
    10/22/2007     53093          $ 318.00 /EA               1,920 EA          $ 610,560.00     
    Average Individual price in invoice = $318.00 (Since there is only 1)
    1/8/2007     4750MT          $ 0.51 /LB               252,000 LB          $ 128,520.00     
    2/9/2007     4750MT          $ 0.32 /LB               252,000 LB          $  80,640.00     
    3/10/2007     4750MT          $ 0.24 /LB               252,000 LB          $  60,480.00     
    4/25/2007     4750MT          $ 0.65 /LB               252,000 LB          $ 163,800.00     
    Average Individual price in invoice = (($0.51 + $0.32 + $0.24 + $0.65) / 4) = $0.43
    10/17/2007     G202          $ 59.40 /EA               6 EA               $ 356.40     
    Average Individual price in invoice = $59.40 (Since there is only 1)
    Hi Kartikey,
    I'll try your method tommorrow morning...thank you for your help so far.
    I really appreciate it
    Best Regards,
    Osman
    Message was edited by:
            Osman Baig

Maybe you are looking for