按查询分组提示

时间:2015-03-28 19:52:06

标签: sql sql-server

在sql server中,我有一个产品表如下

Manf    Prod    Id  Desc
Audi    A1  1   Tyre tye 1
Audi    A1  2   Tyre type 2
Audi    A1  3   Tyre type 3
BMW     B1  4   Tyre tye 1
BMW     B1  5   Tyre type 2
BMW     B1  6   Tyre type 3
Toyota  T1  7   Tyre type 1

我想用manf和prod的列输出组,但id应该是任何值..请告知查询..

Manf    Prod    Id
Audi    A1      1
BMW     B1      4
Toyota  T1      7

2 个答案:

答案 0 :(得分:4)

select Manf, Prod, min(id) as Id
from your_table
group by Manf, Prod

答案 1 :(得分:1)

如果您想要一个完整的行,可以使用row_number()

select t.*
from (select t.*,
             row_number() over (partition by manf, prod order by (select NULL)) as seqnum
      form table t
     ) t
where seqnum = 1;