Latest Address and the Previous Address in the last 30 days

Morning folks.
I need to find out all Customers whose Address changed based on the Effective Date. The Query should be based as of Effective Date 01-JUN-12 and not SYSDATE. So, we want to check the MAX(effective_date) is <= 01-JUN-12 and the Previous Address's Effective Date is between 02-MAY-12 and 01-JUN-12 (30 days). This would suggest that an Address has changed. For argument sake, the Zip Code is enough to differentiate that an address has changed for now.
Based on the data, I only want to pick up Customer 1002 but not 1001, 1003. As for Customer ID 1003, even though his/her address changed in the last 30 days, we are not interested as the Address effective 1st June was good for a long time.
Also, even though the Address for Customer ID 1001 changed twice in that period, I am only interested for the latest 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 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 a bunch.
Edited by: Roxyrollers on Jul 26, 2012 9:35 AM

Something like this
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-12Justin

Similar Messages

Maybe you are looking for

  • Oracle 10g Express Edition, Installation Problem

    After run # /etc/init.d/oracle-xe configure Oracle Database 10g Express Edition Configuration-------------------------------------------------This will configure on-boot properties of Oracle Database 10g ExpressEdition. The following questions will d

  • Payroll Payment via ALE/IDOC

    Hello Everyone, We currently run payroll and process ACH payment information via RFFOUS_T.  The information is sent directly to the bank.  We have a new request which will require HR Payroll payment information to be sent from our SAP system to anoth

  • How to save SM59 RFC entries during system refreshes

    Hello, I will be refreshing our NW04 ERP and SRM QA systems.   Our platform is Oracle/Unix. My question is how to export the SM59 RFC entries before the refresh, so I can reimport them after the refresh.   I have done this successfully in R/3 4.6c, b

  • Problem in Exception Handling...URGENT

    hi, actually i am trying to make a program using Lucene Api and Using NGramSpeller... the information about NGramSpeller is here http://www.marine-geo.org/services/oai/docs/javadoc/org/apache/lucene/spell/NGramSpeller.html#suggestUsingNGrams(org.apac

  • Html Email containing unicode characters [10 Duke Dollars]

    Want to be able to send a html email converted from a Word document, i.e. one that contains special characters. I was having a problem trying to send a specific combination of characters: String html = "<html><head></head><body>\n<p>\u0093hello\u0094