Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
trusted4u
Posting Yak Master
109 Posts |
Posted - 2003-10-07 : 01:29:21
|
| Hi :V have a script which runs daily to insert all emp records from txt file into a table. Something went wrong and instead of having one record of each emp for a particular date the record got split into two. The Foll sample shows the splitting of EMIDNO 1 AND 3 for 2003-10-03 EMIDNO TRANDATE ATTEND REV1 2003-10-01 PRESENT 40001 2003-10-02 PRESENT 42001 2003-10-03 PRESENT NULL1 2003-10-03 NULL 58002 2003-10-01 PRESENT 40002 2003-10-02 PRESENT 41502 2003-10-03 PRESENT 40603 2003-10-01 PRESENT 41003 2003-10-02 PRESENT 12003 2003-10-03 PRESENT NULL3 2003-10-03 NULL 11004 2003-10-01 PRESENT 60004 2003-10-02 PRESENT 61504 2003-10-03 PRESENT 7840I read the article written by Graz about "Deleting Duplicate Records", but in my case the records are not exactly duplicated. So I want to merge the splitted records into one record. Something like this :EMIDNO TRANDATE ATTEND REV1 2003-10-01 PRESENT 40001 2003-10-02 PRESENT 42001 2003-10-03 PRESENT 58002 2003-10-01 PRESENT 40002 2003-10-02 PRESENT 41502 2003-10-03 PRESENT 40603 2003-10-01 PRESENT 41003 2003-10-02 PRESENT 12003 2003-10-03 PRESENT 11004 2003-10-01 PRESENT 60004 2003-10-02 PRESENT 61504 2003-10-03 PRESENT 7840Any help will be really appreciated.Thanks,- Marjo. |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-10-07 : 01:52:59
|
Take a backup of the table first and do this:--Get all the merged records into a temporary tableSELECT EMIDNO, TRANDATE, MAX(ATTEND) AS ATTEND, MAX(REV) AS REVINTO #MergedRecordsFROM TableNameGROUP BY EMIDNO, TRANDATE-Clear existing dataDELETE FROM TableName--Insert clean data back into tableINSERT INTO TableName SELECT * FROM #MergedRecordsYou might have to tweak it a little to suit your requirements, if you need more help, let us know.Owais Make it idiot proof and someone will make a better idiot |
 |
|
|
trusted4u
Posting Yak Master
109 Posts |
Posted - 2003-10-07 : 06:16:25
|
| Thanks A LOT mohdowais, it really worked.But there is one slight problem that I 4got mention in my prev post. Some employee records CAN be repeated but with different values in ATTEND field. For Eg :EMIDNO TRANDATE ATTEND REV2 2003-10-01 PRESENT 40002 2003-10-02 PRESENT 41502 2003-10-03 PRESENT 40602 2003-10-03 WORKED 2SHIFT 4060So it merges these records also and returnsEMIDNO TRANDATE ATTEND REV2 2003-10-01 PRESENT 40002 2003-10-02 PRESENT 41502 2003-10-03 WORKED 2SHIFT 4060and I can't filter on date bcoz it could be any date.Thanks- Marjo. |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-10-07 : 06:34:55
|
| SELECT distinctEMIDNO, TRANDATE, ATTEND, REVINTO #MergedRecordsFROM TableNameWHERE NOT (ATTEND is null OR REV is null) |
 |
|
|
|
|
|
|
|