I tried for some time now, read many posts but I still can't figure out how to handle this request:
How to create an output where the ID, date and all mutated data is shown (that is, including the ID, date and other data just before a change occurs, and the ID, date and other data of the change), whenever there's a change in one of the other columns.
For a one column change, this isn't too hard, using something like:
SELECT *
FROM YourTable
WHERE ARIDNR IN (
SELECT ARIDNR
FROM YourTable
GROUP BY ARIDNR
HAVING COUNT(*) > 1
)
But for changes in multiple columns...?
I have a table like this:
create table A (Reference_date date, Loan_id int, address NVARCHAR (100), house_nr int, status_loan int)
Insert into A values ('2020-01-31',1000,'Boulevard', 5, 10)
Insert into A values ('2020-02-28',1000,'Boulevard', 5, 10)
Insert into A values ('2020-03-31',1000,'Boulevard', 5, 10)
Insert into A values ('2020-04-30',1000,'Boulevard', 5, 10)
Insert into A values ('2020-05-31',1000,'Boulevard', 5, 10)
Insert into A values ('2020-06-30',1000,'Boulevard', 5, 10)
Insert into A values ('2020-07-31',1000,'Hill', 5, 10)
Insert into A values ('2020-08-31',1000,'Hill', 5, 10)
Insert into A values ('2020-09-30',1000,'Hill', 5, 10)
Insert into A values ('2020-10-31',1000,'Hill', 5, 10)
Insert into A values ('2020-01-31',1001,'Elm', 8, 10)
Insert into A values ('2020-02-28',1001,'Elm', 8, 10)
Insert into A values ('2020-03-31',1001,'View', 10, 10)
Insert into A values ('2020-04-30',1001,'View', 10, 10)
Insert into A values ('2020-05-31',1001,'View', 10, 10)
Insert into A values ('2020-06-30',1001,'View', 10, 10)
Insert into A values ('2020-07-31',1001,'Lake', 5, 10)
Insert into A values ('2020-08-31',1001,'Lake', 5, 10)
Insert into A values ('2020-09-30',1001,'Lake', 5, 10)
Insert into A values ('2020-10-31',1001,'Lake', 5, 10)
Insert into A values ('2020-01-31',1002,'Main', 1, 10)
Insert into A values ('2020-02-28',1002,'Main', 1, 10)
Insert into A values ('2020-03-31',1002,'Main', 1, 10)
Insert into A values ('2020-04-30',1002,'Main', 1, 5)
Insert into A values ('2020-05-31',1002,'Main', 1, 5)
Insert into A values ('2020-06-30',1002,'Main', 1, 6)
Insert into A values ('2020-07-31',1002,'Main', 5, 6)
Insert into A values ('2020-08-31',1002,'Main', 5, 6)
Insert into A values ('2020-09-30',1002,'Main', 5, 6)
Insert into A values ('2020-10-31',1002,'Main', 5, 6)
And I'm looking for a sub query in order to create the following outcome:
Reference_date, Loan_id, address, house_nr, status_loan
2020-06-30, 1000, Boulevard, 5, 10
2020-07-31, 1000, Hill, 5, 10
2020-02-28, 1001, Elm, 8, 10
2020-03-31, 1001, View, 10, 10
2020-06-30, 1001, View, 10, 10
2020-07-31, 1001, Lake, 5, 10
2020-03-31, 1002, Main, 1, 10
2020-04-30, 1002, Main, 1, 5
2020-05-31, 1002, Main, 1, 5
2020-06-30, 1002, Main, 1, 6
2020-06-30, 1002, Main, 1, 6
2020-07-31, 1002, Main, 5, 6
Does anyone know how to accomplish this?