I am trying to modify this report that I have. The report is supposed to identify changes to attributes of a set of data.
For example, a person and his address with respect to his lease renewals. So imagine that the person was at address-1 from 1-1-2018 through 3/12/2018, then moved to another address-2, initially signing lease for 3/12/2018 through 12/31/2021, but then changed the lease later to 3/12/2018 through 10/1/2018. He then went ahead and extended his lease from 10/2/2018 to 12/31/2020. In this case, i want to not show the initial lease signing period of 3/12/2018 through 12/31/2021. The current table stores all of these records and i don't want this particular row to be shown just so the end user doesn't get confused.
The way I look at it, if i am able to get all the "duplicates" in columns Person, Person_ID, Phone_Number, end_date and then for those duplicate rows only keep the highest start_date, i would be able to be report out the right way. I am, however, stuck in my ability to eliminate the min(start_date) from those rows while showing all of the data.
select
subset.person,
subset.person_id,
subset.phone_number,
subset.end_date,
count(*)
from subset
group by
subset.subset.person,
subset.person_id,
subset.phone_number,
subset.end_date
having count(*)>1
order by person_id;
Now for these rows, i need to select the max(start_date) and keep only those in the report (I think i am stuck there).
So the table essentially shows the following:
Person Person ID Phone Number Address 1 Zip Code Start Date End Date
Jane 1 8792029484 some address-1 12345 3/5/2016 11/9/2018
Jane 1 8792029484 some address-2 34455 11/10/2018 12/31/9999
Jane 1 8792029484 some address-2 34455 11/10/2018 12/7/2018
Jane 1 8792029484 some address-2 34455 12/8/2018 12/31/9999
John 2 9808845768 another address-1 68687 1/1/2013 11/9/2018
John 2 9808845768 another address-2 89384 11/10/2018 12/31/9999
John 2 9808845768 another address-2 89384 11/10/2018 12/7/2018
John 2 9808845768 another address-2 89384 12/8/2018 12/31/9999
For Jane and John, I don't want the following rows to be displayed:
Jane 1 8792029484 some address-2 34455 11/10/2018 12/31/9999
John 2 9808845768 another address-2 89384 11/10/2018 12/31/9999
Expected result:
Person Person ID Phone Number Address 1 Zip Code Start Date End Date
Jane 1 8792029484 some address-1 12345 3/5/2016 11/9/2018
Jane 1 8792029484 some address-2 34455 11/10/2018 12/7/2018
Jane 1 8792029484 some address-2 34455 12/8/2018 12/31/9999
John 2 9808845768 another address-1 68687 1/1/2013 11/9/2018
John 2 9808845768 another address-2 89384 11/10/2018 12/7/2018
John 2 9808845768 another address-2 89384 12/8/2018 12/31/9999