stapleman527
Programmer
I have a project I am working on where I have a table full of client data that some clients have multiple records in, updates and such. I need to filter the data down to one record per client.
The way I need to do this is to take each group of records for a client and order them by their creation date and then starting with the first one populate all the blocks of data that I can. By blocks of data I mean for example the Name block consists of First_Name, Last_Name, Middle_Name, Name_Suffix, Name_Prefix. If the first row doesn't have this information then I want to take it from the next row, if it doesn't have it then the next, etc.If I get though all of the rows and none of them have it then I will just leave it blank.
I am new to sql server but have used Oracle in the past procedurally like this, although not very much. I figured I could just group by the ssn and count each client. If there is only one then I would just move the entire row to the filtered table, if there were multiple then I would have to read them into a cursor and go through each block of data and to find the first one that has it. Below is my pseudo code for what I was thinking. But my experience is in languages like java so I wanted to know what the best way to do this would be.
Pseudo Code:
Select SSNs into a cursor with a count
For Each Row{
if Count = 1
insert the row into filtered table
else
read all records for that ssn into another cursor
For Each Block{
For Each Row{
Read in all pieces of data for block
if data isnt empty
break
}
//variables will be left with data from the first record that has it.
}
Insert data from all blocks into filtered table
}
This seems very inefficient because you would have to read through the second cursor once for each block of data. But I don't know a better way to do it in SQL, or really if that is possible in SQL.
I am using SQL Server 2005 if that makes a difference.
Thanks,
Stapleman527
The way I need to do this is to take each group of records for a client and order them by their creation date and then starting with the first one populate all the blocks of data that I can. By blocks of data I mean for example the Name block consists of First_Name, Last_Name, Middle_Name, Name_Suffix, Name_Prefix. If the first row doesn't have this information then I want to take it from the next row, if it doesn't have it then the next, etc.If I get though all of the rows and none of them have it then I will just leave it blank.
I am new to sql server but have used Oracle in the past procedurally like this, although not very much. I figured I could just group by the ssn and count each client. If there is only one then I would just move the entire row to the filtered table, if there were multiple then I would have to read them into a cursor and go through each block of data and to find the first one that has it. Below is my pseudo code for what I was thinking. But my experience is in languages like java so I wanted to know what the best way to do this would be.
Pseudo Code:
Select SSNs into a cursor with a count
For Each Row{
if Count = 1
insert the row into filtered table
else
read all records for that ssn into another cursor
For Each Block{
For Each Row{
Read in all pieces of data for block
if data isnt empty
break
}
//variables will be left with data from the first record that has it.
}
Insert data from all blocks into filtered table
}
This seems very inefficient because you would have to read through the second cursor once for each block of data. But I don't know a better way to do it in SQL, or really if that is possible in SQL.
I am using SQL Server 2005 if that makes a difference.
Thanks,
Stapleman527