Select the last 7 days

How to select the last 7 days of my table, I use the query, but it is the calculation 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-10
I want to choose last 7 days
APRIL 26, 10
APRIL 27, 10
28 APRIL 10
APRIL 29, 10
30 APRIL 10
MAY 3, 10
4 MAY 10

kindly Guide

concerning

Hello

Use DENSE_RANK() rather than ROW_NUMBER().

with mytable
as
(
select 'abcFund' FND , to_date('09-MAY-10','dd-mon-rr') dp_date, 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
)
select dp_date
      from ( select dp_date
                  , dense_rank() over (order by dp_date desc) rn
               from mytable
           )
     where rn <= 7;

Courtesy: Rob SQL
* 009 *.

Published by: 009 on May 6, 2010 05:01

Tags: Database

Similar Questions

  • How to get the last business day of the previous month?

    Hi all

    We need the user as below,

    If the user select 18 June 2015 ' quick dashboard as the input value, and then they want to see last month last day of work (business date: 29-may-2015) amount in report, if you have an idea please share with us.thanks

    Note:

    use under request we can able to get the last day of the previous month, we want to for the last business day of the last month, based on the date of entry of the user?

    TIMESTAMPADD (SQL_TSI_DAY,-(1), TIMESTAMPADD (SQL_TSI_DAY, DAYOFMONTH ("DIM_TIME". ("" DataDate ") *-(1) + 1,"DIM_TIME ". (("" DataDate "))

    Thank you

    Deva

    Try this,

    case when Dayofweek (timestampadd (sql_tsi_day, Dayofmonth(Date '2015-06-15') *-1, Date ' 2015-06-15')) = 1 then timestampadd (sql_tsi_day, (Dayofmonth(Date '2015-06-15') *-1)-2, Date '' 2015-06-15) when Dayofweek (timestampadd (sql_tsi_day, Dayofmonth(Date '2015-06-15') *-1, Date ' 2015-06-15')) = 7 then timestampadd (sql_tsi_day, Dayofmonth(Date '2015-06-15') *-1-1, Date '' 2015-06-15) another timestampadd (sql_tsi_day, Dayofmonth(Date '2015-06-15') *-1, Date ' 2015-06-15') end

    As Ftsiot said in this post, it will work only to exclude the sat and Sun. Another one if you want to exclude regional holiday, you may need a calendar in DB table.

    Thank you

    AJ

  • I have photoshop cc 2015. For the last 2 days it does not open when I double click the icon. What should I do?

    I have photoshop cc 2015. For the last 2 days it does not open when I double-click the icon. What can I do to fix this?

    OK completely reboot Windows 7. When he started to look in the Task Manager under process for photoshop.exe it shouldn't be there. If it is highlighted and press the button end process. Now right-click the Photoshop icon and select run as administrator. Photoshop starts, you should see photoshop.exe in the list of running processes. When you close Photoshop it should disappear from the list.

  • Remove the last 35 days of a table data

    Hello

    I had a table to n days of data. I need to remove the last 35 days of data. My table has consecutive dates i.e. There were no load weekend or holidays...

    (1) Using INTERVAL command can simply calculate the difference. So it won't work.

    (2) I can use RANK to find the sequence. Is there a better way to write a logic to delete?.

    concerning

    KVB

    PurveshK,

    I think you have the right idea. Just get the ID of the innermost query.

    This should be quite effective, especially if there is an index on the date.

    drop table t;

    create table t:

    Select trunc (sysdate) - level * 2 double dt connect by level<=>

    delete t

    where rowid in)

    Select rn

    de)

    Select the rowid rn, dense_rank() over (order by dt desc) dr

    t

    )

    where dr<=>

    );

    Post edited by: StewAshton:

    Furthermore, the date field must be NOT NULL for the index be used - or even add WHERE DATE IS NOT NULL to the innermost query.

  • Last address and previous address in the last 30 days

    People in the morning.

    I need to know to all customers whose address has changed, based on the date of entry into force. The query should be based from the 1st June 12 effective date and not SYSDATE. So, we want to check the MAX (effective_date) is < = 1 June 12 and entry into force of the previous address is between 2 May 12 and June 1, 12 (30 days). This suggests that the address has changed. For the sake of the argument, the postal Code is enough to make the difference that the address has changed for now.


    According to the data, I only take customer 1002 but not 1001, 1003. With respect to the customer ID 1003, although his address has changed in the last 30 days, we aren't interested as the effective address June 1 has been good for a long time.

    In addition, even if the address for the customer ID 1001 changed twice in this period, I'm only interested for the last two addresses.

    Table CUST_ADDRESS
    CREATE TABLE CUST_ADDRESS
      (
        "CUST_NO"  VARCHAR2(4 BYTE) NOT NULL,
        "STREET"   VARCHAR2(20 BYTE),
        "CITY"     VARCHAR2(20 BYTE),
        "STATE"    VARCHAR2(2 BYTE),
        "ZIP_CODE" VARCHAR2(9 BYTE),
        "EFFECTIVE_DATE" DATE
      );
    INSERT the Script
    Insert into CUST_ADDRESS (CUST_NO,STREET,CITY,STATE,ZIP_CODE,EFFECTIVE_DATE) values ('1001','123 Picket Fences','Santa Ana','CA','92704',to_date('31-MAY-12','DD-MON-RR'));
    Insert into CUST_ADDRESS (CUST_NO,STREET,CITY,STATE,ZIP_CODE,EFFECTIVE_DATE) values ('1001','12259 Circular Dr','Irvine','CA','92705',to_date('10-APR-12','DD-MON-RR'));
    Insert into CUST_ADDRESS (CUST_NO,STREET,CITY,STATE,ZIP_CODE,EFFECTIVE_DATE) values ('1001','22 Green Road','Newport Beach','CA','92709',to_date('03-JAN-12','DD-MON-RR'));
    
    Insert into CUST_ADDRESS (CUST_NO,STREET,CITY,STATE,ZIP_CODE,EFFECTIVE_DATE) values ('1002','12 Parkside Dr','Chicago','IL','60601',to_date('20-MAY-12','DD-MON-RR'));
    Insert into CUST_ADDRESS (CUST_NO,STREET,CITY,STATE,ZIP_CODE,EFFECTIVE_DATE) values ('1002','3200 Court Road','Indianapolis','IN','46201',to_date('02-MAY-12','DD-MON-RR'));
    
    Insert into CUST_ADDRESS (CUST_NO,STREET,CITY,STATE,ZIP_CODE,EFFECTIVE_DATE) values ('1003','24 Jaeger St','Bellingham','WA','98225',to_date('01-JUL-12','DD-MON-RR'));
    Insert into CUST_ADDRESS (CUST_NO,STREET,CITY,STATE,ZIP_CODE,EFFECTIVE_DATE) values ('1003','145 NE 38th St','Fort Lauderdale','FL','33334',to_date('18-JUN-12','DD-MON-RR'));
    Insert into CUST_ADDRESS (CUST_NO,STREET,CITY,STATE,ZIP_CODE,EFFECTIVE_DATE) values ('1003','212 Coral Drive','Miami','FL','33010',to_date('01-JAN-95','DD-MON-RR'));
    Thanks to a bouquet.

    Published by: Roxyrollers on July 26, 2012 09:35

    Something like that

    SQL> ed
    Wrote file afiedt.buf
    
      1  select *
      2    from (
      3  select ca.*,
      4         max(effective_date) over (partition by cust_no) max_eff_date,
      5         lag(effective_date) over (partition by cust_no order by effective_date) prior_eff_date
      6    from cust_address ca)
      7   where prior_eff_date between date '2012-05-02' and date '2012-06-01'
      8*    and max_eff_date <= date '2012-06-01'
    SQL> /
    
    CUST STREET               CITY                 ST ZIP_CODE  EFFECTIVE MAX_EFF_D PRIOR_EFF
    ---- -------------------- -------------------- -- --------- --------- --------- ---------
    1002 12 Parkside Dr       Chicago              IL 60601     20-MAY-12 20-MAY-12 02-MAY-12
    

    Justin

  • How to select the last 20 transactions in a table

    Hi guys,.
    I have a table named INOUT with following columns

    EMPLOYEECODE VARCHAR2
    DATE OF THE RESPONDENT


    each day employees now their me 'in '. then entered came and came in this table. If we want to select the last 20 EMPLOYEECODE in this table that make their selves 'in '. How to choose that? just 20 last employees who traded. Help, please

    Hello

    This is called a Query Top - N , you want to choose elements of N (N = 20, in this case) from the top of a sorted list.
    Here's a way to do it:

    WITH     got_r_num     AS
    (
         SELECT       emloyeecode
         ,       MAX (intime)     AS latest_intime     -- If wanted
         ,       RANK () OVER (ORDER BY  MAX (intime)  DESC)
                        AS r_num
         FROM       inout
         GROUP BY  employeecode
    )
    SELECT     employeecode
    ,     latest_intime          -- If wanted
    ,     r_num               -- If wanted
    FROM     got_r_num
    WHERE     r_num     <= 20
    ;
    

    That actaully could take more than 20 people: If there be a tie, it includes any person who is entitled to be in the top 20. If you want exactly 20 people, then add the columns of tiebreaker to analytical or use ROW_NUMBER instead of RANK ORDER BY clause.

  • List of the last seven days of sysdate

    Hello

    Anyone, please understand me how to achieve this result defined in oracle?

    I want to display the last seven days from the current date.




    Thank you
    Madam.

    Try this please

    select SYSDATE - level from dual connect by level <=7
    
  • How to set up the filter to provide data for the last 7 days

    Hello

    In our environment of BAM Oracle 11 g, we have a data object that stores a date with the datatime data type field. In the report of BAM, we want to display data only for the last 7 days. Not sure how we can configure the filter to "during the" last 7 days

    Can someone help how to achieve this requirement?

    Thank you.

    Please try the following-
    (1) open the chart bar in edit mode.
    (2) in the editor view, click on the button 'Properties', then go to the tab "Format of the value.
    (3) in the tab "Format of the value", to "apply to" drop-down list, select your datetime field.
    (4) in the contiguous list of 'Catégories', select 'TimeUnit' as the category.
    (5) in the following list of 'Type', select 'day of the week.
    6) click OK/application to see the relevant changes.

    Now you should be able to see the day of the week on the horizontal axis 'Thursday', 'Saturday', etc.

  • calculate data for the last 30 days according to the date of sys

    SELECT DT, ITEMCODE, ITEMNAME, QTY
    FROM   TABLE1
    In above given the query, how do I calculate the details of the amount of the last 30 days, APR, MAY, and JUNE

    As the month during JULY, August, the amount of detail is required for the last 30 days (from sysdate), MAY, JUNE, July

    Please suggest.

    Yogesh

    Published by: user12957777 on July 28, 2010 03:16

    Hello

    CASE expressions can filter data in specific 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, in addition, if you want to separate for each itemname (for example) lines.

    I guess some things, like what do you mean by the last 30 days.

    If you wish to post a few 9creation TABLE or INSERT sample data) and the results you want from this data, then I could test this.

  • Firefox asking me permission open browser to change my pc by allowing the app to make changes. Past the last 2 days.

    The last 2 days, when I open my firefox browser a pop up appears asking permission to an application of firefox to make changes to my computer. I've never seen a fire Fox app front.

    Should say that until 2 weeks ago, firefox has been my default browser, and then has a crash of windows. Since then, I use but not him have not set by default. Too complicated & too worried, it could happen again. This would be part of the problem?

    I have no objection to the application, but want to make sure that it is safe, please?

    Thank you.

    A possible cause of this message, it is that Firefox is configured to run as administrator.

    Make sure that Firefox is not set to run as administrator.

    Right click on the desktop Firefox shortcut and choose "Properties".

    Make sure that all items are disabled in the tab "Compatibility" in the Properties window.

    • Privilege level: "Run this program as Administrator" should not be selected
    • "Run this program in compatibility mode for:" must not be selected
  • Why machine did transfer spontaneously inbox the last 15 days in a folder named Archive / 2015?

    I got lucky that I could find where the machine had hidden messages the last 15 days.
    Am I punished for having too many messages in the box at, or what?

    Hello, you're posting in the firefox support forum - firefox is a browser and does not handle your emails. in case you use thunderbird as a mail client and you have a question about this, please say so, then we will go to your question in the queue of thunderbird.
    If you access your email via webmail, please contact your e-mail provider to help you...

    Problems with email and how to find help

  • For the last 2 days everytime I open to MY MSN, it opens in safe mode. I deleted cookies and the history of FF. all other sites open OK. Vista, 64-bit operating system.

    Vista 64 bit OS

    Thanks for the replies, the problem was on the Microsoft site itself and internal glitch for the last 2 days on the garbled causing MY MSN page without graphics and text.

  • the last two days, I get an error message and Firefox does not open. I'm on 9.0

    The last two days, Firefox will not load. I get an error screen. I'm on 9.0

    Thanks for the help. All is well in Tennessee!

    Gary

  • I have the time to default iPhone 4 iOS 7.1.2 iPhone App not updated since the last 3 days and also checked all the settings for location and also set as new iPhone always present problem... Please try to fix... Thanx

    I have the time to default iPhone 4 iOS 7.1.2 iPhone App not updated since the last 3 days and also checked all the settings for location and also set as new iPhone always present problem... Please try to fix... Thanx

    Turn off your device and turn it on again. If this does not help, sign out of your account and reconnect.

    In addition, you can try to reset your settings.

    • Press and hold the sleep/wake button
    • Press and hold the Home button
    • Press and hold both buttons until the display turns off and on again with the Apple logo on the subject.

    Alternatively, you can go to settings - general - reset - Reset all settings

  • 16 June updated blown all my documents for the last 10 days. Any solution?

    Documents for the last 10 days, all my email settings, all memory of recent activity disappeared.

    Hello Craig,.

    Let's see if the files are actually missing or if they are hidden:

    http://answers.Microsoft.com/en-us/Windows/Forum/Windows_7-files/Unhide-files-and-folders/ca46d3ba-1b51-E011-8dfc-68b599b31bf5

    Best regards

    Matthew_Ha

Maybe you are looking for