复杂SQL选择使用group by

时间:2015-10-12 18:11:24

标签: sql sql-server-2008

请说明你有这张表:

Name    C1   C2
Name1    A   1
Name1    B   2
Name2    B   1
Name2    A   2

我需要的是:

Name1   B  2
Name2   B  1

我试试

select Name, max(C1), max(C2) from table
group by Name

但它不起作用, 请帮忙。

1 个答案:

答案 0 :(得分:1)

试试这个:

Select
    A.*
From [Table] A
Join (Select Name, MAX(C2) From [Table] Group By [Name]) B
    On A.Name = B.Name And A.C2 = B.C2