Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to compare with Audit Table - Microsoft SQL Server 2000 - 8.00.2055

$
0
0

Hi,

Here are two sample Tables.

PLEASE NOTE: THESE ARE JUST SAMPLE TABLES CREATED TO SIMPLIFY THE QUESTION. 
I DID NOT ADD ANY EXTRA THINGS, NO PRIMARY KEY OR ANYTHING. THESE ARE JUST TO WORK WITH PERFECT REPLICATING EXAMPLE.

My SQL Version is "Microsoft SQL Server  2000 - 8.00.2055", so cannot use Row_Number using CTE.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblAudit_tblEmployee](
	[EmployeeID]				[int]			NOT NULL,
	[DepartmentName]			[varchar](50)	NULL,
	[StampAction]				[varchar](50)	NULL,
	[StampActionDateTime]		[datetime]		NULL,
)
GO
GO
CREATE TABLE [dbo].[tblEmployee]
(
	[EmployeeID]				[int],
	[DepartmentName]			[varchar](50)	NULL
)
GO
SET ANSI_PADDING OFF
GO

TRUNCATE TABLE [tblAudit_tblEmployee];
TRUNCATE TABLE [tblEmployee];


INSERT INTO [tblEmployee] VALUES (1, 'Dept 1');
INSERT INTO [tblEmployee] VALUES (2, 'Dept 2');
INSERT INTO [tblEmployee] VALUES (3, 'Dept 3');
INSERT INTO [tblEmployee] VALUES (4, 'Dept 4');


INSERT INTO [tblAudit_tblEmployee] VALUES (1, 'Dept 3', 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (1, 'Dept 3', 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (2, 'Dept 2', 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (1, 'Dept 1', 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (3, 'Dept 3', 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (3, 'Dept 1', 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (3, 'Dept 2', 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (4, 'Dept 2', 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (4, 'Dept 4', 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (3, 'Dept 3', 'U',GETDATE());

Now, I am looking for two queries.
1. I Want to get data from [tblAudit_tblEmployee] where DepartmentName has been changed and where previous department name was "Dept 3"., 
In this case, I want to display this result, basically want to add that Previous Department Name as Column.
EmployeeID , Current Department Name, Previous Department Name, StampActionDateTime
I want to know anything changed from "Dept 3". So if anytime there was any record containing "Dept 3" and was changed, it should list here.
2. Same scenario as above with one change, I want to same list but where last departmentname was "Dept 3".
Means if a record has 3 entries in Audit table and if its current value is not "Dept 3" but its last to current value is "Dept 3", then it should return that record.
EmployeeID , Current Department Name, Previous Department Name, StampActionDateTime

Basically using Audit Table, I want to compare previous row for same Employee in same table, and want to see if that previous row has "Dept 3".

Thanks in advance, 


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>