SQL:删除一列中的重复项,同时保留另一列中具有最高值的行
SQL: Removing Duplicates in one column while retaining the row with highest value in another column
我正在使用 Teradata 并且在尝试编写一些代码时遇到困难...我想根据 ColumnA 中的值删除 columnB 具有重复值的行 - 如果有人可以帮助我,那将是太棒了!
我在 columnA 中有一个序号,我想保留 columnA 中具有最高值的行。
例如。在下面 table 我想保留第 9、7、6 和 2 行,因为尽管它们在第 2 列中有重复,但它们具有该字母的最高 ColumnA 值。
Table 名称:数据Table
Column1 Column2 Column3 Column4 Column5
1 B X X X
2 A 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
如果你只想select行,你可以这样做:
select t.*
from t
where t.columnA = (select max(t2.columnA) from t t2 where t2.columnB = t.columnB);
如果您真的想删除它们,那么一种方法是:
delete from t
where t.columnA < (select max(t2.columnA) from t t2 where t2.columnB = t.columnB);
如果您想 return 使用 SELECT
那些行,则不需要相关子查询,OLAP 函数通常执行得更好:
select *
from tab
qualify
row_number() over (partition by ColumnB order by columnA DESC) = 1
如果您确实想要 DELETE
其他行用于 Gordon 的查询。
我正在使用 Teradata 并且在尝试编写一些代码时遇到困难...我想根据 ColumnA 中的值删除 columnB 具有重复值的行 - 如果有人可以帮助我,那将是太棒了!
我在 columnA 中有一个序号,我想保留 columnA 中具有最高值的行。 例如。在下面 table 我想保留第 9、7、6 和 2 行,因为尽管它们在第 2 列中有重复,但它们具有该字母的最高 ColumnA 值。
Table 名称:数据Table
Column1 Column2 Column3 Column4 Column5
1 B X X X
2 A 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
如果你只想select行,你可以这样做:
select t.*
from t
where t.columnA = (select max(t2.columnA) from t t2 where t2.columnB = t.columnB);
如果您真的想删除它们,那么一种方法是:
delete from t
where t.columnA < (select max(t2.columnA) from t t2 where t2.columnB = t.columnB);
如果您想 return 使用 SELECT
那些行,则不需要相关子查询,OLAP 函数通常执行得更好:
select *
from tab
qualify
row_number() over (partition by ColumnB order by columnA DESC) = 1
如果您确实想要 DELETE
其他行用于 Gordon 的查询。