How to calculate a rolling 12 months to date calculation?

Hi there
We have a business requirement to calculate a rolling 12 month to date value. That is, for any month, sum up the last 12 months of data. E.g. for June 2011, its May 2010 -> June 2011. We have the standard Year and Period dimension, and the member which holds this calculation is in another dimension called TimeView. TimeView just holds dynamic calculations for Month To Date(MTH), Year To Date (YTD) etc.
The problem I am having is because we have the standard Year and Period dimensions so I need to do a cross dimensional sum. All the solutions I am trying either give me an insufficient dynamic calc cache (increase lock block setting) or something similar. Our lock block setting is quite high and it doesnt seem to make a difference when I increase it.
Anyway, I have tried a few solutions:
Short one:
@SUMRANGE("MTH",@MDSHIFT(@CURRMBRRANGE(Period,LEV,0,1,),-1,"Year",)) + @SUMRANGE("MTH",@CURRMBRRANGE(Period,LEV,0,,0));
Long one:
IF(@ISUDA(ACCOUNT,"Flow"))
     IF(@ISMBR(Jan))
          YTD->Jan + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Jan,-1,"Year",);
     ELSEIF(@ISMBR(Feb))
          YTD->Feb + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Feb,-1,"Year",);
     ELSEIF(@ISMBR(Mar))
          YTD->Mar + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Mar,-1,"Year",);
     ELSEIF(@ISMBR(Apr))
          YTD->Apr + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Apr,-1,"Year",);
     ELSEIF(@ISMBR(May))
          YTD->May + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->May,-1,"Year",);
     ELSEIF(@ISMBR(Jun))
          YTD->Jun + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Jun,-1,"Year",);
     ELSEIF(@ISMBR(Jul))
          YTD->Jul + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Jul,-1,"Year",);
     ELSEIF(@ISMBR(Aug))
          YTD->Aug + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Aug,-1,"Year",);
     ELSEIF(@ISMBR(Sep))
          YTD->Sep + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Sep,-1,"Year",);
     ELSEIF(@ISMBR(Oct))
          YTD->Oct + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Oct,-1,"Year",);
     ELSEIF(@ISMBR(Nov))
          YTD->Nov + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Nov,-1,"Year",);
     ELSEIF(@ISMBR(Dec))
          YTD->Dec
     ELSEIF(@ISMBR(Q1))
          YTD->Mar + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Mar,-1,"Year",);
     ELSEIF(@ISMBR(Q2))
          YTD->Jun + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Jun,-1,"Year",);
     ELSEIF(@ISMBR(Q3))
          YTD->Sep + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Sep,-1,"Year",);
     ELSEIF(@ISMBR(Q4))
          YTD->Dec
     ELSEIF(@ISMBR(H1))
          YTD->Jun + @MDSHIFT(YTD->Dec,-1,"Year",) - @MDSHIFT(YTD->Jun,-1,"Year",);
     ELSEIF(@ISMBR(H2))
          YTD->Dec
     ELSEIF(@ISMBR("YearTotal"))
          YTD->Dec
     ENDIF
ELSEIF(@ISUDA(ACCOUNT,"First"))
     MTH->Jan;
ELSE
MTH;
ENDIF
I am sure this may be a common requirement across companies - does anyone know a solution to this?
Much appreciated!

Closing this thread, just needed to increase cache levels.

