Skip to content
Advertisement

Displaying the difference between rows in the same table

I have a table named Employee_audit with following schema,

emp_audit_id eid name salary
1 1 Daniel 1000
2 1 Dani 1000
3 1 Danny 3000

My goal is to write a SQL query which will return in following format, considering the first row also as changed value from null.

columnName oldValue newValue
name null Daniel
salary null 1000
name Daniel Dani
name Dani Danny
salary 1000 3000

I have written the below SQL query,

But it gives the result in following format

oldname newname oldsalary newsalary
Daniel Dani null null
Dani Danny 1000 3000

Could you please answer me, how can I get the required result.

Advertisement

Answer

If you give each row a row number in a CTE then join on yourself to the next row you can compare the old and the new values. Unioning the 2 different column names is a bit clunky however, if you needed a more robust solution you might look at pivoting the data.

You also obviously have to convert all values to a common datatype e.g. a string.

Returns:

emp_audit_id columnName oldValue newValue
1 name NULL Daniel
1 salary NULL 1000.00
2 name Daniel Dani
3 name Dani Danny
3 salary 1000.00 3000.00

I highly encourage you to add DDL+DML (as show above) to all your future questions as it makes it much easier for people to assist.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement