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

Get Two Highest Records for specific IDs and convert both to columns

$
0
0

Hi,
Here are my sample tables.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblAudit_tblEmployee](
	[AuditID]					[int]			IDENTITY(1,1) NOT NULL,
	[EmployeeID]				[int]			NOT NULL,
	[FirstName]					[varchar](50)	NULL,
	[LastName]					[varchar](50)	NULL,
	[ActionCodeID]				[int]			NULL,
	[ActionDate]				[datetime]		NULL,
	[DepartmentID]				[int]			NULL,
	[StampAction]				[varchar](50)	NULL,
	[StampActionDateTime]		[datetime]		NULL,
 CONSTRAINT		[PK_tblAudit_tblEmployee] PRIMARY KEY CLUSTERED 
(
	[AuditID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblActionCodeMaster](
	[ID]						[int]			IDENTITY(1,1) NOT NULL,
	[ActionCode]				[varchar](50)	NULL,
	[ActionCodeDescription]		[varchar](50)	NULL,
 CONSTRAINT		[PK_tblActionCodeMaster] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblDepartmentMaster](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Dept_DistrictName] [varchar](250) NULL,
 CONSTRAINT [PK_tblDept_DistrictMaster] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO


TRUNCATE TABLE tblActionCodeMaster;
TRUNCATE TABLE [tblAudit_tblEmployee];
TRUNCATE TABLE [tblDepartmentMaster];

INSERT INTO tblActionCodeMaster VALUES ( 1, 'Test 1');
INSERT INTO tblActionCodeMaster VALUES ( 2, 'Promoted');
INSERT INTO tblActionCodeMaster VALUES ( 3, 'Test 3');
INSERT INTO tblActionCodeMaster VALUES ( 4, 'Test 4');

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


INSERT INTO [tblAudit_tblEmployee] VALUES (10, 'First Name 10', 'Last Name 10', 2, GETDATE(), 1, 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (20, 'First Name 20', 'Last Name 20', 3, GETDATE(), 1, 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (10, 'First Name 10', 'Last Name 10', 1, GETDATE(), 2, 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (10, 'First Name 10', 'Last Name 10', 2, GETDATE(), 3, 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (10, 'First Name 10', 'Last Name 10', 2, GETDATE(), 1, 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (20, 'First Name 20', 'Last Name 20', 2, GETDATE(), 4, 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (20, 'First Name 20', 'Last Name 20', 3, GETDATE(), 3, 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (20, 'First Name 20', 'Last Name 20', 4, GETDATE(), 1, 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (10, 'First Name 10', 'Last Name 10', 1, GETDATE(), 1, 'U',GETDATE());
INSERT INTO [tblAudit_tblEmployee] VALUES (10, 'First Name 10', 'Last Name 10', 2, GETDATE(), 1, 'U',GETDATE());

What I am looking for is to get records from [tblAudit_tblEmployee] where ActionCodeID = 2 and previous audit record for that employee.
I need to know only for the 'promoted' record and last audit record for the same employee, and I need to display result as 'Promition In' and 'Promotion Out'.
Means that I want to show if employee has got promotion, display his promotion and his last record values before promotion to know his previous department as well.
I just need the Current Audit Row where ActionCodeID = 2 and last Audit row for that employee, MEANS PREVIOUS AUDIT ROW FOR THAT EMPLOYEE where ActionCodeID = 2,
So that I can display his promotion and his previous department. AND I NEED TO DISPLAY BOTH THESE ROWS AS ONE SINGLE ROW BUT SEPARATE COLUMNS.

Here is the result set I am looking for.

Employee ID | First Name | Last Name | NewActionCodeID | NewActionCodeDate | NewAuditID | NewDepartmentID | NewDepartmentName | PreviousActionCodeID | PreviousActionCodeDate | PreviousAuditID | PreviousDepartmentID | PreviousDepartmentName

So I am not sure if I can achieve this in a single query, but If we can achieve this in a single query, that would be best.
If not, can you tell me two separate queries, first to get row where ActionCodeID = 2 and second query to get previous audit records for all ActionCodeID = 2.
I need to develop a report so I can add sub report for second query and can link both with EmployeeID.

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>