Select Last 7 days

How can I select last 7 days from my table, I use query but it is calculating according to sysdate
It is the data of my talble
DP_DATE
03-APR-10
21-APR-10
22-APR-10
23-APR-10
26-APR-10
27-APR-10
28-APR-10
29-APR-10
30-APR-10
03-MAY-10
04-MAY-10
11 rows selected.
  select distinct dp_date
  from dailyprice
  where trunc(dp_date) between trunc(sysdate-7) and trunc(sysdate)
this query is returning
DP_DATE
29-APR-10
30-APR-10
03-MAY-10
04-MAY-10I want to select last 7 days
26-APR-10
27-APR-10
28-APR-10
29-APR-10
30-APR-10
03-MAY-10
04-MAY-10
kindly guide
regards

Hi,
Kame wrote:
with t
as
select 'abcFund' FND , to_date('09-MAY-10','dd-mon-rr') dpdate, 10 price from dual union all
select 'dddFund', to_date('09-MAY-10','dd-mon-rr'), 12 from dual union all
select 'abcFund', to_date('09-MAY-10','dd-mon-rr'), 12 from dual union all
select 'edfFund', to_date('09-MAY-10','dd-mon-rr'), 12 from dual union all
select 'eeeFund', to_date('09-MAY-10','dd-mon-rr'), 12 from dual union all
select 'dddFund', to_date('08-MAY-10','dd-mon-rr'), 12 from dual union all
select 'abcFund', to_date('08-MAY-10','dd-mon-rr'), 12 from dual union all
select 'edfFund', to_date('08-MAY-10','dd-mon-rr'), 12 from dual union all
select 'eeeFund', to_date('08-MAY-10','dd-mon-rr'), 12 from dual union all
select 'eeeFund', to_date('08-MAY-10','dd-mon-rr'), 12 from dual union all
select 'dddFund', to_date('07-MAY-10','dd-mon-rr'), 12 from dual union all
select 'abcFund', to_date('07-MAY-10','dd-mon-rr'), 12 from dual union all
select 'edfFund', to_date('07-MAY-10','dd-mon-rr'), 12 from dual union all
select 'edfFund', to_date('05-MAY-10','dd-mon-rr'), 11 from dual union all
select 'dddFund', to_date('05-MAY-10','dd-mon-rr'), 12 from dual union all
select 'eeeFund', to_date('05-MAY-10','dd-mon-rr'), 12 from dual union all
select 'dddFund', to_date('04-MAY-10','dd-mon-rr'), 12 from dual union all
select 'abcFund', to_date('04-MAY-10','dd-mon-rr'), 12 from dual union all
select 'dddFund', to_date('03-MAY-10','dd-mon-rr'), 12 from dual union all
select 'abcFund', to_date('03-MAY-10','dd-mon-rr'), 12 from dual union all
select 'edfFund', to_date('03-MAY-10','dd-mon-rr'), 12 from dual union all
select 'eeeFund', to_date('03-MAY-10','dd-mon-rr'), 12 from dual union all
select 'dddFund', to_date('01-MAY-10','dd-mon-rr'), 12 from dual union all
select 'abcFund', to_date('01-MAY-10','dd-mon-rr'), 12 from dual union all
select 'abcFund', to_date('30-APR-10','dd-mon-rr'), 11 from dual
)Hot to get my result from this data.What result is that?
Do you mean what you posted in your first message?
I want to select last 7 days
26-APR-10
27-APR-10
28-APR-10
29-APR-10
30-APR-10
03-MAY-10
04-MAY-10The last thing I saw posted
this query will not work in this situation
select dp_date
from ( select dp_date
, row_number() over (order by dp_date desc) rn
from mytable
where rn <= 7ROW_NUMBER will get the 7 most recent rows , whether they all have the same dp_date, 7 different dp_dates, or anything in between.
DENSE_RANK will get the 7 most recent values , whether they come from 7 rows or more.
select DISTINCT
     dp_date
from      (
          select      dp_date
          ,     DENSE_RANK () over (order by dp_date desc)      AS rn
          from      mytable
where      rn     <= 7If you want the 7 most recent calendar days, then use TRUNC (dp_dtate) in the analytic function:
DENSE_RANK () over (order by TRUNC (dp_date) desc)      AS rnThis will count (for example) these 3 distinct DATE values:
6-May-2010 07:00
6-May-2010 08:10
6-May-2010 23:59
all as one value.

Similar Messages

  • 1st & last day of month based on month selected

    Hi All,
    I am an infant in BO. I want to create a variable in which I need to compare Order Creation Date with First Day of Month and Last Day of Month.
    I have gone through the posts here regarding this, I found huge no of posts on calculating the 1st & last day of month based on a date value. But,
    I don't have any date objects in the universe level, instead I have only Month Name/Month Number objects in the universe level (as it is a monthly report)
    So, How to create 1st day & last day of month based on the month selected.
    That is, If I run the report for FEB 2011 (Prompt values), I need the outputs as 01/02/2011 & 28/02/2011 as my 1st & last date values respectively.
    I am working on BOXI R 3.1
    Thanks in Advance,
    Mitch

    Hi
    Create a variables and write a formula like
    Also add Month number Object in query level
    Var_Month
    =userresponse("Enter Month")
    Var_A
    "01/"MonthNumberObj+Right(var_Month;4)
    var_First date
    Todate(var_A;"dd/MM/yyyy")
    var_lastdate
    =lastdayofmonth(var_Firstdate)
    Note: check the default format for date according to ur database like MM/dd/yy or something like that . and add in the var_firstdate
    regards
    Sunil

  • SQL select Statement -first day and last day of the month - 1 year from now

    Hi,
    I need to write a SQL to get the dates in between first day and last day of the month one year from now.
    SELECT last_day(add_months(sysdate,12)) as lastday from dual
    What could be the Query to get the first day of the month one year from now..
    ie ..Sysdate - 3-DEC-2009
    Result - 1-DEC-2010
    thank you

    Hi,
    You can use TRUNC with 2 arguments to get the first DATE in a month, year, quarter, week, hour, minute, ISO year, ...
    SELECT  TRUNC ( ADD_MONTHS ( SYSDATE
                               , 12
                  , 'MONTH'
                  )     AS first_of_month
    FROM    dual
    ;The DATE returned will be in the same month, year, quearter, ... as the first argument.
    \We convered the last day of the month in [your previous question|http://forums.oracle.com/forums/message.jspa?messageID=3942939#3942939].
    At that time, I warded about using LAST_DAY as a cutoff point; TRUNC is a much better way.
    For example, to find all appointment_dates in the current month next year:
    SELECT  *
    FROM    appointments
    WHERE   appointment_date >= TRUNC (ADD_MONTHS (SYSDATE, 12), 'MONTH')
    AND     appointment_date <  TRUNC (ADD_MONTHS (SYSDATE, 13), 'MONTH')Note that
    the first part of the WHERE clause calls for dates on or equal to the beginning of the 12th month in the future, but
    the second part of the WHERE clause calls for dates before, not equal to , the beginning of the 13th month in the future.

  • Select statement for last day of the last quarter and first quarter

    Anybody provide me the single select statement which should result the last day of the last quarter and last day of current quarter?
    Ex: Select last date of the last quarter,last day of current quarter from XYZ(DUAL...)
    Thanks in Advance

    Hi,
    Here's one way:
    SELECT               TRUNC (SYSDATE, 'Q') - 1     AS prev_qtr_end
    ,     ADD_MONTHS ( TRUNC (SYSDATE, 'Q') - 1
                 , 3       
                 )                    AS this_qtr_end
    FROM    dual;TRUNC (dt, 'Q') returns the first day of the quarter containing dt.
    Subtract one day from that, and you have the last day of the previous quarter.
    Add 3 months to that, and you have the last day of the current quarter. ADD_MONTHS (dt, n) will always return the last day of a month if its first argument, dt, is the last day of a month.

  • How to select last year's first day

    Hi friends,
    How to get the last year's first day using sysdate as on sysdate
    this gives the current year's start date
    I need last year's start date like 01-jan-2010 as on sysdate
    thanks

    Another:
    SQL> select trunc(sysdate-365.25,'YYYY') from dual;
    TRUNC(SYSDATE-365.25
    01-JAN-2010 00:00:00Note: the .25 accounts for this query being run on the last day of a leap year e.g.
    SQL> select trunc(date '2012-12-31'-365.25,'YYYY') from dual;
    TRUNC(DATE'2012-12-3
    01-JAN-2011 00:00:00Edit: Note, this still isn't completely accurate and could give a wrong result. Better solution is to use the add_months function.
    Edited by: BluShadow on 06-Apr-2011 11:55

  • How to get the last day according to fiscal period input in selection scree

    Hello expert
    how to get the last day of fiscal period input.
    the fiscal period inculdes 1-16
    when fiscal period is greater than 12, only calculate the last day of 12nd month
    your solution will be apprecaited, FM existing?
    thank you
    Kevin

    Hi,
    when you give a particular date in any month
    the following fm will give you the last date of that month
    here you can give
    R_FDATE-HIGH  as 01 and month as the period you wnat and year for current year
    concatenates '01'  month year  into r_fdate-high separated by '.'.
    then it will give g_ltdt for that month and year which wil be the last date of that month
        CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
          EXPORTING
            DAY_IN            = R_FDATE-HIGH
          IMPORTING
            LAST_DAY_OF_MONTH = G_LTDT
          EXCEPTIONS
            DAY_IN_NO_DATE    = 1
            OTHERS            = 2.
        IF SY-SUBRC <> 0.
          MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                  WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
        ENDIF.
    thanks & regards,
    Venkatesh

  • 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

  • How to get the last day of the payroll period

    Hi all,
    I need to get the last day of the payroll period e.g. last day of Jan 2007 is 31 Jan 2007. Can anyone suggest as to how to get it?
    Thanks,
    Madhu

    T549S contains the payroll periods with pay date.  T549Q contains the begin and end dates of the payroll period.
    You can select from the appropriate table (or from both depending on your given data) to get the end date of the period.
    Hope that helps.
    Mary

  • How to get the last day of the week?

    Hii
    i can get the calender week number for any given date using
    SELECT to_char(to_date('04/04/2011','MM/DD/YYYY'),'WW') FROM dual
    can any body tell me, how to get the last day of that week ?
    and the answer should be 04/08/2011(8th april )
    thanks
    San
    Edited by: sandeep9 on Apr 4, 2011 3:50 AM

    Hi, San,
    Here's one way:
    WITH     sample_data     AS
         SELECT  DATE '2011-04-04'     AS dt
         FROM     dual
    SELECT  dt
    ,     TO_CHAR (dt, 'WW')     AS week_num
    ,     NEXT_DAY ( dt - 1
               , TO_CHAR ( TRUNC (dt, 'YEAR') - 1
                      , 'Day'
               )          AS end_o_week
    FROM     sample_data;Another way is to use date arrithmetic:
    WITH     sample_data     AS
         SELECT  DATE '2011-04-09'     AS dt
         FROM     dual
    SELECT  dt
    ,     TO_CHAR (dt, 'WW')     AS week_num
    ,     TRUNC (dt, 'YEAR')
          + (7 * CEIL ( (dt - (TRUNC (dt, 'YEAR') - 1))
                / 7
          - 1               AS using_date_arithmetic
    FROM     sample_data;

  • Last day of each month in a year based on a Input date?

    Hi all,
    I have a request from a customer who wants to have a yearly report created in BEx Query Designer which starts in January up to December and they want to have a Month To Date (MDT) and Year to Date (YTD) calculation of a key figure. This report will only show one year at the time based on the date the user give as input at the Variable Screen.
    This key figure should be calculated by using the last date of each month for input when performing the MTD and YTD calculation and summarizations.
    Example:
    January: 31.01.2008  Key Figure = 1000. MTD = 1000 and YTD = 1000
    February: 29.02.2008  Key Figure = 2500. MTD = (2500-1000) 1500 and YTD = 2500
    March: 31.03.2008  Key Figure = 6000. MTD =(6000-2500)=3500 and YTD = 6000
    Etcu2026.
    This means that I have to have a lot of hidden Key figures which gets restricted on the different month end dates and formulas to calculate the different MTD and YTD results for the months as the year progresses.
    The way I have solved it now is that I have a Customer Exit which gives me the Last day of last year (Exp: 31.12.2007) based on input date and use a Offset on the last day of last year date to get the different last dates of each month.
    Since we have a leap year (one extra day in February) this year, the offset to calculate the end month dates will be different for 2007 and next year (2009). This solution is not very flexible, and it will not give the correct MTD and YTD if the customer wants to go back to 2007 and off course next year (2009).
    One solution is to create 12 Customer Exits that gives me the different month end dates (January-December), and also takes in account leap year for February for the different years. These Exits will be based on the Input date the Customers put in at the variable screen.
    I would rather want to avoid making 12 new customer exits and want your advice and expertise to find out if this is possible in any other way (maybe with only 1 customer exit) to get the last date of each month based on an input date.
    Thanks for all your advices on beforehand.
    Regards
    Oddmar Lid

    Hi,
    Thanks for you replay and documentation, but it doesn't give me excatly the functionality I'm after. The only MTD calculation code the document provides is the calculation of the following functionality:
    "Month to Date (MTD) u2013 From the 1st of month to u201CKey Dateu201D - for current year." This doesn't give the the functionality I want, which is to retrieve a given vaule for a Key Figure for the last dates of the months in a year to calculate MTD and YTD.
    This way I have created the query is to have multiple hidden Key Figures that calculates the MTD and YTD.
    In Columns in Query Designer it will look something like this:
    Selection: MTD January--> Date = 31.01.2008, Key Figure (Always Show)
    Selection: YTD January --> Date= 31.01.2008, Key Figure (Always Show)
    Selection: MTD February --> Date= 29.02.2008, Key Figure (Always Hide)
    Formula: MTD February --> MTD February - MTD January (Always Show)
    Selection YTD February --> Date = 29.02.2008, Key Figure (Always Show)
    And so on....
    This is off course a simplified version, but it shows the core of the solution. What I want to achive here is that the dates used to get the Key Figures (last date of a month) is calculated as flexible as possible, so that the users can go back and forth in time without worrying about the leap year problem and so on. I have now used an offset from the last date of last year and this is good for all the normal years, but when it is a leap year the query will use wrong dates to get the key figure vaule for the last date of each month.
    Any ideas on how to achieve this without creating 12 different Customer Exit variables (one for each end date of each month)?
    Thanks
    Regards
    Oddmar Lid

  • Last Day of Fiscal Calendar

    Hi All,
    Im am trying to modify an initialization block to get the start dates and end dates of Prior Fiscal Year and Prior Fiscal Year - 2 Years Ago.
    Fiscal Year starts on 01-JUN each year and ends on 31-MAY of following year
    I already have a field called fiscal_year_start_date and using select statement
    SELECT ADD_MONTHS(fiscal_year_start_date,12) FROM TABLE.
    Im not getting the last day of the fiscal year which should be 31-MAY but instead getting 01-JUN. How can I change this.

    Hi,
    If you want to get a day before 1-Jun i.e. 31-May then you can subtract 1 from the date.
    SELECT ADD_MONTHS(fiscal_year_start_date,12) - 1 FROM TABLE
    Thanks

  • How to get last day of prev month?

    SELECT to_char(SYSDATE, 'MM') -1, last_day(sysdate)
    FROM DUAL;
    If current month is April, How to get the last day of March? Thanks.

    SQL> select sysdate, trunc(sysdate, 'mm') - 1 from dual ;
    SYSDATE     TRUNC(SYSDA
    06-APR-2004 31-MAR-2004
    1 row selected.
    SQL>

  • How to get last day of a month

    using pl/sql
    What i need is that all members whose birthdate is in the curren month will be included as having had their birthdate regardless of the date of the month. The code i have is:
    Select me607.subscriber_id
    me607.mbr_dob,
    ** trunc(months_between (*end of current month*,me607.mbr_dob) / 12) as age
    from odw.mbr607_member_expanded me607
    The asterixed line is what I need to change. If I put the end of the current month (where I put 'end of current month') then all those whose birthday month is in Feb will show as having had their birthdate. So if run today, folks whose birthdate is 26-feb-2005 will show an age of 5.
    When grabbing the last day of the month using visual basic I just use the first day of the subsequent month minus 1. Any assistance is appreciated..

    Hi,
    This does what you requested:
    Select  me607.subscriber_id,     -- Don't you need a comma here?
         me607.mbr_dob,
         ROUND ( MONTHS_BETWEEN ( SYSDATE
                                , me607.mbr_dob
               / 12
    from    odw.mbr607_member_expanded     me607
    WHERE     TO_CHAR (me607.mbr_dob, 'MONTH') =
         TO_CHAR (SYSDATE,      'MONTH')
    ;If this is run sometime in February, 2010, and mbr_dob is sometime in Febrary, 2005, then the result of MONTHS_BETWEEN will be between 59 and 61. When we divide by 12 convert that to years, that difference of up to one month becomes so small that it will be lost in the rounding.
    If this isn't exactly what you need, tthen post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
    Edited by: Frank Kulash on Feb 17, 2010 1:32 PM
    Added explanation.

  • If Current Month then use last day, else use month

    My cube is setup where the current month is at a daily level and the previous months just contain the last day of data.
    I would like to for the last month to only return the last day and return the month level for all previous months. The reason being is our third party software gives us the capability of doing a drillthrough, but when doing it for the latest month, it is
    returning every day in that month.
    Here is my attempt, but it is only returning the last month, not the rest of the months.
    select [Measures].[Current Balance]
    on 0, non empty
    [Date].[Month].children
    on 1
    from [MyCube]
    where
    IIF([Date].[Month].CurrentMember = ClosingPeriod([Date].[Month].[Month]),
    TAIL (NonEmpty([Date].[Date].members,[Measures].[Count Of Accounts]),1),
    [Date].[Month].CurrentMember)

    Hi Dodge,
    I’m writing to follow up with you on this post. Was the problem resolved by the above query?
    If the issue was solved, I’d like to mark this issue as "Answered". Please also feel free to unmark the issue, with any new findings or concerns you may have.
    Thanks,
    Charlie Liao
    TechNet Community Support

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

Maybe you are looking for