SQL: Removing Duplicates in one column while retaining the row with highest value in another column -
i using teradata , stuck trying write code... remove rows in columnb has duplicate value, based on values in columna - if can me great!
i have sequencial number in columna , retain row highest value in columna. eg. in below table retain rows 9,7,6 & 2, because although have duplicate in column 2 have highest columna value letter.
table name: datatable
column1 column2 column3 column4 column5 1 b x x x 2 y y y 3 e z z z 4 b x x x 5 c y y y 6 e z z z 7 c x x x 8 b y y y 9 b z z z
if want select rows, can do:
select t.* t t.columna = (select max(t2.columna) t t2 t2.columnb = t.columnb);
if want remove them, 1 method is:
delete t t.columna < (select max(t2.columna) t t2 t2.columnb = t.columnb);
Comments
Post a Comment