i need tenurestart date and tenureenddate to be updated.
i have 3 agents ab0186, cc676h, rd885e. Agentid is unique in the table. when agent changes his director, his tenure start date should not change, if the agent changes his center then the tenure should change and should be the same as profilestartdate and his previous profile tenureendddate should all be updated . if both change then the priority goes to center and his tenure date should be changed. please see the result below.
results required as below (tenuredates)
i am giving you the create and insert script, please help me out with the code.
CREATE TABLE [dbo].[test]([agentid] [int] IDENTITY(1,1) NOT NULL,
[attuid] [nvarchar](10) NULL,
[Center] [nvarchar](50) NULL,
[Director_ATTUID] [nvarchar](10) NULL,
[ProfileStartDate] [datetime] NULL,
[ProfileEndDate] [datetime] NULL,
[TenurStartdate] [datetime] NULL,
[TenureEnddate] [datetime] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[test] ON
INSERT [dbo].[test] ([agentid], [attuid], [Center], [Director_ATTUID], [ProfileStartDate], [ProfileEndDate], [TenurStartdate], [TenureEnddate]) VALUES (457630, N'ab0186', N'Tempe - 31+', N'mr1439', CAST(0x0000A1EC00000000 AS DateTime), CAST(0x0000A20C00000000 AS DateTime),'','')
INSERT [dbo].[test] ([agentid], [attuid], [Center], [Director_ATTUID], [ProfileStartDate], [ProfileEndDate], [TenurStartdate], [TenureEnddate]) VALUES (458647, N'ab0186', N'Canto - 31+', N'mr1439', CAST(0x0000A20D00000000 AS DateTime), CAST(0x0000A2C400000000 AS DateTime),'','')
INSERT [dbo].[test] ([agentid], [attuid], [Center], [Director_ATTUID], [ProfileStartDate], [ProfileEndDate], [TenurStartdate], [TenureEnddate]) VALUES (473595, N'ab0186', N'Tempe - 31+', N'rk2675', CAST(0x0000A2C500000000 AS DateTime), CAST(0x0000A2FF00000000 AS DateTime),'','')
INSERT [dbo].[test] ([agentid], [attuid], [Center], [Director_ATTUID], [ProfileStartDate], [ProfileEndDate], [TenurStartdate], [TenureEnddate]) VALUES (482175, N'ab0186', N'Damo - 31+', N'rk2675', CAST(0x0000A30000000000 AS DateTime), CAST(0x0000ACA200000000 AS DateTime),'','')
INSERT [dbo].[test] ([agentid], [attuid], [Center], [Director_ATTUID], [ProfileStartDate], [ProfileEndDate], [TenurStartdate], [TenureEnddate]) VALUES (418165, N'cc676h', N'Manila', N'mr1439', CAST(0x0000A04B00000000 AS DateTime), CAST(0x0000A0F200000000 AS DateTime),'','')
INSERT [dbo].[test] ([agentid], [attuid], [Center], [Director_ATTUID], [ProfileStartDate], [ProfileEndDate], [TenurStartdate], [TenureEnddate]) VALUES (470935, N'cc676h', N'Alphaland - 31+', N'mr1439', CAST(0x0000A2B200000000 AS DateTime), CAST(0x0000A2C400000000 AS DateTime),'','')
INSERT [dbo].[test] ([agentid], [attuid], [Center], [Director_ATTUID], [ProfileStartDate], [ProfileEndDate], [TenurStartdate], [TenureEnddate]) VALUES (474047, N'cc676h', N'Alphaland - 31+', N'rk2675', CAST(0x0000A2C500000000 AS DateTime), CAST(0x0000ACA200000000 AS DateTime),'','')
INSERT [dbo].[test] ([agentid], [attuid], [Center], [Director_ATTUID], [ProfileStartDate], [ProfileEndDate], [TenurStartdate], [TenureEnddate]) VALUES (443034, N'rd885e', N'Makati - 31+', N'nd1275', CAST(0x0000A1E90085A270 AS DateTime), CAST(0x0000A1ED00000000 AS DateTime),'','')
INSERT [dbo].[test] ([agentid], [attuid], [Center], [Director_ATTUID], [ProfileStartDate], [ProfileEndDate], [TenurStartdate], [TenureEnddate]) VALUES (452569, N'rd885e', N'Makati - 31+', N'mr1439', CAST(0x0000A1EE00000000 AS DateTime), CAST(0x0000A2C400000000 AS DateTime),'','')
INSERT [dbo].[test] ([agentid], [attuid], [Center], [Director_ATTUID], [ProfileStartDate], [ProfileEndDate], [TenurStartdate], [TenureEnddate]) VALUES (477450, N'rd885e', N'Davao - 31+', N'rk2675', CAST(0x0000A2C500000000 AS DateTime), CAST(0x0000ACA200000000 AS DateTime),'','')
SET IDENTITY_INSERT [dbo].[test] OFF