How to read only the most recent records in PL/SQL

Oracle version: 11.2

I have a table in ORACLE called RATES and it is EFFECTIVE_DATE, RATE_NAME, RATE_AMOUNT


RATE
------------
1 RATE_ONE 01/01/2000 0.10
2 RATE_ONE 01/05/2005 0.15
RATE_ONE 12/01/2010 3, 0.20
RATE_TWO 4 10/15/2009 0.33
5 RATE_THREE 07/09/2011 0.05
6 RATE_THREE 31/12/2011 0.06

A rate can be there several times, but the most recent rate is the rate of the asset. I need to query this table and want only to bring the currently active lines:
DOCUMENTS 3, 4 and 6

How can I do this in the most elegant way?

I can say:

SELECT RATE WHERE trunc (sysdate) between EFFECTIVE_DATE and trunc (sysdate);

(it will not work. I don't know how to do this without a date column exp and then I could tell
SELECT RATE WHERE trunc (sysdate) between EFFECTIVE_DATE and end_date

The problem is that I can't add this column at this time, so need to find a way to do it by programming

Published by: userLynx on February 1, 2012 08:56

Hello

Here's one way:

WITH     got_r_num     AS
(
     SELECT     rate.*          -- or list columns you want
     ,     RANK () OVER ( PARTITION BY  rate_name
                      ORDER BY          effective_date     DESC
                    )           AS r_num
     FROM    rate
--     WHERE     ...          -- If you need any filtering, put it here
)
SELECT     *          -- Or list all columns except r_num
FROM     got_r_num
WHERE     r_num     = 1
;

If you would care to post CREATE TABLE and INSERT statements for your sample data, and then I could test this.

If the combination (rate_name, effective_date) is not unique, this includes all rows that have the last effective_date for this rate_name. If you want exactly one line per rate_name when there is a tie, then add the columns of tiebreaker to analytical or use ROW_NUMBER instead of RANK ORDER BY clause.

Tags: Database

Similar Questions

  • How to find the most recent record?

    How to find the most recent record?

    Hello

    It is part of our Bill the elements table, retrieved by the following query:

    SELECT movimento, dt_movimento, cd_material,
    cd_tp_operacao, nf, series,
    sole, pr_unitario
    Of itemfat
    WHERE cd_material IN ('722136', '759177', ' 810725', 818848')
    AND cd_tp_operacao ("' 1102 c ' ', ' 2102 C")
    ORDER BY cd_material,
    dt_movimento


    MOVIMENTO DT_MOVIMENTO CD_MATERIAL CD_TP_OPERACAO NF SERIES AS PR_UNITARIO
    2008-03-11 2137625, 722136 2102 1084738 3 120 2 915
    2588890 08/05/2008 722136-2102 1090987 3 120 2,5615
    755522 09/10/2007 759177-2102 101449 1 200 0.6
    701055 03/10/2007 810725-2102 1013865 1 36 10.9
    1588804 20/12/2007 810725 2102 1014182 1 96 10.9
    1305653 03/12/2007 818848-2102 108555 1 240 6.5
    3102134 07/18/2008 818848 2102 109784 1 360 6.5


    Seems that I whant to retrieve only the most recent records for each item, as:

    MOVIMENTO DT_MOVIMENTO CD_MATERIAL CD_TP_OPERACAO NF SERIES AS PR_UNITARIO
    2588890 08/05/2008 722136-2102 1090987 3 120 2,5615
    755522 09/10/2007 759177-2102 101449 1 200 0.6
    1588804 20/12/2007 810725 2102 1014182 1 96 10.9
    3102134 07/18/2008 818848 2102 109784 1 360 6.5


    Can someone help me with a sentece SQL to do?

    Thanks in advance.

    You can certainly do it:

    SELECT   movimento, dt_movimento, cd_material, cd_tp_operacao, nf, serie,
             quantidade, pr_unitario
        FROM itemfat i
       WHERE cd_material IN ('722136', '759177', '810725', '818848')
         AND cd_tp_operacao IN ('1102C', '2102C')
         AND dt_movimento =
                (SELECT MAX (dt_movimento)
                   FROM itemfat d
                  WHERE d.cd_material = i.cd_material
                    AND d.cd_material IN ('722136', '759177', '810725', '818848')
                    AND d.cd_tp_operacao IN ('1102C', '2102C'))
    ORDER BY cd_material, dt_movimento
    

    : p

  • How can I get the most recent copy of the SAS 70 of EchoSign reports?

    How can I get the most recent copy of the SAS 70 of EchoSign reports?

    Hello Emilyp96102994,

    I recommend to open a ticket with the support that they can provide more detailed information about:

    Support | services of Adobe eSign

    -Usman

  • How to turn off the most recent list used to term & other apps

    were if you search the registry problem disable MRU in all applications, especially the run command on the start menu, but I would like to eliminate all the other apps as well

    I found several solutions including one that mentions comdlg32, others who need to add fields to the register, any other fair value dword

    Is there a single solution or do I need a combination of solutions

    I didn't do this, but here I found at least 5 versions, one of which is correct & universal
    :
    1. [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Comdlg32]
    "NoFileMru" = DWORD: 00000001

    2.HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
    Value name: NoRecentDocsMenu
    Value type: REG_DWORD
    Value data: 0 or 1
    0 = Yes, allow these recent docs
    1 = no, do not allow any recent docs

    Value name: ClearRecentDocsOnExit
    Value type: REG_DWORD
    Value data: 0 or 1
    0 = no does not clear
    1 = Yes, clearly their

    Value name: NoRecentDocsHistory
    Value type: REG_DWORD
    Value data: 0 or 1
    0 = historic Yes
    1 = no history

    3 turn off the history:
    Click Start--> Run--> type "regedit" in the Run dialog box.
    When in the registry, locate the following key:
    HKEY_CURRENT_USER > Software > Microsoft > Windows > CurrentVersion > Explorer > RunMRU.
    Right click on the RunMRU key and choose "Permissions."... ».
    Click on "Add..." ».
    Type "Everyone" and click OK in the box that opens.
    In the permissions (security) dialog box, select all and then deny read access.
    Click OK.
    Close the registry editor. You may need to restart your computer for the changes to take effect

    ' 4 ' most recently used ' list - how to disable file
    Previous

    The information in this article applies to:

    WinWay RESUME 9.0 and versions later

    Summary

    Like many other Windows applications, the menu 'File' of WinWay RESUME luxury offers easy access to read files recently. This article explains how to remove this list.
    More information

    Windows offers a system policy to disable the file 'most recently used' lists for applications in accordance with this particular policy standard.
    To turn off the "most recently used" list of files, download and run this file.
    To restore the list, download and run this file.

    Notes:

    This setting affects several applications, not only WinWay RESUME
    This setting applies only to the user currently logged on to the computer. You can repeat these steps for the other connections if necessary

    For advanced users

    The parameters above can be entered through the Publisher of the registry as well.

    Select Start, run, Reg Edit
    Navigate to this registry key:
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
    To disable the "recently used file list", adds the value:

    Name: NoRecentDocsHistory
    Type: DWORD
    Value: 1

    To activate the "recently used file list", right-click on the 'NoRecentDocsHistory' and select 'delete '.

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion \Policies\Comdlg32 key. 2. create a REG_DWORD value called NoFileMru and set the value to 1.

    5.
    Type regedit in the box RUN, and then press ENTER. It will open the registry editor.

    2. now go to following keys successively:

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer
    HKEY_USERS\.default\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer

    In the right pane, look for a DWORD 'ClearRecentDocsOnExit '. Its value could be set to 1. Set its value to 0 or delete it completely.

    The recent documents menu can be disabled easily by editing the registry. To do this, go to the following key:
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
    Now in the right pane, create a new DWORD with the name: NoRecentDocsMenu and set its value to 1. Restart the browser to save the changes.
    You can also clear the RUN MRU history. All ads are stored in the key:
    HKEY_USERS\. Default\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU
    You can remove individual entries or the entire list. To remove history to find inscriptions date on:
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Doc find Spec MRU and delete.

  • How do you get the most recent backup that always has this app on it?

    My 2 years has removed an application in the course of the past week. He regularly plays with my phone, so I have no idea when he managed to do so. I do iCloud backups every night and have this updated app to make backups as well. Is there a way to check the applications included in backups to restore the most recent backup with this app still on it and I hope that the latest data? The only option I can think of right now is to keep remove and restore my phone until I get to a backup with the application, but this seems incredibly long.

    You can see a breakdown of the contents of the backup of the last backup. If you know that it has been deleted some time in the past week, restore a backup from a week ago. If there was some new data on the phone since then you do not want to lose, then Yes, you would probably try the trial and error method.

  • I have to keep each backup set or only the most recent

    Original title: backup sets

    I use backup and restore to make a weekly backup of my files.  A new set of files is created every week, but the size varies.  Each new set of backup files is complete or each new lot only contains the updated items?  I have to keep each backup set or just the most recent?

    Hello

    It depends on what type of backup you do?

    If you did a full backup of the system simply perform a differential or incremental backup on a regular basis.

    Differential backup

    A differential backup copies files that were created or changed since the last normal or incremental backup. It does not mark files as having been backed up (in other words, archive attribute is not cleared). If you are performing a combination of normal and differential backups, restoring files and folders requires that you have the last normal and last differential backup.

    Incremental backup

    An incremental backup backs up only files that were created or changed since the last normal or incremental backup. It marks files as having been backed up (in other words, archive attribute is cleared). If you use a combination of normal and incremental backups, you will need to have the last normal backup set as well as all incremental backup sets to restore your data.

    http://TechNet.Microsoft.com/en-us/library/cc784306 (v = WS.10) .aspx

  • How to sort files in most recent records; last updated sorts by month (1, 2, etc.)

    Recent update has changed the sorting of files in my files - now sorts by # first date, i.e. the month, then all the files of January appear first (or December if I reverse).  They were all sorted by "most recent" as is my Outlook Inbox.  Can't find "most recent" or "recency" sort.  Help, please!  It is a mess and is complicating my job and wasting my time searching.

    I'm not at a computer vista right now, so my instructions is perhaps a little far, let me know if they are, and I'll fix them.

    In the case in question, on the top bar where you click to organize by name, size, etc. do a right-click, and I don't really know what's going to come up (perhaps a long list of options with check marks next to them) and look for an option 'creation date' or selection, which will add the creation date in the top bar , so you can click on it, and it will sort the file there, so created the most recent thing must be upward and the thing created a long time ago should be down.

  • How can I get the most recent email at the top of the list?

    My most recent email comes at the bottom of the list. I want it to appear at the top. How?

    Click on the heading of column titled Date.
    By clicking on any header indicates that the sort column. Clicking the same header again reverses the sort.

  • Format/erase how to read only the sd card.

    The card had already switch to unlock. When try to formatting so that send the message that this is a read-only. The map equipped with old data GPS which must erase and reuse the card.

    Rastafario

    So it is probably locked by Ford for GPS use only.

  • How can I solve this problem of 'Two versions of Adobe', then only the most recent version remains activated, the old version remains DISABLED, and/or uninstall the old version?

    I went to my addons tab, and I have, in fact, have two versions of shockwave flash, a 11.9... version and a 13.9, which, if I'm not wrong, is the latest version. There is a 'request to activate, activate always, never activate' button for EACH module. I clicked on never turn on the old version and the closed tab. I then tried to play a video on the Web. He would not play, I tried another site with the same result, so I went back to my tab add-on, and the button for the old version had changed again to "always enable" in itself. He continues to repeat this, and WILL NOT stay on "never activate. Whenever I return to the page complementary modules, THE NEW VERSION IS DISABLED, SO THAT THE OLD ONE. I tried to uninstall the old version, but when I go into Control Panel and make the contents of the file, the old version is not yet there. I did start, search and typed in "Adobe", and the old version is NOT FOUND! The two versions to change back the turn on button and they both will disable themselves, no matter how many times I fix it! SMH!

    No worries, I was just starting.

    The Flash plugin is stored in this folder:

    C:\Windows\SysWOW64\Macromed\Flash
    

    The filenames for the plugin in Firefox have this model:

    NPSWF32_13_0_0_206.dll
    

    where the middle part (not in bold) is the version number of the plugin.

    Update from Adobe should normally remove older versions, but if they are actively used, it is possible that the update is not able to remove them.

    If you find the file, try to delete or move the temporary office.

    Firefox may not immediately realize that the file has disappeared, but hopefully that will detect only after the next time you're out and start it back up when it updates its scan plugins.

    A little luck?

  • How to sort by the most recent with folders on top?

    Is there a way to sort files by date (first for the setback, current topics) but keep files on top of the directory structure? If so, how? I used to have this setting and it disappeared.

    To be clear, I'm looking for something like this

    [folder] abc
    [folder] def
    IGS [folder]
    [file] no matter - updated 11/01/11
    [file] file name is not serious - modified 10/01/11
    [file], you get the idea - modified on 04/01/11
    [file] and it was 2010 - amended 12/29/10

    Does make sense? Again, I had this before setting and he disappeared. What happened, and how do I get it back? A reboot it does not solve for me. I tried to right click "sort by" and do not see this option. ALL I want are the files on top... I don't like on the folder names.

    Thank you!

    The guys that the solution to this problem is on the first page, you must view the files in the folder in detail to see each way you can sort.

    Then you click Update, then shift + click on the name, it will fix the sorting in order to always put first records.

  • Satellite L675-10R - how to upgrade to the most recent display drivers?

    How to change the display drivers - AMD Radeon HD5650 to the newest.

    AMD is the new 11.11 on the site Web of Toshiba to download is 8.712.2.1. Some new games have performed better on the latest Radeon drivers

    Please don t forget you have laptop and not of office. There is big difference in support for mobile and desktop.

    Generally manufacturers doesn't support graphics card machines mobile and the responsibility is on the laptop manufacturers. Due to limited and specific design of cooling capacity that it is not possible to use the latest drivers you can find on the page of graphics card manufacturer s.

  • find the most recent record inserted by using rowid

    Yes, I suspect that it is a bad idea!

    I work with an application that has been modelled incorrectly. a historical table of the status had been modeled with a primary key on a key of the company and the entry into force instead of the date of the end of the recording of the State. There is one status as possible at the same time.

    logic has been developed to insert new records of status with the dates of null terminator and then a trigger was provided that populated end dates except the one with the MAX (ROWID). so the hope is that ROWID will always get bigger.

    now, the database has been moved in environments of promotion using data pump. in our development environments, it always seems to work. but not in promotional customers environments.

    My question is: is there a setting associated with data pump that the client has used that does not respect the ROWID as it has in our development environment?

    I'm trying to determine if there is a way to continue to use the old code without immediately re-development.

    Thanks in advance for your comments.

    so the hope is that ROWID will always get bigger.

    It is an expectation that has no guarantee of being correct. He has a very good chance that a new line will have a lower value of the ROWID.

    My question is: is there a setting associated with data pump that the client has used that does not respect the ROWID as it has in our development environment?

    There is not this setting. Data pump just inserts rows into a table. Because we cannot guarantee that the next row will have a higher ROWID, then what you are looking for there is no.

    It is time to rethink this misguided approach.

    See you soon,.
    Brian

  • How can I cancel the most recent update of the first cc in 2015

    I have problems playing audio with Premiere Pro CC 2015 (v9.1.0) I am also having problems of video rendering, where my film renders with the warning in offline media.

    I would use the version prior to this update.

    Hi Dvandergoes,

    Please refer to this document: How to find and install previous Version of Adobe Apps in CC 2015

    Thank you

    Regalo

  • BlackBerry smartphone how to navigate to the most recent message

    I know this is going to be a simple question. Every once in a while my email goes back to the oldest messgae. What is the command to move to the more current message.

    ON the models of the storm, you can "Browse" your finger down to scroll quickly to the top.

    Or press Menu > display keyboard > press the "t" (for the top).

    "B" to the bottom.

Maybe you are looking for

  • HP 110-210: mulberry pci slot

    Where is the PCI Express slot on the mulberry?

  • How to store a timer value

    Hi guys, im fairly new to all this labview, so I apologize if this is a silly question, im using an elapsed timer, I got a signal when it is greater than a constant, it resets the timer that gives a value of 0 for a defiler, when the signal is in the

  • (2003 server) error 2019: the server was unable to allocate from the system non-paged pool because the pool was empty

    Hello I have a Windows 2003 server and get error (Server 2003) 2019: the server was unable to allocate from the system non-paged pool because the pool was empty. If anyone has encountered this error and has an instruction step by step to solve this p

  • My printer does not play nice!

    Hi, I hope someone can help. I have an e-710n Officejet 6500 wireless and I have two questions, but don't think they are related. Check my ink levels and they are good. BUT if I print anything, like the wireless network settings NOTHING prints on the

  • Several VPN GET with Multicast clouds

    Hi all It is a recommended approach to use different multicast addresses if you use a key server to manage several groups GET VPN? It is not a provider environment hosted service but just for one customer in need of a logical separation. I think it w