Similar Messages

  • How to calculate week number from system date

    Hi experts,
    how to calculate week number from system date  ?
    thanks.
    SK

    hi,
    The above FM can be used for BADI or customer exit, if it is design level then you can use formula as well, under formula date functions you can use DATE_TO_WEEK.
    regards,
    Arvind.

  • How to Calculate last 12 months in webi?

    Hi,
    I am working on a webi report based on oracle. My report table does not have any month field in it. Hence, I created a derived table in the universe for 12 months. Now, in my report I need to calculate last 12 months data. The month and year value will be given by the user and from that date values will be displayed for previous 12 months on a chart.
    Any ideas how achieve this in webi?
    Regards,
    C Mira

    Hi,
    Generally you can get the user values using the UserResponse() function.
    Using this, you can do some basic transformation of the user values for month and year to create a "Year_month_user" variable - e.g. in the format YYYYMM or better yet - create a date variable!
    (using the ToDate() function)
    When this variable is created, you can create a similar variable "Year_month_unv" from the universe objects.
    Then apply a custom filter on your chart with the syntax
    =[Year_month_unv] Between([Year_month_user];RelativeDate([Year_month_user];-365))
    I hope this helps.
    Btw: my first forum post - Yes!
    Edited by: Simon Steiper on Jul 28, 2010 2:25 PM (added the RelativeDate to syntax)

  • How to calculate AGE from two different date fields

    hi
    I need to calculate AGE from two different date fields.
    Can some help me how to do, when i try to do substraction formula it is showing error.
    Thank You
    Manu

    Manu wrote:
    Hi
    Both fields are in date format only, i need to calculate no . of days between two different dates
    Thanks
    ManuThe reason for the question about the format of the column is because the simplest solution only works on DATE columns. Others have mentioned this here, but if you want the difference between two date fields, you can use this formula:
    TIMESTAMPDIFF(SQL_TSI_DAY, date_column1, date_column2)
    The above being said, you still didn't tell me what you did, or what error message you received. So again, if the above formula didn't work, what did you do? Where did you put the formula? What was the exact syntax you used? What was the error message you got?
    Please don't make us work more than we need to. Answer all the questions in your next post. Thanks.

  • How to calculate next 12 months

    Hi Gurus,
    If a user enters a date like : 15.05.2007, I need to calculate next 12 months values.
    i.e. low = 16.05.2007
         high= 15.05.2008.
    Any help greatly appreciated. Thanks.
    Best Regards,
    Reddy.

    data: l_month         type i,
            l_year          type i,
            l_sum_of_months type i.
    data : V_DATE TYPE  D,
             V_MONTHS TYPE  I DEFAULT 0,
             E_DATE TYPE  D.
        l_sum_of_months = v_date+4(2) + v_months.               "YREM981595
        l_month         = ( l_sum_of_months + 1 ) mod 12.       "YREM981595
        if l_month eq 0.
          l_month = 12.
        endif.
        l_year  = l_sum_of_months div 12.
        l_year  = l_year + v_date(4).
        if l_year gt 9999.
          e_date      = con_hidate.
        else.
          e_date+6(2) = '01'.
          e_date+4(2) = l_month.
          e_date(4)   = l_year.
          e_date      = e_date - 1.
        endif.
    rewards if useful,
    regards,
    nazeer

  • How to get the current month starting date and ending date by default ?

    Hello Creators,
    This is my requirement, kindly do the needful, Please dont gimme any links i've no access for other sites, gimme the right solution.
    My Requirement :
    Creation Date (VBAK-ERDAT) - Select Option. By default, the program should run for the current Month ie between the starting and ending date of the current month when the program is run. Today the date is 18th using sy-datum we can bring the current date by calling it in the initialization event, but the requirement is it should not give me the current date, it should only give me the month starting date and the end date. Kindly help.
    Thanks in advance ...

    DATA: gv_datum LIKE vbak-erdat.
    SELECT-OPTIONS: so_date FOR gv_datum.
    INITIALIZATION.
      CALL FUNCTION 'HR_JP_MONTH_BEGIN_END_DATE'
        EXPORTING
          iv_date             = sy-datum
        IMPORTING
          ev_month_begin_date = so_date-low
          ev_month_end_date   = so_date-high.
      so_date-option = 'BT'.
      APPEND so_date.
    START-OF-SELECTION.
    Hope this solves the issue. Don't forget to be generous
    Cheers,
    Sougata.

  • Fiscal Month/Year Date Calculations

    Hi Gurus,
    I am trying to create a fiscal year and fiscal month offset using filter advanced SQL in answers. The “Offset” should allow to define a period of time + or – from a given starting point in order to report on a period of time. For example the Current Month Offset - When the variable is defined and set-up in the “Time” drop down menu in Answers. You would select Current Month Offset and in the create/edit filter dialog the Current Month Offset column contains a value of 0 for the most recent loaded month of data, a value of -1 for the month prior to that and so on. By selecting the “Operator”, “is between” and “Value” -5 and “Value” 0 you would filter to display the last 6 months.
    I created the offset with calendar year using the Advanced filter and worked fine for if i need to change the year to what ever value
    (((YEAR(CURDATE())-YEAR(Time.Date)) * 12) + (MONTH(CURDATE())-MONTH(Time.Date))) * -1 = -1
    The issue comes when i create for Fiscal year and Fiscal month using filter advanced SQL it throws an error
    (((FISCAL YEAR(CURDATE())-FISCALYEAR(Time.Date)) * 12) + (FISCAL MONTH(CURDATE())-FISCAL MONTH(Time.Date))) * -1 = -1
    for fiscal year it throws an error below
    Error getting drill information: SELECT Time."Fiscal Year" saw_0, Time."Fiscal Year" saw_1 FROM "AAFES Procurement - Purchase Orders" WHERE Time."Fiscal Year" = (((FISCAL YEAR(CURDATE())-FISCALYEAR(Time.Date)) * 12) + (FISCAL MONTH(CURDATE())-FISCAL MONTH(Time.Date))) * -1 = -1
    Error Details
    Error Codes: YQCO4T56:OPR4ONWY:U9IM8TAC:OI2DL65P
    Odbc driver returned an error (SQLExecDirectW).
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 27002] Near <CURDATE>: Syntax error [nQSError: 26012] . (HY000)
    SQL Issued: {call NQSGetLevelDrillability('SELECT Time."Fiscal Year" saw_0, Time."Fiscal Year" saw_1 FROM "AAFES Procurement - Purchase Orders" WHERE Time."Fiscal Year" = (((FISCAL YEAR(CURDATE())-FISCALYEAR(Time.Date)) * 12) + (FISCAL MONTH(CURDATE())-FISCAL MONTH(Time.Date))) * -1 = -1')}
    Thanks
    Simmz

    Hi,
    Kindly refer the below link it may help you.
    http://obieeelegant.blogspot.com/2011/06/obiee-date-expressions-reference.html
    Thanks
    Deva

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

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

  • How to calculate a renters move out date?

    Hi all, I have been working on a spreadsheet that helps me to calculate residents rent.
    attached is a screen shot of where I am up to
    You will see that I am trying to make the spreadsheet automate how much is owed depending on when someone has moved in or out.
    It is proving tricky mainly because the billing cycles start on the 15th of the month not the 1st.
    2 problems. In the peach coloured cells, You will see a problem with my move out formulas. in F4 of the bottem left table 'rent charged', 'Christop' has moved out on November 12 2011. The move out extra column in the top table, shows that Christop owes an additional amount for octobers rent, yet, he is still being charged for oct, nov, and dec in entirety. If I have someone move out, how can I get the main table to stop charging them those months?
    Can I use the column i've added called 'tick if tenant moves out' to trigger something?
    the other problem, in the green cells, is that these amount need to be negative.
    The numbers are the amount from jan 1 2012 to jan 14 2012, so seeing as this table is about 2011, they should be in negative, as the tenants have been charged the full cycles amount on dec 15.
    So maybe January needs another cost added on Jan 1st, representing the first 14 days of the year, and December charges less on Dec 15 charging only for the rest of december,
    The way we got this working for the move in fraction, when someone moves in after Jan 1st, is the following.
    =IF(LEN($A2)>0,IF($A2>=LOOKUP(D$1,Resi info for rent :: $Tenant,First billing cycle of year for tenant),LOOKUP(D$1,Resi info for rent :: $Tenant,Resi info for rent::$Rent),""),"") Perhaps a variation of this will sort out the 'peach' issue.
    I know I am coming at this from wierd angles, I don't have any experience on Numbers before this, so its getting tangled quickly.
    thanks to any who can help!
    Evan

    Xen Ochren wrote (ex forum):
    "The first (error) is created by changing Matts move in date, or 'first date of year tenant stayed' I get the error 'argument 1 must be no bigger than argument 2' in extra days owed B6."
    The error message is pretty much self explanatory.
    Column D is the move in date, Column E is the start of the "first (full) Billing Cycle of the year" for this tenant. Matt's dates are highlighted. In all the other rows, the column E value (second argument) is the first 'fifteenth' following the move in date in column D; a 'bigger' date than the move in date. In Matt's case, the column E value is the most recent 'fifteenth' before Matt's move in date, a 'smaller' date than the move in date.
    Set Matt's billing cycle date to Aug 15, 2011 and the error message disappears.
    You can avoid this type of error by using a formula to calculate the start date of the first full billing cycle from the move in date:
    =DATE(YEAR(D),MONTH(D)+IF(DAY(D)>15,1,0),15)
    Enter into E2 of table RiR and fill down to the row for the last tenant listed.
    A similar formula in G2 (and filled down) can be used to calculate the start date of the last billing cycle in column G of RiR:
    =DATE(YEAR(F),MONTH(F)-IF(DAY(F)<15,1,0),15)
    The second issue, a difference in the year's rent calculated as 12*monthly and the rent for the same period with the first two weeks charged on a daily rate calcualtion and the first two weeks of the next year refunded on a daily rate calculaton, is a bit more challenging.
    As you surmise,the issue arises from the varying length of months (with a touch of complication from the offset of the billing cycle from the calendar cycle).
    More on that in a direct note, tomorrow or Thursday.
    Here's a quick fix to the "Sam" problem on the 'extra days owed' table (last column).
    E2: =MIN(PRODUCT(D2,RiR :: C2),RiR :: B2)
    It works for Sam, where the move out charges are for 31 days, but doesn't address the issue of year end charges and (virtual) refunds tenants in residence for the full year.
    Note that filling the formula down from E2 will override the conditional formatting rules you have on these cells with the rule in E2.
    Regards,
    Barry

  • How to calculate totals based on year (date) and comparing othe columns in the same table please

    Hello Good Evening,
    Could you please help me here
    how to write condition for self table year records, such 2012 name and acctno match with 2013 name and acctno then total, provided below,
    create table #tab1 (MasterKey int, AcctNo varchar(12),name varchar(25), SumaofShares numeric, request_dat datetime )
    --drop table #tab1
    insert into #tab1 values (1000, 100,'Tom', 2500, '10/01/2012')
    insert into #tab1 values (1001, 101,'Bat', 1550, '08/11/2012')
    insert into #tab1 values (1002, 102,'Kit', 1600, '06/12/2012')
    insert into #tab1 values (1003, 103,'Vat', 1750, '04/15/2012')
    insert into #tab1 values (1010, 104,'Sim',200, '04/21/2013')
    insert into #tab1 values (1011, 105,'Tim',500, '06/18/2013')
    insert into #tab1 values (1012, 100,'Tom',800, '08/22/2013')
    insert into #tab1 values (1013, 101,'Bat',550, '09/15/2013')
    insert into #tab1 values (1014, 100,'Pet',200, '02/21/2013')
    insert into #tab1 values (1015, 103,'Vat',150, '03/18/2013')
    insert into #tab1 values (1016, 110,'Sun',800, '03/22/2013')
    insert into #tab1 values (1017, 111,'Bet',550, '12/15/2013')
    insert into #tab1 values (9999, 111,'AAA',110, '12/15/2014')
    create table #tab2 (IssueKey int, totalOutstanding numeric, sharedBenefits varchar(1) )
    --drop table #tab2
    insert into #tab1 values (1000, 500, 'V')
    insert into #tab1 values (1001, 150, 'U')
    insert into #tab1 values (1002, 100, 'N')
    insert into #tab1 values (1003, 170, 'U')
    insert into #tab1 values (1010, 100, 'U')
    insert into #tab1 values (1011, 200, 'K')
    insert into #tab1 values (1012, 340, 'U')
    insert into #tab1 values (1013, 560, 'N')
    insert into #tab1 values (1014, 280, 'V')
    insert into #tab1 values (1015, 150, 'V')
    insert into #tab1 values (1016, 840, 'V')
    insert into #tab1 values (1017, 530, 'N')
    i would like to get 4 columns output
    how to get sumofshares (#tab1) and TotalOutStanding(#tab2) summ up with these values please.,
    MasterKey (#tab1) and IssueKey (#tab2) are like primary key and foreign key
    so the request is
    need to calculate, sumofshares (#tab1) and TotalOutStanding(#tab2) as below
    1)ShareBenefist = U and year( request_dat) in (2012 , 2103) and (Name for 2012 should match with 2013 name and 2012 Acctno should match with 2013 accounno) in (#tab1)
    then '2012 and 2013 accts UN Veriverted'
    2)ShareBenefist = V and year( request_dat) in (2012 , 2103) and (Name for 2012 should match with 2013 name and 2012 Acctno should match with 2013 accounno) in (#tab1)
    then '2012 and 2013 accts Veriverted'
    3)ShareBenefist = N and year( request_dat) in (2012 , 2103) and (Name for 2012 should match with 2013 name and 2012 Acctno should match with 2013 accounno) in (#tab1)
    then '2012 and 2013 accts NONVERT'
    4)year( request_dat) =2102 and Name and Acctno not match with 2013 account name and acctno (#tab1)
    then '2012 last year accounts'
    5)year( request_dat) = 2013 and Name and Acctno not match with 2013 account name and acctno (#tab1)
    then '2012 This year accounts'
    for ex 1) the below accounts in #tab1 has both 2012 and 2013 and acctno same in both years and name is same in both years so it is condired as
    insert into #tab1 values (1012, 100,'Tom',800, '08/22/2013')
    for ex 2)
    insert into #tab1 values (1013, 101,'Bat',550, '09/15/2013')
    for ex 4) 2012 records there is not match acctno and name in 2013 recods
    insert into #tab1 values (1002, 102,'Kit', 1600, '06/12/2012')
    for ex 5) 2013 records there is no match of name and acct no with 2012 records
    insert into #tab1 values (1010, 104,'Sim',200, '04/21/2013')
    insert into #tab1 values (1014, 100,'Pet',200, '02/21/2013')
    insert into #tab1 values (1016, 110,'Sun',800, '03/22/2013')
    insert into #tab1 values (1017, 111,'Bet',550, '12/15/2013')
    Expected Results (just for format)
    AcctTypeDescription, SumofShares, OtotalutStand
    '2012 and 2013 accts UN Veriverted',2700,234
    '2012 and 2013 accts Veriverted' ,2890,234
    '2012 and 2013 accts NONVERT' ,4533,325
    '2012 last year accounts' ,2334,567
    '2012 This year accounts' ,2222,877
    Please
    Thank youy in advance
    asita

    As I understand it, your sample output was only to show the format, and did not pretend to be give the exact result given the sample data. This is pity, because means that I was not able to verify that my query below gives the desired result.
    The exact relation between #tab1 and #tab2 is not clear to me. I'm making the assumption that a row in #tab1 may have zero or one row in #tab2, but not many.
    My solution has two CTEs. The first simply joins the tables together and extracts the year. In the second CTE, I perform a self-join over the first CTE which I have divided in two by year. This is a full join, since an account may appear for only one of
    the year. Note that it is instrumental to extract the year data in inner queries first.
    The final query is just an aggregation over the categories.
    create table #tab1 (MasterKey int, AcctNo varchar(12),name varchar(25), SumaofShares numeric, request_dat datetime )
    --drop table #tab1
    insert into #tab1 values (1000, 100,'Tom', 2500, '10/01/2012')
    insert into #tab1 values (1001, 101,'Bat', 1550, '08/11/2012')
    insert into #tab1 values (1002, 102,'Kit', 1600, '06/12/2012')
    insert into #tab1 values (1003, 103,'Vat', 1750, '04/15/2012')
    insert into #tab1 values (1010, 104,'Sim',200, '04/21/2013')
    insert into #tab1 values (1011, 105,'Tim',500, '06/18/2013')
    insert into #tab1 values (1012, 100,'Tom',800, '08/22/2013')
    insert into #tab1 values (1013, 101,'Bat',550, '09/15/2013')
    insert into #tab1 values (1014, 100,'Pet',200, '02/21/2013')
    insert into #tab1 values (1015, 103,'Vat',150, '03/18/2013')
    insert into #tab1 values (1016, 110,'Sun',800, '03/22/2013')
    insert into #tab1 values (1017, 111,'Bet',550, '12/15/2013')
    insert into #tab1 values (9999, 111,'AAA',110, '12/15/2014')
    create table #tab2 (IssueKey int, totalOutstanding numeric, sharedBenefits varchar(1)  )
    --drop table #tab2
    insert into #tab2 values (1000,  500,  'V')
    insert into #tab2 values (1001,  150,  'U')
    insert into #tab2 values (1002,  100,  'N')
    insert into #tab2 values (1003,  170,  'U')
    insert into #tab2 values (1010,  100,  'U')
    insert into #tab2 values (1011,  200,  'K')
    insert into #tab2 values (1012,  340,  'U')
    insert into #tab2 values (1013,  560,  'N')
    insert into #tab2 values (1014,  280,  'V')
    insert into #tab2 values (1015,  150,  'V')
    insert into #tab2 values (1016,  840,  'V')
    insert into #tab2 values (1017,  530,  'N')
    go
    WITH joined AS (
      SELECT a.MasterKey, a.AcctNo, a.name, a.SumaofShares,
             year(a.request_dat) AS year, b.totalOutstanding, b.sharedBenefits
      FROM   #tab1 a
      LEFT   JOIN #tab2 b ON a.MasterKey = b.IssueKey
    ), categories (category, sumofshares, totaloutstanding) AS (
       SELECT CASE WHEN A.MasterKey IS NOT NULL AND
                        B.MasterKey IS NOT NULL
                        THEN '2012 and 2013 accts ' +
                             CASE B.sharedBenefits
                                WHEN 'U' THEN 'UN Veriverted'
                                WHEN 'V' THEN 'Veriverted'
                                WHEN 'N' THEN 'NONVERTED'
                                ELSE ''
                             END
                   WHEN A.MasterKey IS NOT NULL THEN '2012 last year accounts'
                   WHEN B.MasterKey IS NOT NULL THEN '2013 this year accounts'
              END,
              coalesce(A.SumaofShares, 0) + coalesce(B.SumaofShares, 0),
              coalesce(A.totalOutstanding, 0) + coalesce(B.totalOutstanding, 0)
       FROM   (SELECT * FROM joined WHERE year = 2012) AS A
       FULL   JOIN (SELECT * FROM joined WHERE year = 2013) AS B
             ON A.AcctNo = B.AcctNo
            AND A.name   = B.name
    SELECT category, SUM(sumofshares) AS sumofshares,
           SUM(totaloutstanding) AS totaloutstanding
    FROM   categories
    GROUP  BY category
    go
    drop table #tab1, #tab2
    Erland Sommarskog, SQL Server MVP, [email protected]

  • How to calculate year and month

    Dear Friends
    I have to make a customised report.In this report there are different buckets of one year from the year and month being entered by the user.
    For example if user is giving input value for year and month as 200912 then system has to show data in first bucket for the period 200912 to 200901.In the second bucket system will show data for period 200812 to 200801.In this way system has to show data in 11 buckets for 11 years.
    Is there any function module/BAPI in SAP through which system will calculate period of one year for different buckets for my report from the input value(year and month) being given by the user.
    Moderator message - Please see The specified item was not found. before posting - post locked and Points unassigned
    Edited by: Rob Burbank on Jan 5, 2010 11:03 AM

    Hi Satish,
    You can do this with a Standard function module.
    I am providing my code below :
    data : G_date type sy-datum.
    CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
        EXPORTING
          DATE      = sy-datum " instead of sy-datum provide
              " the select option here which user provides.
          DAYS      = C_fourty"45
          MONTHS    = C_MM    "00
          SIGNUM    = C_Negative "'-'
          YEARS     = C_YY    "00
        IMPORTING
          CALC_DATE = G_date. "G_date will have date less than 45 days
    *                         " from the day program has been executed.
    Now based upon the G_DATE, we can fetch remaining 10 buckets.
    Regards,
    Kittu
    Edited by: Rob Burbank on Jan 5, 2010 11:01 AM

  • How to calculate Average time from a date field

    Hi All,
    I have a date type field in my table .
    I want to calculate average time for a given country in a select query. Date has to be exculded. Only time has to be taken into consideration.
    Kindly help me.
    Sample data
    india 25-JUN-09 08:12:45
    india 25-JUN-09 09:01:12

    Take which one you want.WITH dates AS
      (SELECT sysdate x FROM dual
        UNION
       SELECT sysdate + 1 +1/24 FROM dual
    SELECT TO_CHAR(to_date(AVG(to_number(TO_CHAR(to_date(TO_CHAR(x,'HH24:MI:SS'),'HH24:MI:SS'),'sssss'))),'sssss'),'hh24:mi:ss')
       FROM dates;
    WITH dates AS
      (SELECT sysdate x FROM dual
        UNION
       SELECT sysdate + 1 +1/24 FROM dual
    SELECT floor(24 * AVG(x- TRUNC(x)))
      || ':'
      || floor(mod(24 * AVG(x- TRUNC(x)),1) * 60)
      || ':'
      || floor(mod(mod(24 * AVG(x- TRUNC(x)),1) * 60,1) * 60)
       FROM dates;By
    Vamsi

  • How to calculate the days with a date field

    I need to make a query that shows the number of flown days at the end of the month for the whole fleet, given a date that represents one day within that month.
    I would appreciate all the help I can get
    thanks in advance

    Hi,
    I am not sure if I interpreted your question correctly, but are you looking for counting for number of days between two days?
    If yes, see if the following example query helps you.
    SQL>select to_date('30-Jun-2003') - to_date('30-May-2003') from dual
    - this returns 31.
    If not, could you please give more details of your requirement with an example?
    -Savitha.

  • Confused - How to Calculate Number of Days Between Dates but Exclude Weekend Dates If There Hasn't Been a Weekend Update

    Hello -
    I've been tearing my hair out over this problem i'm trying to solve, probably just been staring at it too long which is making it worse -
    I have a series of open support tickets which are supposed to be updated on a daily basis, the problem is that they aren't always being updated daily.  So, the business wants to know the number of days from when a ticket was last updated and today's
    date.  I have this basic calculation and it's working fine, however now the business wants to exclude weekends from the calculation.  The other problem is that some reps DO go in on weekends and update their tickets, so sometimes there will be updates
    made on weekend dates.
    To give an example -
    Today's date is 2014-02-10 (Monday).  A ticket was last updated last Thursday, 2014-01-30.  The difference between the two dates is 11, so it's been 11 days since the ticket was last updated.  Now, if I exclude Saturdays and Sundays, then
    it's actually been 7 days since the ticket was last updated.  I'm not sure how to do this in T-SQL.
    Now, to further complicate the problem, sometimes a ticket IS updated on a Saturday or Sunday.  So, if a ticket was updated on 2014-02-02 (Sunday), then it should be counted.  Again i'm not sure how to do this. 
    What gets me is that this is probably fairly simple and i've just been staring at it too long.  In the meantime, can someone offer some guidance?
    Thanks!!

    I've adapted this from a function on my blog. you will need to add set the YourTicketTable to where ever your tickets are stored.
    CREATE
    FUNCTION [dbo].[CalcWorkDaysBetween](@StartDate
    As DateTime,@EndDate
    AS DateTime)
    RETURNS
    INT AS
    BEGIN
    SET @EndDate
    =DATEADD(DAY,1,@EndDate)
    DECLARE @Count
    AS Int= 0
    DECLARE @Date
    As Date=@StartDate
    WHILE @Date
    < @EndDate
    BEGIN
    IF (DATEPART(WEEKDAY,@Date)IN(1,7)
    OR (SELECT
    Count(*)
    FROM YourTicketTable WHERE TicketDate=@Date)=1)
    BEGIN
    SELECT @Count = @Count
    + 1
    END
    SELECT @Date=DATEADD(Day,
    1,@Date)
    END
    RETURN
    DATEDIFF(DAY,@StartDate,@EndDate)- @Count
    END
    Regards,

Maybe you are looking for

  • Without restart/reload - web.xmlupdate security constraints, is it possible

    Is there a way to make use of the 9ias/j2ee security framework, where add/modify security constrains; change roles and url-patterns etc, in web.xml without having to restart/reload the webapp. Doesn�t Restart/reload remove existing user sessions from

  • Oracle 8.1.6 on Sun Sparc Solaris 8

    Any successful story of Oracle 8.1.6 runs on Sun Sparc Solaris 8? The documentation of Oracle 8.1.6 mentions that it can run on Solaris 2.6 and 7. But how about Solaris 8? Thanks.

  • Unable to Connect, ORA 12154, Setup routine for Microsoft ODBC errors

    Hi all, I have a Windows 7 Enterprise 64bit SP1 system and I wanted to be able to connect to an oracle database server. However, I am facing a problem with ODBC configuration. I have currently installed both below in ... product\11.2.0\client_1 folde

  • I don't get tones when a message arrives

    For some reason I no longer hear a tone when a message arrives on my iPhone 5.  Occasionally I do get a tone, but usually not - any suggestions?

  • DS 6.3.1 thousands of open established connections

    Hello everyone, I have been fighting a problem on our backup server. Every night i get about 500 emails stating a problem with ldap. The error is as follows: libsldap: Status: 81 Mesg: LDAP ERROR (81): Error occurred during receiving results. This ma