Which SQL function to use to get previous month's data?

Hi
The reporting need: Display of two month's payroll run results....current and previous (based on the parameter passed) in Oracle Discoverer.
Data source: A table in which run result values are stored. Of course to be linked to number of other tables.
Can somebody guide me on which SQL function to use to get the data for previous month?
Secondly, as Discoverer does not support parameters, I cannot put parameter in the query itself. I'll be applying parameter later while generating my report.
Please advice.
Thanks and regards,
Aparna

It's not very clear in my head... but you can try :
SQL> select * from test;
ENAM        SAL DT
TOTO       1000 30/05/06
TOTO       1001 20/04/06
TOTO       1002 11/03/06
TATA       2000 30/05/06
TATA       1500 20/04/06
TUTU       3500 30/05/06
6 rows selected.
SQL> select ename, dt, sal currmonth,
            case when trunc(lag(dt,1,dt) over (partition by ename order by dt),'MM') = trunc(add_months(dt,-1),'MM')
                 then lag(sal,1,sal) over (partition by ename order by dt)
                 else null end prevmonth
     from   test
SQL> /
ENAM DT        CURRMONTH  PREVMONTH
TATA 20/04/06       1500
TATA 30/05/06       2000       1500
TOTO 11/03/06       1002
TOTO 20/04/06       1001       1002
TOTO 30/05/06       1000       1001
TUTU 30/05/06       3500
6 rows selected.
SQL>
SQL> Nicolas.
Just an additional question : do the previous month is current month-1, or is there hole in month suite (e.g. the previous month can be current month-2) ?
Message was edited by:
N. Gasparotto

