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 |
|
goligol
Posting Yak Master
128 Posts |
Posted - 2011-05-20 : 14:00:57
|
| HiIn the following table knew that there are some duplicated records and also some records that their kes sets (ODCM) are the same but the value of the V0 is slightly different in each record.I have found both these two groups of records through the following command:Command 1: SELECT O,D,C,M,V0 FROM myTable GROUP BY O,D,C,M,V0 HAVING COUNT(*)>1Comand2: SELECT O,D,C,M FROM myTable GROUP BY O,D,C,M HAVING COUNT(DISTINCT V0)>1Now I want to remove one of the duplicated records and also from the records with same key set but different V0 value, only keep one of the record and eliminate the other one. Question:Remove the duplicated records from command 1,Keep one of the records of command 2 and remove the other one. (there are records with same key set (ODMC) but different V0, delete all except one)Thank youO D C M V004000US01 010 2 02 404000US01 010 3 02 404000US01 010 5 02 404000US01 010 2 03 73004000US01 010 3 03 64404000US01 010 4 03 40204000US01 010 5 03 24204000US01 010 6 03 8604000US01 010 13 03 1504000US01 010 15 03 1504000US01 010 19 03 6104000US01 010 2 04 71204000US01 010 3 04 712 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-05-20 : 14:07:38
|
| To clarify, you want to delete all the rows for #1, or just keep 1 row for each group? And for #2, which row do you want to keep? |
 |
|
|
goligol
Posting Yak Master
128 Posts |
Posted - 2011-05-20 : 15:22:20
|
| For # 1 if when there is duplication let say ODMCV0(1) = ODMCV0(2) = ODMCV0(3), I want to keep only one of the records.For # 2 if there is duplication in Key set like ODMC(1) = ODMC(2)=ODMC(3) but V0(1)is not equal to V0(2)and V0(3), here I want to keep one of them no matter which one! |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-05-20 : 15:27:39
|
| SELECT O, D, C, M, MAX(V0) V0 INTO #temp FROM myTable GROUP BY O, D, C, MTRUNCATE TABLE myTableINSERT INTO myTable SELECT * FROM #tempDROP TABLE #temp |
 |
|
|
goligol
Posting Yak Master
128 Posts |
Posted - 2011-05-23 : 11:58:23
|
| Hi,I am doing these queries in a view. and I receive the following error:(390312 row(s) affected)Msg 4708, Level 16, State 2, Line 2Could not truncate object 'dev.testV0' because it is not a table.1-Should I change the format of view to a table or I can do it for a view as well.2- for the records with douplicate keys and distinct values (#2) can I make a average of all the values and keep one key with the average value.Thank you |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-05-23 : 13:16:03
|
| 1. For a view, you would either change the definition to return only one row, or remove the duplicates from the underlying table(s).2. Sure, you can use any aggregate function instead of MAX(). |
 |
|
|
goligol
Posting Yak Master
128 Posts |
Posted - 2011-05-24 : 16:51:13
|
| Thank you. But still I can not make the first query which is SELECT O, D, C, M, MAX(V0) V0 INTO #temp FROM myTable GROUP BY O, D, C, MTRUNCATE TABLE myTableThx |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-05-24 : 18:03:13
|
| You can't use TRUNCATE TABLE on a view, and you probably can't DELETE from it either, and you probably shouldn't for what you're trying to accomplish. Post the CREATE VIEW statement and the CREATE TABLE statements for the tables it accesses. |
 |
|
|
goligol
Posting Yak Master
128 Posts |
Posted - 2011-05-25 : 14:32:43
|
| Create view [dev].[testV0]asSELECT O ,D ,M ,C ,V0 FROM [dev].[GAMS_Init_Var] where V0 is not null That is the "view" that I have to create [testV0]. and for this view I want to do the above 2 queries. One to delete one of the duplications. and the second is to average the value of "V0" for the records with the same key sets of (ODCM).Thanks |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-05-25 : 17:35:03
|
| SELECT O, D, C, M, MAX(V0) V0 INTO #temp FROM [dev].[GAMS_Init_Var] GROUP BY O, D, C, MTRUNCATE TABLE [dev].[GAMS_Init_Var]INSERT INTO [dev].[GAMS_Init_Var] SELECT * FROM #tempDROP TABLE #temp |
 |
|
|
goligol
Posting Yak Master
128 Posts |
Posted - 2011-05-27 : 11:30:28
|
| When I try the above mentioned statement:SELECT O, D, C, M, MAX(V0) V0 INTO #temp FROM [dev].[V0] GROUP BY O, D, C, MTRUNCATE TABLE [dev].[V0]INSERT INTO [dev].[V0] SELECT * FROM #tempDROP TABLE #tempit gives me the following error:Msg 4406, Level 16, State 1, Line 3Update or insert of view or function 'dev.GAMS_Init_Var' failed because it contains a derived or constant field.and I have to mention that the view V0 is made from dev.GAMS_Init_Var as:cview [dev].[V0]asSELECT O ,D ,M ,C ,V0 FROM [dev].[GAMS_Init_Var] where V0 is not null GO |
 |
|
|
goligol
Posting Yak Master
128 Posts |
Posted - 2011-05-31 : 10:06:39
|
| is there any comment! |
 |
|
|
|
|
|
|
|