Sql server SQL Server获取唯一行或字段值最大的行

Sql server SQL Server获取唯一行或字段值最大的行,sql-server,stored-procedures,function,duplicates,Sql Server,Stored Procedures,Function,Duplicates,我在SQLServer2005中有一个表,如下所示,比如字段a、B、C、D 如果我有以下数据: A B C D 1 B1 C1 D1 - 2 B1 C1 D1 - 3 B2 C2 D2 - 4 B2 C2 D2 - 5 B2 C2 D2 - 6 B3 C3 D3 - 我希望能够挑出唯一的行(在B、C和D上),或者,如果不是唯一的(在B、C和D上),那

我在SQLServer2005中有一个表,如下所示,比如字段a、B、C、D

如果我有以下数据:

A     B     C     D

1     B1    C1    D1 -
2     B1    C1    D1 -
3     B2    C2    D2 -
4     B2    C2    D2 -
5     B2    C2    D2 -
6     B3    C3    D3 -
我希望能够挑出唯一的行(在B、C和D上),或者,如果不是唯一的(在B、C和D上),那么我希望只挑出A字段中具有最大值的行。因此,我想在上述场景中返回第2行(A=2)、第5行和第6行。如果可能的话,我不想使用游标,而是基于集合的操作

有没有什么方法可以实现这一点,可以用在存储过程或表函数中

提前感谢,,
蒂姆:那应该很容易。只需按
b、c、d进行分组,然后使用
MAX
获得
a
的最大值

Select Max(a), b, c, d
From your_table
Group By b, c, d

假设您可能有更多的列要返回

with cte as
(
select a,b,c,d,
row_number() over (partition by b,c,d order by a desc) as rn
from yourtable
)
select a,b,c,d
from cte
where rn=1

“分组方式”在这个特定场景中似乎简单得多,(但无论如何+1)@Michael-如果没有其他列可返回,我同意。谢谢!这正是我要找的!