Similar Messages

  • To get previous month's data

    Hi
    My report requirement:
    Display current month and previous month's data (sal) next to each other based on the month selected as a current month.
    Expression I'm using at the moment:
    case when trunc(lag(dt,1,dt) over (partition by ename order by dt),'MM') = trunc(add_months(dt,-1),'MM')
    then lag(sal,1,sal) over (partition by ename order by dt)
    However, instead of 'dt' I'm trying to use a parameter (dt as a current month's dt).
    This gives me an error message.
    Can anyone guide me on this?
    Thanks and regards,
    Aparna

    Why not simply join the table with itself? I.e. join the two months data sets and have a single row containing current and previous month results
    Simplistic example. We have a YEARLY_TOTALS table where the primary key is MONTH in the date format YYYY/MM. You can then compare the sales totals per month of this year with that of last year using the following type of SELECT construct:
    SELECT
    cur.month,
    cur.sales as CURRENT_SALES,
    prev.sales as LAST_YEAR_SALES
    FROM yearly_totals cur
    JOIN yearly_totals prev
    ON ADD_MONTHS(prev.month,12) = cur.month
    WHERE cur.month >= TRUNC(SYSDATE,'YY')

  • Previous month first data and previous month last date

    can any body have query to get previous month first date and previous month last date.
    Ex: First day of the previous week:
    TIMESTAMPADD(SQL_TSI_DAY,-6, (TIMESTAMPADD(SQL_TSI_DAY, DAYOFWEEK(CURRENT_DATE) *-1,CURRENT_DATE)))
    Last day of the previous week:
    TIMESTAMPADD(SQL_TSI_DAY, DAYOFWEEK(CURRENT_DATE) *-1,CURRENT_DATE)
    can anybody have it for first day of the previous month,last day of the previous month?
    Edited by: user12255470 on Apr 7, 2010 3:30 AM

    Hi,
    1st day of previous month :
    TIMESTAMPADD(SQL_TSI_DAY, ( DAYOFMONTH(TIMESTAMPADD(SQL_TSI_MONTH,-1,CURRENT_DATE)) * -1) + 1, TIMESTAMPADD(SQL_TSI_MONTH,-1,CURRENT_DATE))
    last day of previous month :
    TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(TIMESTAMPADD(SQL_TSI_MONTH,-1,CURRENT_DATE)) * -1 , TIMESTAMPADD(SQL_TSI_MONTH, 1, TIMESTAMPADD(SQL_TSI_MONTH,-1,CURRENT_DATE)))
    Please mark Q answered and award points for correct answers !
    Thanks
    Alastair

  • Which table i can use to get standard price of previous periods ?

    Hello, all.
        Which table i can use to get standard price of previous periods ? Example, current period is 2008/11, i want to get the standard price of period 2008/05 .
    Thanks.
    Xinzhou.

    Look in MBEW and MBEWH.
    Regards

  • Which table field is used for getting Release quantity

    Hi friends,
    please tell me which table and field used to get "Release Quantity" also i want "Cumulative GR by release" table and field please do reply if u know.
    Thanks
    Yogesh

    Hi,
    RSEVENTCHAIN       Event
    Chain Processing Event Table
    RSEVENTHEAD        Header
    for the event chain
    RSEVENTHEADT        Header
    for the event chain
    RSPCCHAIN     Process
    chain details
    RSPCCHAINATTR      
    Attributes for a Process Chain
    RSPCCHAINEVENTS      Multiple
    Events with Process Chains
    RSPCCHAINT      Texts
    for Chain
    RSPCCOMMANDLOG     System
    Command Execution Logs (Process Chains)
    RSPCLOGCHAIN      
    Cross-Table Log ID / Chain ID
    RSPCLOGS
    Application Logs for the Process Chains
    RSPCPROCESSLOG     Logs for
    the Chain Runs
    RSPCRUNVARIABLES      
    Variables for Process Chains for Runtime
    RSPC_MONITOR     Monitor
    individual process chains
    Regards
    Ashwin

  • Which are function modules used to convert into XML format in SAP 4.6c Ver

    which are function modules used to convert into XML format in SAP 4.6c Ver

    Hi,
    check this program , I think this will help you
    TYPE-POOLS: ixml.
    TYPES: BEGIN OF xml_line,
    data(256) TYPE x,
    END OF xml_line.
    data : itab like catsdb occurs 100 with header line.
    data : file_location type STRING.
    data : file_name like sy-datum.
    data : file_create type STRING.
    file_name = sy-datum .
    file_location = 'C:\xml\'.
    concatenate file_location file_name into file_create.
    concatenate file_create '.XML' into file_create.
    DATA: l_xml_table TYPE TABLE OF xml_line,
    l_xml_size TYPE i,
    l_rc TYPE i.
    select * from catsdb into table itab.
    append itab .
    CALL FUNCTION 'SAP_CONVERT_TO_XML_FORMAT'
    EXPORTING
    I_FIELD_SEPERATOR =
    I_LINE_HEADER =
    I_FILENAME =
    I_APPL_KEEP = ' '
    I_XML_DOC_NAME =
    IMPORTING
    PE_BIN_FILESIZE = l_xml_size
    TABLES
    i_tab_sap_data = itab
    CHANGING
    I_TAB_CONVERTED_DATA = l_xml_table
    EXCEPTIONS
    CONVERSION_FAILED = 1
    OTHERS = 24
    IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    CALL METHOD cl_gui_frontend_services=>gui_download
    EXPORTING
    bin_filesize = l_xml_size
    filename = file_create
    filetype = 'BIN'
    CHANGING
    data_tab = l_xml_table
    EXCEPTIONS
    OTHERS = 24.
    IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
    write : 'INTERNAL TABLE DATA IS SUCCESSFULLY DOWNLOADED TO LOCATION', file_create .
    Thanks.

  • I have an iPhone in my name and my wife has an iPad in her name. We have just ordered an iMac and wondered which account we should use to get the best out of all the devices

    I have an iPhone in my name and my wife has an iPad in her name. We have just ordered an iMac and wondered which account we should use to get the best out of all the devices? Can we use both accounts in iCloud?

    Yes you can.  However, it is better to use one person's account most of the time for purchases in the Mac App Store and iTunes since you can share purchases, such as songs, among various devices using the SAME account.  You Cannot share purchases among different accounts, though.   So make one of your accounts the main "purchasing" account so you can share purchases.
    Hope this helps

  • Previous month last date in sql

    hi
    i am using function
    select dateadd(day,1 - DATEPART(day,dateadd(month,24-36,GETDATE())),dateadd(month, 24- 36, getdate()))
    this will giveme 2014-03-01,
    now what changes  i need to make this function which will give me previous month last date.
    2014-02-28

    Hi coool_sweet,
    The answers in above posts are excellent. In case you may need any tip for date calculation, here is a good link for your reference.
    Date and Time Data Types and Functions - SQL Server (2000, 2005, 2008, 2008 R2, 2012)
    If you have any question, feel free to let me know.
    Eric Zhang
    TechNet Community Support

  • Getting previous month

    could you please tell me how to get previous month?
    using sysdate gives me current date. from that i can get current month using to_char function. But how can I get the previous month?

    All you ned to remember is that there are two date units, days and months. Everything else is a multiple/fraction of these.
    To increment
    n days:    SYSDATE + n
    n hours:   SYSDATE + n/24
    n minutes: SYSDATE + n/24/60
    n seconds: SYSDATE + n/24/60/60
    n weeks:   SYSDATE + n*7
    n months:  ADD_MONTHS(SYSDATE,n)
    n years:   ADD_MONTHS(SYSDATE,12*n)

  • Previous month end data for report

    Hi expert,
    I have to calculate previous month end data for my report.
    let say if user select 15 oct then he should be able to see 30 sept data.
    I have calander prompt.
    Thanks,

    Hi,
    Use presentation variable in date prompt.
    Apply sql filter(covert to sql) on report as date_column= TIMESTAMPADD(SQL_TSI_DAY,-DAYOFMONTH(date 'presntation_variable'),date 'presentation_variable')
    Refer : How to get LAST_DAY in obiee
    Regards,
    Srikanth

  • Getting Last Month End date

    Dear Experts,
    I want the following result. I have a date parameter. I want to
    get the last / end date of the previous month
    for any given date which the user inputs.
    For ex:
    If user puts the date 15.06.2008
    then i want to get the previous month end date i.e. 31.05.2008.
    The reason being i am creating a program for updating Opening and Closing Stocks of every month in a ZTABLE. The user will put the date in the parameter and system will bring the closing stock of previous month and then calculate for the current month.
    I hope i am not complicating matters too much....
    Basic funda is to arrive at the end date of the previous month for any date which the user puts.
    Please help me... it is mission critical..
    Thanks & Regards,
    Jitesh M Nair

    hi,
    use this to get last month end date.
    ex:
    data: d2 like sy-datum.
    d2 = sy-datum.
    d2+6(2) = '01'.
    d2 = d2 - 1. "prev mnth last date
    write:/ d2.

  • Getting year, month and date

    I am getting the current year, month and date as follows:
    public void setDate(Date date) {
    String strDate = "";
    //year
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    strDate += cal.get(Calendar.YEAR);
    //month
    strDate += cal.get(Calendar.MONTH) + 1;
    //date
    strDate += cal.get(Calendar.DATE);
    this.date = strDate;
    }This gives me the date in teh format yyyymd.
    i.e for Mar 5 2007, it returns 200735
    How can it retrieve it in yyyymmdd format?
    i.e. Mar 5 2007 must be 20070305

    use the function:
    private String getTwoNumberFormat(int i){
         if (i<10)
              return "0"+i;
         else
              return ""+i;
    }and then call
    //month
    strDate += getTwoNumberFormat(cal.get(Calendar.MONTH)+1);or use SimpleDateFormat :)

  • Fetching previous month's date

    Hii,
    Can anyone  provide me the code for retrieving firstWorkingDay for previous month ??

    Hi,
    Please check code:
    import java.text.*;
    import java.util.*;
    public class DateUtils {
      public enum IntervalType {  Month, Week  }
      private DateUtils() {  }
      public static Calendar[] getDateIntervals(IntervalType type, Calendar reference) {
        if (reference == null) {
          reference = Calendar.getInstance();
        Calendar startDate = (Calendar)reference.clone();
        Calendar endDate = (Calendar)reference.clone();
        if (type == IntervalType.Month) {
          // first date of the month
          startDate.set(Calendar.DATE, 1);
          // previous month
          startDate.add(Calendar.MONTH, -1);
          // first date of the month
          endDate.set(Calendar.DATE, 1);
          // previous month, last date
          endDate.add(Calendar.DATE, -1);
        else {
          // previous week by convention (monday ... sunday)
          // you will have to adjust this a bit if you want
          // sunday to be considered as the first day of the week.
          //   start date : decrement until first sunday then
          //   down to monday 
          int dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
          while (dayOfWeek  != Calendar.SUNDAY) {
            startDate.add(Calendar.DATE, -1);
            dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
          while (dayOfWeek != Calendar.MONDAY) {
            startDate.add(Calendar.DATE, -1);
            dayOfWeek = startDate.get(Calendar.DAY_OF_WEEK);
          // end date , decrement until the first sunday
          dayOfWeek = endDate.get(Calendar.DAY_OF_WEEK);
          while (dayOfWeek  != Calendar.SUNDAY) {
            endDate.add(Calendar.DATE, -1);
            dayOfWeek = endDate.get(Calendar.DAY_OF_WEEK);
        return new Calendar[] { startDate, endDate };
      public static void main(String[] args) {
        try {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          System.out.println("** previous month (relative today)");
          Calendar [] results = DateUtils.getDateIntervals(IntervalType.Month, null);
          System.out.println(sdf.format(results[0].getTime()));
          System.out.println(sdf.format(results[1].getTime()));
          System.out.println("** previous week (relative today)");
          results = DateUtils.getDateIntervals(IntervalType.Week, null);
          System.out.println(sdf.format(results[0].getTime()));
          System.out.println(sdf.format(results[1].getTime()));
          System.out.println("** previous month (relative jan 1, 2007)");
          results = DateUtils.getDateIntervals(IntervalType.Month,
            new GregorianCalendar(2007, 00, 1));
          System.out.println(sdf.format(results[0].getTime()));
          System.out.println(sdf.format(results[1].getTime()));
          System.out.println("** previous week (relative jan 1, 2007)");
          results = DateUtils.getDateIntervals(IntervalType.Week,
            new GregorianCalendar(2007, 00, 1));
          System.out.println(sdf.format(results[0].getTime()));
          System.out.println(sdf.format(results[1].getTime()));
        catch (Exception e) {
          e.printStackTrace();
        output :
        ** previous month (relative today)
        2008-06-01
        2008-06-30
        ** previous week (relative today)
        2008-06-30
        2008-07-06
        ** previous month (relative jan 1, 2007)
        2006-12-01
        2006-12-31
        ** previous week (relative jan 1, 2007)
        2006-12-25
        2006-12-31
    this will solve all your requirements!
    Thanks
    Pravesh

  • Previous month end date

    hello,
    i would like to find out the previous month end date when i give the input any date .
    finding previous month end date.
    eg :-> if i give the date as 02-Jan-2008, the result should be 31-dec-2007
    if i give the input date as 10-nov-2007, the previous month end date is 31-Oct-2007
    etc...

    ME_XE?select the_date, trunc(the_date, 'MM') - 1 as las_day_prev_month
      2  from
      3  (
      4     select add_months(sysdate, level) as the_date from dual connect by level <=12
      5  );
    THE_DATE                   LAS_DAY_PREV_MONTH
    02-FEB-2008 08 58:30       31-JAN-2008 12 00:00
    02-MAR-2008 08 58:30       29-FEB-2008 12 00:00
    02-APR-2008 08 58:30       31-MAR-2008 12 00:00
    02-MAY-2008 08 58:30       30-APR-2008 12 00:00
    02-JUN-2008 08 58:30       31-MAY-2008 12 00:00
    02-JUL-2008 08 58:30       30-JUN-2008 12 00:00
    02-AUG-2008 08 58:30       31-JUL-2008 12 00:00
    02-SEP-2008 08 58:30       31-AUG-2008 12 00:00
    02-OCT-2008 08 58:30       30-SEP-2008 12 00:00
    02-NOV-2008 08 58:30       31-OCT-2008 12 00:00
    02-DEC-2008 08 58:30       30-NOV-2008 12 00:00
    THE_DATE                   LAS_DAY_PREV_MONTH
    02-JAN-2009 08 58:30       31-DEC-2008 12 00:00
    12 rows selected.
    Elapsed: 00:00:00.12
    [pre]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • FOX to read previous month's data

    Hi Guys, i have the following FOX to calculate LINE '57' but cannot read previous month's data.
    please assist.
    DATA LINE TYPE ZBPLINE.
    DATA FISCPER TYPE 0FISCPER3.
    DATA PREV_MONTH TYPE 0FISCPER3.
    DO.
    PREV_MONTH = TMVL (FISCPER , -1) .
    {0BALANCE,FISCPER ,57} = {0BALANCE,PREV_MONTH,57} + {0BALANCE,FISCPER,12}.
    FISCPER  = TMVL(FISCPER , 1).
    IF FISCPER  > PREV_MONTH.
    EXIT.
    ENDIF.
    ENDDO.

    Lerato,
    I am not sure how is your data model, but here are a couple tips
    DATA LINE TYPE ZBPLINE.
    DATA FISCPER TYPE 0FISCPER3. ->>>>>  This sHould probably be 0FISCPER
    DATA PREV_MONTH TYPE 0FISCPER3. ->>>>>  This should probably be 0FISCPER
    data finalmonth type 0FISCPER3. ** or 0fiscper!
    finalmonth = TMVL(FISCPER , 12).
    *initialize you fiscper
    fiscper = '201001'. * or call a variable
    DO.
    PREV_MONTH = TMVL (FISCPER , -1) .
    {0BALANCE,FISCPER ,57} = {0BALANCE,PREV_MONTH,57} + {0BALANCE,FISCPER,12}.
    I am not sure what is 57 and 12. Is it your planning item?
    FISCPER = TMVL(FISCPER , 1).
    This exit if statement should be like this
    if FISCPER  > finalmonth.
    EXIT.
    ENDIF.
    ENDDO.
    Hope that helps
    Alex Zetune
    goldstrategy.com

Maybe you are looking for

  • Apache virtual host with OSX Server?

    I have been running OSX Server for a while now, and just recently I decided to get McMyAdmin for my Minecraft server. The web client for McMyAdmin runs on port 8080, but I wanted it on port 80. I wanted to use it with my domain, so I just created a s

  • Adding Visual custom component in spark DataGrid

    I am working with flex 4.5. I want to create Gauge component Datagrid. I am using open source com.betterthantomorrow.components. I have created custom components like this     <?xml version="1.0" encoding="utf-8"?>     <s:BorderContainer xmlns:fx="ht

  • Blend mode "colour" is greyed out !

    Hi there. I have a black and white photo which I wish to colourise. I've added a new blank layer which I wish to change the blend mode of to "colour" but the Hue, Saturation, Colour and Luminosity options are all "greyed" out. Why is this ?

  • Premiere Elements 11 Adding A Red Tint

    Premiere Elements 11 is adding a red tint to my video. It is fine before I put the file in elements and on the time line. It is only when I render and save it that the tint appears. I save in MPEG and use the HD 720p 30 preset. I have included some s

  • Cannot make Recovery DVDs for the G580 with Windows 8

    I notice that Windows 8 Notebooks are no longer being sold with Licenses affixed to the underside.  So if anything goes wrong (e.g. we have to replace the HDD at a later date)  we cannot just use a Windows 8 disk to restore the operating system. This