Calculate data of last 30 days on basis of sys date

SELECT DT, ITEMCODE, ITEMNAME, QTY
FROM   TABLE1In above given query, how i could calculate the QTY details of last 30 days, APR, MAY, and JUNE
As the current month is JULY, in AUG, the Qty details are required for last 30 days (from sysdate), MAY, JUNE, JUL
Please suggest.
Yogesh
Edited by: user12957777 on Jul 28, 2010 3:16 AM

Hi,
Use CASE expressions to filter data in particular columns:
SELECT     SUM ( CASE
            WHEN dt >= ADD_MONTHS ( TRUNC (SYSDATE, 'MONTH')
                         , -3
            AND  dt <  ADD_MONTHS ( TRUNC (SYSDATE, 'MONTH')
                         , -2
            THEN qty
           END
         )          AS third_month
,     SUM ( CASE
            WHEN dt >= ADD_MONTHS ( TRUNC (SYSDATE, 'MONTH')
                         , -2
            AND  dt <  ADD_MONTHS ( TRUNC (SYSDATE, 'MONTH')
                         , -1
            THEN qty
           END
         )          AS second_month
,     SUM ( CASE
            WHEN dt >= ADD_MONTHS ( TRUNC (SYSDATE, 'MONTH')
                         , -1
            AND  dt <               TRUNC (SYSDATE, 'MONTH')
            THEN qty
           END
         )          AS last_month
,     SUM ( CASE
            WHEN dt >= TRUNC (SYSDATE) - 30
            AND  dt <  TRUNC (SYSDATE)
            THEN qty
           END
         )          AS last_30_days
FROM     table1
;You can do this with GROUP BY, also, if you want separate rows for each itemname (for example).
I had to guess at some things, such as what you mean by the last 30 days.
If you'd like to post some sample data 9CREATE TABLE and INSERT statements) and the results you want from that data, then I could test this.

Similar Messages

  • Show data of last day of previous month against any day of current month

    Hi,
    I have fact table which contains data at date level (we have data for oct-2009 to april-2010). Our requirement is to show data of last day of previous month against any day of current month in obiee 11g. I am facing problem in Feb 2010 its picking data of 28-Jan-2010 instead of 31-jan-2010 and for April its picking data of 30-mar-2010 instead of 31-mar -2010.
    Any suggestion ???

    You're asking to filter your data set to only include rows between:
    1) last day of the previous month
    2) any day of the current month
    This can be achieved with prompting in OBIEE Answers.
    last day of previous month = TIMESTAMPADD( SQL_TSI_DAY , -(1), TIMESTAMPADD( SQL_TSI_DAY , DAYOFMONTH( CURRENT_DATE) * -(1) + 1, CURRENT_DATE)) . The problem is you need to make query work within Oracle's Answer syntax.
    In the prompt, select the operator type for your date dimension as 'between' and default to 'SQL Results'.
    For the 'last day of previous month' , use the query:
    SELECT
    case when 1=0 then Time."Fiscal Date" else TIMESTAMPADD( SQL_TSI_DAY , -(1), TIMESTAMPADD( SQL_TSI_DAY , DAYOFMONTH( CURRENT_DATE) * -(1) + 1, CURRENT_DATE))
    end
    FROM ENTER_YOUR_PRESENTATION_FACT_FOLDER_HERE
    For the current date, use the query:
    SELECT
    case when 1=0 then Time."Fiscal Date" else CURRENT_DATE
    end
    FROM ENTER_YOUR_PRESENTATION_FACT_FOLDER_HERE
    Another option is to create a last_date_pervious_month variable in the RPD and have that as the default value in your prompt.

  • Successive dates with  last day of that month being added to succesive date

    Hi ,
    I have a piece of code that displays the succuessive dates from the given date to tilll date with a date interval of 30 days. My requirement goes some thing like this. since the given date is '26-nov-08' the next successive date should be the number of days in nov(30) added to '26-nov-08'. that will be around 26-dec-08. For this date it has to add the number of days in December(31) to '26-dec-08' and that turns out to be '26-jan-09'. and this must go on till date.
    In the below code i have used 30 for the interval of 30 days but the interval has to the last day of the successive date's month( for dec the interval has to 31 and for january the interval has to be 31 and for february 28)
    select to_date('26-nov-08', 'dd-mon-yy') + (level - 1) * 30
    from dual
    connect by level <= trunc((sysdate-to_date('26-nov-08', 'dd-mon-yy'))/30 )Please advice

    hi ,
    the query sent by you is correct and could it be in corporated for this query :
    SELECT TRUNC(inst_due_date) + (LEVEL-1 ) * (ilpa_term_type),iloan_code,LOAN_AMT,FEE_AMT,
    LOAN_DATE,LOAN_STATUS_ID
    FROM (
    SELECT inst_due_date,sm.iloan_code,LOAN_AMT,
    FEE_AMT,
    LOAN_DATE,
    LOAN_STATUS_ID,
    decode(sm.ilp_term_type, 26 ,14,24,15,12,(select to_number(to_char(last_day(inst_due_date),'dd')) from dual)) as ilpa_term_type
    FROM ST_IL_SCHEDULE sc,ST_IL_MASTER sm
    WHERE sc.iloan_code = sm.iloan_code
    AND sc.inst_num = (SELECT MAX(inst_num) FROM ST_IL_SCHEDULE b WHERE b.iloan_code = sc.iloan_code)
    AND sc.iloan_code =123456789)
    CONNECT BY LEVEL <= TRUNC((TRUNC(SYSDATE)-TRUNC(inst_due_date))/(ilpa_term_type) )
    Here we are using the decode concept if ilp_term_type = 26 then 14 but we need to implement the code of adding the number of days to that month in place of this code :
    select to_number(to_char(last_day(inst_due_date),'dd')) from dual:
    Please advice

  • Last day of previous month for data load

    Hi,
    I have to load data from the previous month into the psa and then into an infocube. I was wondering as to how to get the last of the previous month to write a code in ABAP. I will be writing the code at the infopackage level in the data selection. I could load data from the 1st of the previous month to the 1st of the current month. This will be an additional load of 30,000 records for the 1st of every month, since I will be loading 30,000 records everyday, I was wondering if I could limit the load from the 1st of every previous month to the last day of that month. This will be a repetitive loading.
    DATA: CURR_MM(2) TYPE N,
    CURR_YYYY(4) TYPE N,
    CURR_DD(2) TYPE N,
    PREV_MM(2) TYPE N,
    PREV_YYYY(4) TYPE N,
    PREV_DD(2) TYPE N,
    YYYY_MM(6),
    YYYY_MM1(6),
    DATE LIKE SY-DATUM.
    DATE = SY-DATUM.
    CURR_YYYY = DATE+0(4).
    CURR_MM = DATE+4(2).
    CURR_DD = DATE+6(2).
    PREV_DD = 1.
    IF CURR_MM = '01'.
    PREV_MM = '12'.
    PREV_YYYY = CURR_YYYY - 1.
    ELSE.
    PREV_MM = CURR_MM - 1.
    PREV_YYYY = CURR_YYYY.
    ENDIF.
    concatenate PREV_YYYY PREV_MM PREV_DD into YYYY_MM.
    concatenate CURR_YYYY PREV_MM PREV_DD into YYYY_MM1.
    read table l_t_range with key
    fieldname = 'BLDAT'.
    l_idx = sy-tabix.
    l_t_range-low = YYYY_MM.
    l_t_range-high = YYYY_MM1.
    l_t_range-sign = 'I'.
    l_t_range-option = 'BT'.
    modify l_t_range index l_idx.
    p_subrc = 0.
    Mind you this code will load data from 1st of the previous month to the 1st of current month. I just don't want to load that extra "1st day" of current month data as I have 30,000 records everyday.
    Say for example, I want to load data from 1st Mar to 31st Mar or 1st Feb to 28thFeb. How should I modify the above code.
    Is there a formula to get the last date of the previous month. That's all I need. This would solve the problem.

    try this routine. it will return a range from 1st day to end of the month.
    DATA: l_s_range TYPE rsr_s_rangesid,
              E_T_RANGE TYPE  RSR_T_RANGESID.
    DATA: year(4) TYPE n,
          month(2) TYPE n,
          day(2) TYPE n,
        ld_keydate  TYPE sydatum,
          ld_lastday  TYPE sydatum.
      REFRESH e_t_range.
      CLEAR l_s_range.
      year  = sy-datum(4).
      month = sy-datum+4(2).
    *Months with 31 days in year
      IF month = '01' OR
         month = '03' OR
         month = '05' OR
         month = '07' OR
         month = '08' OR
         month = '10' OR
         month ='12'.
        day = '31'.
      ENDIF.
    *check for leap year: provoking sy-sybrc <> 0
      IF month = '02'.
        day = '29'.
        MOVE:   '02'        TO ld_keydate+4(2),
                year        TO ld_keydate(4),
                day         TO ld_keydate+6(2).
        CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
          EXPORTING
            date   = ld_keydate
          EXCEPTIONS
            OTHERS = 1.
        IF sy-subrc <> 0.
          day = '28'.
        ENDIF.
      ENDIF.
    *months with 31 days in year
      IF month = '04' OR
         month = '06' OR
         month = '09' OR
         month = '11'.
        day = '30'.
      ENDIF.
      MOVE: year              TO ld_lastday(4),
            month             TO ld_lastday+4(2),
            day               TO ld_lastday+6(2).
      l_s_range-low  = sy-datum.
      l_s_range-high = ld_lastday.
      l_s_range-sign = 'I'.
      l_s_range-opt  = 'BT'.
      APPEND l_s_range TO e_t_range.

  • Last day from the list of dates

    i have a query which give me list of dates. From these list of date i want to find the last day in all months. when i am using last day fucntion i am getting last day from the sysdate and not from the list of dates generated by my query.
    the query
    (select distinct to_date(substr(batch_id,1,6),'DDMMRR') batch_id from gcon_mst_v0)
    order by batch_id desc
    i want last day from each month from the list of dates listed from the query.
    Help would be higly appriciated
    Thanks in advance

    The column is a varchar but i am convering it to get
    the list od dates as you can see in the query. i want
    all the last dates of every month from that list of
    dates for eg. below is the list of the dates i am
    getting from the query.
    7/11/2007
    7/10/2007
    7/9/2007
    7/8/2007
    7/6/2007
    7/5/2007
    7/4/2007
    7/3/2007
    7/2/2007
    7/1/2007
    6/29/2007
    6/28/2007
    6/27/2007
    6/26/2007
    6/25/2007
    6/24/2007
    6/22/2007
    6/21/2007
    6/20/2007
    6/19/2007
    6/18/2007
    6/17/2007
    6/15/2007
    6/14/2007
    6/13/2007
    6/12/2007
    6/11/2007
    6/10/2007
    6/8/2007
    6/7/2007
    6/6/2007
    6/5/2007
    6/4/2007
    6/3/2007
    6/1/2007
    5/31/2007
    5/30/2007
    5/29/2007
    5/28/2007
    5/27/2007
    5/25/2007
    5/24/2007
    5/23/2007
    5/22/2007
    5/21/2007
    5/20/2007
    5/18/2007
    5/17/2007
    5/16/2007
    5/15/2007
    5/14/2007
    5/13/2007
    5/11/2007
    5/10/2007
    5/9/2007
    5/8/2007
    5/7/2007
    5/6/2007
    5/4/2007
    5/3/2007
    5/2/2007
    5/1/2007
    4/30/2007
    4/29/2007
    4/27/2007
    4/26/2007
    4/25/2007
    4/24/2007
    4/23/2007
    4/22/2007
    4/20/2007
    4/19/2007
    4/18/2007
    4/17/2007
    4/16/2007
    4/15/2007
    4/14/2007
    4/12/2007
    4/11/2007
    4/10/2007
    4/9/2007
    4/8/2007
    4/6/2007
    4/5/2007
    4/4/2007
    4/3/2007
    4/2/2007
    4/1/2007
    3/30/2007
    3/29/2007
    3/28/2007
    3/27/2007
    3/26/2007
    i want the last date for all the months listed. that
    would be
    6/29/2007 -- last day for june
    5/31/2007-- last day for may
    4/30/2007-- last day for april
    3/30/2007-- last day for march and so on
    thankswrite query as
    SELECT LAST_DAY(TO_DATE(SUBSTR(COLUMN_NAME,X),'MM/DD/YYYY'))
    FROM TABLE_NAME

  • Due date falls on 80 days from base line date ........but end of that month

    Hi All
    Can any one please guide me in creating a payment term
    The Base Line Date would be 80 days from Posting date ........... But end of that month
    Example
    Posting date 01.01.2009 so the base line date would be on 21st March
    01.01 ;          31.01 ;                            31 Days
    01.02 ;          28.02 ;                            28 Days
    01.03 ;          21.03 ;                            21 Days               80 Days              
    But due date should fall on end of that month 31st of march
    So irrespective of any day calculated as above in that month  it will always take the baseline date as last day of the month    
    Its Picking up the posting day as Baseline date from Transaction VA01 -> Additional data tab -> ETD ( Festinated Time of Departure)
    From this date it need to calculate the due date as above ( 80 day but last day of the month)
    Please Guide
    Many thanks in advance
    Aadi

    Hi Aditya,
    You may have to create 2 payment terms using the day limit option to meet this requirement.
    Follows the below steps -
    T-code OBB8
    Term 1
    Payment terms = xxxx (as you wish)
    Day limit = any number between 10-15 (you may fix an appropriate date as you think fit for your business)
    Default for baseline date = Posting date
    Payment terms/fixed date = 31
    Payment terms/additional months = 2
    Term 2
    Payment terms = same name as above
    Day limit = 31
    Default for baseline date = Posting date
    Payment terms/fixed date = 31
    Payment terms/additional months = 3
    Logic here is... for all invoices with posting date on or before 10th-15th of a month, 80th day will fall definitely on the 2nd month. Invoices with posting date after 15th, the 80th day will fall on the 3rd month.
    You have to take a concious decision on the day limit number. Because 80 days will not exactly work for all the months.
    Regards,
    Mike

  • Find the first and last day of week giving a certain date

    Hi,
    i have an application in wich the user puts a date, say today 2010-08-10 and i have to calculate first and last day of that week, in this case 2010-08-09 and 2010-08-15. How can i do this?
    Many thanks in advance,
    Nuno Almeida

    nfalmeida wrote:
    i have an application in wich the user puts a date, say today 2010-08-10 and i have to calculate first and last day of that week, in this case 2010-08-09 and 2010-08-15. How can i do this?First step is being sure that you know what a 'week' is.
    For example does it really start on monday? And will it always start on monday?
    And what day does the 'week' end on for 2010-12-29? In some businesses it will end on 2010-12-31 (friday)

  • How to find first and last day of last month?.

    Hello,
    I am using Crystal Report XI.
    Reports will be generated on first of every month for
    previous month. I want to find start and end date of previous month
    dynamic.
    For august, Start date is July 1st and End date July 31st.
    For September, start date is Aug 1st and Aug 31st.
    How can i get first of previous month as start date and last
    day of the previous month as end date?.
    Same kind of thing I want to do for future report ..find next
    month start date and end date.
    Thanks

    Adomacro,
    you could do a function, like this
    <cffunction name="getReportdates" returntype="struct"
    hint="Given an arbitrary month and year, the function returns a
    structure containing the first and last day of the previous
    month">
    <!--- Take current month and current year as the default
    --->
    <cfargument name="mnth" type="string" required="No"
    default="#monthAsString(month(now()))#">
    <cfargument name="yr" type="numeric" required="No"
    default="#year(now())#">
    <cfset firstDayOfGivenMonth = parseDateTime("1 " &
    arguments.mnth & " #arguments.yr#")>
    <cfset lastDayOfPreviousMonth =
    dateAdd("d",-1,firstDayOfGivenMonth)>
    <cfset numberOfDaysOfPreviousMonth =
    daysInMonth(lastDayOfPreviousMonth)>
    <cfset firstDayOfPreviousMonth =
    dateAdd("d",-#numberOfDaysOfPreviousMonth#,firstDayOfGivenMonth)>
    <cfset reportDates = structNew()>
    <cfset reportDates.firstDayOfPreviousMonth =
    firstDayOfPreviousMonth>
    <cfset reportDates.lastDayOfPreviousMonth =
    lastDayOfPreviousMonth>
    <cfreturn reportDates>
    </cffunction>
    <!--- Example usage --->
    <p>
    Given month: <strong>March
    2004</strong><br>
    begin date:
    <cfoutput>#dateFormat(getReportdates('March',2004).firstDayOfPreviousMonth,"d
    mmm yyyy")#</cfoutput><br>
    end date:
    <cfoutput>#dateFormat(getReportdates('March',2004).lastDayOfPreviousMonth,"d
    mmm yyyy")#</cfoutput><br>
    </p>
    <p>
    Given month: <strong>August
    2007</strong><br>
    begin date:
    <cfoutput>#dateFormat(getReportdates('August',2007).firstDayOfPreviousMonth,"d
    mmm yyyy")#</cfoutput><br>
    end date:
    <cfoutput>#dateFormat(getReportdates('August',2007).lastDayOfPreviousMonth,"d
    mmm yyyy")#</cfoutput><br>
    </p>

  • How to find first and last day of previous month?.

    Based on current month, I want to find start and end day of
    previous month.
    For example,
    For august, Start date is July 1st and End date July 31st.
    How can i get first of previous month as start date and last
    day of the previous month as end date?.
    Same way,
    i want to find start date of current month and end date of
    next month.
    Example:
    For august,
    i want to get start date, august 1st and end date : september
    30.
    How can i do this from current date or now().
    I am looking for best and easy way to find start and end
    dates..
    Thanks

    <cfset today = now()>
    <cfset firstOfThisMonth = createDate(year(today),
    month(today), 1)>
    <cfset lastOfNextMonth = dateAdd("d", -1, dateAdd("m", 2,
    firstOfThisMonth))>
    <cfoutput>
    today = #today#<br>
    firstOfThisMonth = #firstOfThisMonth#<br>
    lastOfNextMonth = #lastOfNextMonth#<br>
    </cfoutput>
    Edit - To find the start and end day of previous month, get
    the first of THIS month. Use Subtract 1 month ("m") to get the
    start date. Subtract 1 day ("d") to get the end date.

  • How to get Last Day of a Previous Month

    Hi all,
    I need to get Last Day of the Previous Month. I am able to get Current Month Last Day using Calendar.getActualMaximum(Calendar.DATE)
    But I need previous Month's Last Day.
    Thanks
    Vamshi.

    Thanks all....
    I have been trying the same and could get it.....
    here is the code for that....
    SimpleDateFormat simpleDate = new SimpleDateFormat("MM/dd/yyyy");
    Calendar calendar = Calendar.getInstance();
    month = calendar.get(Calendar.MONTH);
    //year = calendar.get(Calendar.YEAR);
    calendar.set(Calendar.MONTH, month-1);
    calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
    //lastDayOfMonth = calendar.getActualMaximum(Calendar.DATE);
    System.out.Println("Previous Month End Date is :: " + simpleDate.format(calendar.getTime()));
    this works....
    thanks for ur replys....

  • How to Calculate the Actuall work days ....

    Hi Friends,
    how to calculate the Actuall Working days Based on the Joining date for 5day work and 6 days work?
    is there any FM to calculate this?
    Thanks in Advance,
    sudha.

    Hi Sudha,
    In your company , If they are maintaining Time Events then you can get the information of Employee's Presense or Absense from infotype 2011 (Time Events) and Absense from 2001 (Absense)..
    Also Consider Factory Calender for it.. and check out working days..
    Table TEVEN
    Try below FM as well..
    HR_DV_TIMEEVENTS_GET
    Hope it will solve your problem..
    Thanks & Regards
    ilesh 24x7
    ilesh Nandaniya

  • Report of Total GR qty/IR qty,date when last GR was passed (latest one)

    Dear all
           Kindly let me know how to get the below report using input of PO no:
    Report of Total GR qty,IR qty,date when last GR was passed (latest one),date when last IR was passed (latest one)
    Standard t.code such as ME2n.me2l is not giving the above report.

    Dear
    Try ME80FN and ME2N/L with the setting with Selection Parameters
    regds
    dev...!

  • Why is my Samsung suddenly taking forever to load emails?  It has used almost 1 GB of data in two days. The foreground data is minimal. background data is what is taking all the data.

    Why is my Samsung suddenly taking forever to load emails?  It has used almost 1 GB of data in two days!  All in background data; very little in foreground.

        ChinaPanda we'll get you up and running! To clarify, do you have the Galaxy S4 or the Mini? What type of email account do you have? Are you having trouble with other data activities such as browsing the web?
    AshleyS_VZW
    Follow us on Twitter @VZWSupport

  • I have a Mac Book (2006) All my saved emails for two and a half years have the same date. How do I get the original dates back?

    From the "date and time" I tried to drag the month and year to my menu bar. It did not stay, so I did it several times, but to no avail. I closed the window and went back to my Thunderbird email only to find that the date I tried to use became the date in the "date received" column of all of my emails from back to 2011. I changed the date a few days later, and then the date changed on all of my emails. I would like to undo whatever I did to the dates so that the emails show when they were originally received. I did manage to get "the order received" which helps a little, but I cannot see the original dates.
    I have checked "Set date and time automatically" but it doesn't change the dates on my saved emails.

    I at first thought it might be a Mac thing, but I couldn't find a solution. I realized that the date only affected my email, so then I thought it must be a Mozilla problem.

  • How to calculate the days between last movement of material till today date

    Hello Experts,
    Could someone help me please?
    I have a key figure in the InfoCube 0IC_C03 witch contain the last date of movement of material.
    (it is a key figure created in format date and contain the max value).
    How can I to calculate the number of days between this date (last movement),  till today date (system date).
    I need to show:
    material........last movement.....days without movements.
    xxxxxxxxxxx.....dd/mm/yyyy..........9999
    Im trying to do this calculation in bex using variable formula but it doesn't work becouse current date (customer exit) brings sy-datum in format yyyymmdd and the kf-last-mov is in format number (I dont know what kind of number)
    Thanks in advance,
    Points are assured.
    Regards,
    Silvio Meurer.
    Message was edited by:
            Silvio Meurer

    Hi Parth,
    Here we are using the version 7.0 and SP 10, I'm afraid I could not understand you. I Can't find the function DATE_DIFF (Where is it?)
    I'm using the formula and it doesn't work. Coud you see the result is:
    last movement date     Today date        result
    02.04.2007                  20.070.625       19.337.856
    18.05.2007                  20.070.625       19.337.810
    the "today date" is from customer exit and the result is a strange number to me.
    Could you help me?

Maybe you are looking for