Get the closest previous date you

First, example table and data:
drop table CUSTOMER_INFO_TEST;

CREATE TABLE CUSTOMER_INFO_TEST
(
ACCOUNT_NUM VARCHAR2 (40 BYTE),
PHONE VARCHAR2 (100 BYTE),
E-MAIL VARCHAR2 (300 BYTE),
DATE OF START_DT,
DATE OF CHANGE_DT,
END_DT DATE
);



INSERT INTO CUSTOMER_INFO_TEST VALUES ('BOB', 555-1234', ", TO_DATE ('2011-01-01', 'YYYY-MM-DD'), TO_DATE ('2011-01-05', 'YYYY-MM-DD'), TO_DATE ('2011-01-10', 'YYYY-MM-DD'));
INSERT INTO CUSTOMER_INFO_TEST VALUES ('BOB', 555-1234', ", TO_DATE ('2011-01-01', 'YYYY-MM-DD'), TO_DATE ('2011-01-11', 'YYYY-MM-DD'), NULL);
INSERT INTO CUSTOMER_INFO_TEST VALUES ('BOB', 555-1234', ", TO_DATE ('2011-01-01', 'YYYY-MM-DD'), TO_DATE ('2011-01-15', 'YYYY-MM-DD'), NULL);
INSERT INTO CUSTOMER_INFO_TEST VALUES ('JACK', 555-4321', ", TO_DATE ('2011-01-01', 'YYYY-MM-DD'), TO_DATE ('2011-01-05', 'YYYY-MM-DD'), NULL);
INSERT INTO CUSTOMER_INFO_TEST VALUES ('JACK', 555-4321', ", TO_DATE ('2011-01-01', 'YYYY-MM-DD'), TO_DATE ('2011-01-09', 'YYYY-MM-DD'), NULL);

commit;

I want to query this table with a date range, change_dt, always to give me the lines in the range AND the previous row.
select * from CUSTOMER_INFO_TEST where change_dt >= TO_DATE('2011-01-05', 'YYYY-MM-DD') and change_dt <= TO_DATE('2011-01-10', 'YYYY-MM-DD')

BOB     555-1234          2011-01-01     2011-01-05     2011-01-10
JACK     555-4321          2011-01-01     2011-01-05     
JACK     555-4321          2011-01-01     2011-01-09     
The foregoing will give me the correct result, a BOB and two jacks, since also while JACK and BOB changes the 5 January.

But I want the same result, when you select it with 6 Jan as start date,


This is how I can make it explicit for BOB
select * from CUSTOMER_INFO_TEST where 
change_dt >= (select max(change_dt ) from CUSTOMER_INFO_TEST where change_dt <= to_date('2011-01-06','YYYY-MM-DD') and ACCOUNT_NUM = 'BOB')
and change_dt <= to_date('2011-01-06','YYYY-MM-DD')
and ACCOUNT_NUM = 'BOB'

BOB     555-1234          2011-01-01     2011-01-05     2011-01-10
Is this possible without a sub request, and in a statement across the table without specifying the actual table data, as I did with "BOB" in the above statement?

Edited by: Rydman January 14, 2013 14:39

Hello

I think I understand now: you have 2 parameters, range_start_dt and range_end_dt, and you want to find the rows where the change_dt is between these parameters, PLUS you want the last row for each account_number coming before the interval given, if the same customer has lines in the range.

Here's a way to do it:

VARIABLE  range_start_dt     VARCHAR2 (10)
VARIABLE  range_end_dt          VARCHAR2 (10)
EXEC      :range_start_dt := '2011-01-06';
EXEC      :range_end_dt   := '2011-01-10';

WITH     got_r_num     AS
(
     SELECT     account_num, phone, email, start_dt, change_dt, end_dt
     ,     RANK ()       OVER ( PARTITION BY  account_num
                               ,                    SIGN (start_dt - TO_DATE ( :range_start_dt
                                                             , 'YYYY-MM-DD'
                                                 )
                                   )
                         ORDER BY        start_dt   DESC
                       )         AS r_num
     FROM     customer_info_test
     WHERE     change_dt     < TO_DATE ( :range_end_dt
                             , 'YYYY-MM-DD'
                           ) + 1   -- See note below
)
SELECT       account_num, phone, email, start_dt, change_dt, end_dt
FROM       got_r_num
WHERE       change_dt     >= TO_DATE ( :range_start_dt
                            , 'YYYY-MM-DD'
                       )
OR       r_num          = 1
;

Range_end_dt indicates just the calendar day of the end of the range; in other words: range_end_dt = ' 2011-01-10' means anything at any time, January 10, 2011, until 23:59:59, must be included. Instead of saying you want everything up to and including 23:59:59 on January 10, I find it easier just to say you want everything up to but not including the day after January 10.

If all change_dts for a given account_num after range_end_dt, then no output for this account_num will be generated.

Tags: Database

Similar Questions

  • ETA App should help to get the time and date feature HELP!

    Hi everyone, I'm doing an application where I need to get the time and date of the antone device can help, I know there is a function such as getDate or getTime but im not to know how to use it.

    As far as I know,

    I use this to get the time system (it returns long type)

    System.currentTimeMillis();
    

    After that, you can use SimpleDateFormat to convert date and time

    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
    String time = sdf.format(new Date(System.currentTimeMilis()));
    
  • How can I get the list of data stores in a cluster data store?

    How can I get the list of data stores in a cluster data store? I mean the command line option.

    Hello

    by command line, you mean PowerCLI?

    If so, you can display data with this warehouses:

    Get-DatastoreCluster-name DSClustername | Get-Datastore

    Tim

    Edit: Moved the thread to the PowerCLI community

  • I get the message "serial number you provided is valid, but calling it a product was not found on this computer" when I try and install Acrobat Standard XI

    I have a PC with windows 7 installed.  Reader Acrobat X and XI of Acrobat Pro trial version will work perfectly.  I bought Acrobat Standard XI, and there is a serial number in my account.  Acrobat Standard XI installs fine, but I get the error message after you put my serial number in, 'serial number you provided is valid, but calling it a product was not found on this computer

    The message indicates that you have purchased a version upgrade of applications and it asks you to identify the previous version of this application you are upgrading, and then provide serial number.

    If you had no intention to buy a version upgrade then you will probably cancel that and buy it at the price high.

  • How can I get the time and date appears on a sony cyber-shot camera

    How can I get the time and date appears on a sony cyber-shot camera

    Hi terpmarty,

    What is the model number of the Sony Cybershot camera?
    Instructions for superimposing the date and time are model specific. Some models do not have the ability to superimpose the date and time too. In such a case, you can use the supplied software Picture Motion Browser (PMB) or PlayMemories home (WPH) (Compatible with the Windows operating system) to print the date and time on pictures.

  • When the research report I get the message "no data found".

    When I run a search on one of the fields of research, I get the message "no data found". Anyone can help me to understand what it is that I don't hurt or not done? Thank you

    Deanna

    http://Apex.Oracle.com/pls/OTN/f?p=4550:1:4031563210204584

    Workspace: DEANNA2
    Username: DDOVE@KHP. KS.GOV
    Password: Dclipse03

    Page 21

    No, I added a calculation to the page to see what is the sql code that is returned by the function... Now, I could add in another column and see what happens...

    That's why I suggested moving it to a function outside the page... You can test this in the sql command window and the prefect what you are after... Have done reports a little like this... Or even more fun, with a function in the pipeline...

    Thank you

    Tony Miller
    Webster, TX

  • Exception occurred in the Microsoft Access database engine: the field is too small to accept the amount of data you attempted to add.

    Hello

    I try to save a path in a table in an access database, but an error occurs:

    "Exception occurred in the Microsoft Access database engine: the field is too small to accept the amount of data you attempted to add." Try insert or paste less data. "in create a NI_Database_API.lvlib:Rec - Command.vi-> NI_Database_API.lvlib:Cmd Execute.vi-> NI_Database_API.lvlibData.vi B tools insert-> project total.vi.

    I've attached a JPEG of a part of the code and the code, but it won't work because the database is not attached,

    any help please?

    Ok

    I solve the resized problem.i column size

    Thank you

  • Get the blue screen when you create a new message or the response message in OWA Exchange 2007

    Get the blue screen when you create a new message or the response message in OWA (Exchange 2007)

    Hello

    I suggest you post the question in the forums and check them off below if it helps:

    http://social.technet.Microsoft.com/forums/en-us/exchangesvrclients/

    It will be useful.

  • Get the Error Message When you try to download and install Vista Service Pack 1

    Hello

    Update Windows guard neglect when you try to download and install Vista Service Pack 1 and I get the error message 80092004.  Please notify.

    Thank you
    Andy

    Hello

    Welcome to the windows vista forums.

    1. stop the Windows Update service (make sure that you are logged on as administrator)

    Click the Start button | Control Panel | System and Maintenance | Administration tools
    Double-click Services.
    (Provide the administrator password or a confirmation if prompted to do so.)
    Right-click the Windows Update service and click on stop.

    2. Click Start, type %windir%\System32 in the search box and press ENTER.

    Find and rename the folder C:\Windows\SoftwareDistribution.

    3. start the Windows Update service

    Start button | Control Panel | System and Maintenance | Administration tools
    Double-click Services.
    (Provide the administrator password or a confirmation if prompted to do so.)
    Right-click the Windows Update service and click Start.

    This procedure will erase your history on the Windows Update Site. Not important.

    Don't forget to restart and then try the update again. If there is no corruption in that
    folder, it would be gone now.

     

    Please, try now to windows update and check if you get the error.

    You can also download the stand-alone SP1 package. You can download the windows vista SP1 by clicking on the link below, please follow the instructions in method 3 in the KB article:

    http://support.Microsoft.com/kb/935791#Method3

    I hope this information is useful.

    Please let us know if you need further assistance.

    Concerning
    Rehman - Microsoft Support

  • Problem has been detected and windows has been shutdown error STOP: 0x0000008E. Get the memory dump after you have installed the trial version of MS Project 2010.__space.

    Original title: problem has been detected and windows has been shut down. Get the memory dump after you have installed the trial version of MS Project 2010.__space.

    Computer Message: Check if you have enough disk space. If a driver is identified in the stop message, disable the driver or check with the manufacturer for driver updates. Try changing video cards.

    Check with your suppliers of material, updates the BIOS. Disable the BIOS memory options such as implementing caching or shading. If you need to use Safe Mode to remove or disable components, restart your computer, press F8 to start advance startup options, and then select Safe mode.

    Technical information: * STOP: 0x0000008E (0xC0000005, 0x8054556C, 0xADB78A98, 0x00000000)

    Beginning physical memory dump

    Total physical memory dump. Contact technical support for further assistance group.

    Hi fernandomventura,

    Step 1: Disconnect all external devices (printers, scanner, USB (universal serial bus) readers, etc...) Except the keyboard and mouse and then start.

    If this solves the problem, then add back devices at a time until you discover the piece of hardware causing the issue.  Then get any drivers\software update for the device.

    Step 2: Follow the steps mentioned in the articles below which deals with a similar question

     

    General troubleshooting for an error message that you receive randomly in Windows XP: "Stop 0x0000008E"-restore the system to achieve ".

    http://support.Microsoft.com/kb/945658/

    You receive a random "0x0000008E" error message on a blue screen in Windows XP

    http://support.Microsoft.com/kb/827663

    Thank you, and in what concerns:

    Ajay K

    Microsoft Answers Support Engineer

    Visit our Microsoft answers feedback Forum and let us know what you think.

  • get the fatal error when you try to start Windows 7 Easy Transfer...

    get the fatal error when you try to start Windows 7 Easy Transfer... what should I do?

    Hello Jo,.

    Thanks for posting your question on the Microsoft community.

    We need more information about the issue so that we can help you better.

    What is the full error message or code when you say, ""getting fatal error when you try to start Windows 7 Easy Transfer? '"

    Thank you for details on the issue.

    This problem may occur because of damaged or missing Windows files.

    I suggest you run the system file checker (SFC) and see if it helps.
    Reference:
    Use the System File Checker tool to repair missing or corrupted system files
    https://support.Microsoft.com/en-us/KB/929833

    System File Checker is a Windows utility that allows users to find corruptions in Windows system files and restore the damaged files.

    I hope this information helps.

    Please let us know if you need more help.

    Thank you

  • How can I get the game controller that you want to correlate to the keyboard commands?

    How can I get the game controller that you want to correlate to the keyboard commands?

    You may need to use the profiling software that comes with any game controller you are using.

    Some software controller has a library of game titles with default button/key configurations that

    You can change in the software according to your desires.

    -The software will need to navigate you to the games executable ( .exe ) from in the profiling software, which allows for the profile that you create in order to be recognized when you load the game in question.

    You can create orders, or simply assign a key combination, of a particular button on the controller

    and save the profile.

    -If the controller does not have a profiling software available for download, you may be stuck

    with some default orders can work with this controller and as a monopoly$ insinuated oft

    the controller XBOX games PC as a default configuration, a few years ago, it could mean

    your controller can be very useful if it is not programmed to emulate the XBOX controller.

    -If your controller is programmed to emulate the XBOX controller (like many recent controllers)

    It should be just a matter of changing the configuration in game controller options.

  • I transferred X 1 pro overall to another PC, but opening I get the message "serial number you provided is valid but calling it a product was not found on this computer", the drop-down list of products responding to the does not contain the ProX1 op

    I transferred X 1 pro overall to another PC, but opening I get the message "serial number you provided is valid but calling it a product was not found on this computer", the drop-down list of products responding to the does not contain the option ProX1.

    Hi Carl,

    It seems that you have installed Acrobat XI pro as an upgrade and an upgrade you will need a serial number for a product older Acrobat X, etc. 9:

    See, error: "this serial number is not for a product calling it" | Adobe Creative Suite

    Also, I checked with your Adobe ID on the forums and I can only locate XI of Acrobat pro upgrade recorded, please check if you have registered an earlier version of Acrobat on some another email to get this serial number.

    Let us know if that helps.

    Kind regards

    Sandrine Bani

  • How can I get the download instructions if you buy a new computer and edit programs for the new device

    How can I get the download instructions if you buy a new computer and edit programs for the new device

    Download the CS6 products

    Mylenium

  • Get the error message when you try to launch the 64-bit CC bridge.

    Get the error message when you try to launch Bridge from Photoshop CC 64 bit.  "Cannot complete order because the extension cannot be loaded."  Need of difficulty.  Thank you.

    Hey Adobe Bubba,

    Please uninstall the software, run the cleanup tool, reboot your machine and install again.

    Cleaning tool: http://www.adobe.com/support/contact/cscleanertool.html .

    Kind regards

    Romt Sinha

Maybe you are looking for

  • HP ENVY 700-349 Desktop PC: can I put this desktop computer graphics card at a 960 GTX?

    I decide to upgrade my videocard to a GTX 960 a GTX 645. My question is if I can spend in the first place (I'm not the most ideal for this kind of situations). The exact specifications of my computer is listed here: http://support.hp.com/us-en/docume

  • Question for Satellite A200 XP installation

    I followed the instructions using nLite to add drivers for chipset XP installation CD. Mentioned DennisFarina burn the ISO image that was created using nLite to disk. I now have a CD with an ISO image on. What should I do with this ISO image because

  • OKI 810 works do not with el capitan

    I upgraded to El Capitan, and then my printer Oki C810 CDN has stopped working. When printing I get the answer: filter Stopped-' failed in the dialog box I tried: 1. download and install the file update OKI on their web site (Hiper - c) 2 remove the

  • Error loading C:\windows\iambcex.dll error codes

    Original title: error codesOn my pc, I have not been able to obtain the nywhere.  When I open windows I get two error messages:Error loading C:\windows\iambcex.dllError loading C:\windows\iyigexindoduse.dll

  • I can't download service pack 3 (SP3), error 0 x 80070424

    I have Windows XP Home Edition (32 bit), I need to service pack 3, so I tried to use the Windows Update tool, but when I search the new updates, I get this error: [error number: 0 x 80070424] Please help me I need SP3 so I can download visual c++ 201