Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 分组查询建议_Sql_Sql Server - Fatal编程技术网

Sql 分组查询建议

Sql 分组查询建议,sql,sql-server,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 我想用m

在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的group by列输出,但id应该是任意一个值。。请告知我的问题

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

如果需要完整的行,可以使用
行号()

select Manf, Prod, min(id) as Id
from your_table
group by Manf, Prod
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;