Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.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,我正在尝试做这个查询。这就是我所拥有的 我的桌子是:桌子 QId InternalId type. priority userid 100 100 1 0 X101 100 100 1 1 X102 100 100 2 0

我正在尝试做这个查询。这就是我所拥有的

我的桌子是:桌子

QId    InternalId      type.     priority       userid
100       100              1            0                X101
100       100              1            1                X102
100       100              2            0                X103
100       100              2            0                X104
100       100              2            0                X105
100       100              3            0                X106
100       100              3            0                X107
101       101              2            1                X114
101       101              2            0                X115
101       101              3            0                X116
101       101              3            0                X117
对于QId和InternalId,我们有类型1,2,3。我需要为每个基于组的类型设置一行。这里的条件是,如果优先级为1,那么我们需要获取该记录。如果未设置优先级,则需要获取第一条记录

我需要的结果如下表

QId    InternalId      type.     priority       userid
100       100              1            1                X102
100       100              2            0                X103
100       100              3            0                x106
101       101              2            1                X114
101       101              3            0                X116
你能帮我解决这个问题吗?

只需使用
行号()

使用
行号()
窗口函数

select t.* from (select *,row_number()over(partition by QId,InternalId,type order by case when priority=1 then 1 else 2 end) rn
) where t.rn=1

您可以尝试使用
row\u number()


另一种方法可以是带领带的
,如下所示

select top 1 with ties *
from @table
order by row_number() over (partition by QId, InternalId, type order by (select 1))

“我正在尝试执行此查询”显示您的尝试定义“第一条记录”。第一张唱片是关于什么的?
select * from
(
select *, row_number() over(partition by qid, internalid, type order by priority desc) as rn
from tablename
)A where rn=1
select top 1 with ties *
from @table
order by row_number() over (partition by QId, InternalId, type order by (select 1))