Hi All,
I have a table with 6 columns, below are the table structures...
Create statement:
CREATE TABLE [dbo].[TestTable](
[ID] [int] NOT NULL,
[NameID] [int] NOT NULL,
[FirstName] [varchar](20) NULL,
[MiddleName] [varchar](20) NULL,
[LastName] [varchar](20) NULL,
[DateField] [datetime] not null,
CONSTRAINT [PK_TestTable] PRIMARY KEY NONCLUSTERED
(
[ID] ASC
))
Insert Statement:
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (1,1001,'A','B','C','01/01/2001')
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (2 ,1002 ,'A','B','C','01/01/2001')
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (3 ,1003 ,'A','B','C','02/23/2003')
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (4 ,1010 ,'D','E','F','03/15/2004')
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (5 ,1022 ,null,'G','H','06/21/2006')
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (6 ,1025 ,null,'G','H','06/21/2006')
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (7 ,1028 ,null,'G','H','08/24/2010')
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (8 ,1035 ,'I','J','K','12/25/2007')
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (9 ,1035 ,'I','J','K','12/25/2007')
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (10,1040 ,'I','J','K','12/30/2007')
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (11,1045 ,'L',null,'M','06/22/2006')
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (12,1048 ,'L',null,'M','06/22/2006')
Insert into TestTable (ID,NameID,FirstName,MiddleName,LastName,DateField) Values (13,1050 ,'L',null,'M','08/24/2012')
My source data looks like this..
ID NameID FirstName MiddleName LastName DateField
1 1001 A B C 01/01/2001
2 1002 A B C 01/01/2001
3 1003 A B C 02/23/2003
4 1010 D E F 03/15/2004
5 1022 null G H 06/21/2006
6 1025 null G H 06/21/2006
7 1028 null G H 08/24/2010
8 1035 I J
K 12/25/2007
9 1035 I J K 12/25/2007
10 1040 I J K 12/30/2007
11 1045 L null M 06/22/2006
12 1048 L null M 06/22/2006
13 1050 L null M 08/24/2012
My Output should look like below...
Output data:
ID NameID FirstName MiddleName LastName DateField
1 1001 A B C 01/01/2001 --> (display same, no change)
2 1001 A B C 01/01/2001 -->(In Id=2, NameId should change to 1001, since FirstName,MiddleName,LastName and DateField is same for Id=1,2)
3 1003 A B C 02/23/2003 -->(No change in Id=3, becoz DateField is different eventhough FirstName,MiddleName,LastName is same as Id=1,2)
4 1010 D E F 03/15/2004 --> (display same, no change)
5 1022 null G H 06/21/2006
6 1022 null G H 06/21/2006 -->(In Id=6, NameId should change to 1022, since FirstName,MiddleName,LastName and DateField is same for Id=5,6)
7 1028 null G H 08/24/2010 -->(No change in Id=7, becoz DateField is different eventhough FirstName,MiddleName,LastName is same as Id=5,6)
8 1035 I J K 12/25/2007 --> (display same, no change)
9 1035 I J K 12/25/2007 --> (display same, no change)
10 1040 I J K 12/30/2007 --> (display same, no change)
11 1045 L null M 06/22/2006
12 1045 L null
M 06/22/2006 -->(In Id=12, NameId should change to 1045, since FirstName,MiddleName,LastName and DateField is same for Id=11,12)
13 1050 L null M 08/24/2012 -->(No change in Id=13, becoz DateField is different eventhough FirstName,MiddleName,LastName is same as Id=11,12)
I need a T-SQL code to get above output. I am using SQL Server 2008.
Thanks in advance,
RH
sql