SQL按一列获取不同的行,但按第三列排序的另一列也获取不同的行
我有以下一组数据: 这是一个交叉联接的输出,它使用一个函数将左侧的每个值与右侧的每个值进行比较,但这有点不相关 我需要从中得到两行,在这个特殊情况下,我需要第一行和第四行 逻辑是。。。我希望在具有最高accu值的b_索引列上进行区分,但不希望在具有已使用的a列的行上进行区分 所以。。第一行必须是b_index=1,b_index的最大累计值为95,所有a_索引值都可用,所以我想要那一行。这使得_索引2对下一行不可用 第二个b_索引是2,查找所有可用的a_索引值,在这种情况下,我只有一个可用的a_索引1,我想要accu最高的行 我已经使用stackoverflow很长一段时间了,但这是我的第一篇帖子,如果我做错了,请原谅: 我提前向你寻求帮助 更新:SQL按一列获取不同的行,但按第三列排序的另一列也获取不同的行,sql,distinct,Sql,Distinct,我有以下一组数据: 这是一个交叉联接的输出,它使用一个函数将左侧的每个值与右侧的每个值进行比较,但这有点不相关 我需要从中得到两行,在这个特殊情况下,我需要第一行和第四行 逻辑是。。。我希望在具有最高accu值的b_索引列上进行区分,但不希望在具有已使用的a列的行上进行区分 所以。。第一行必须是b_index=1,b_index的最大累计值为95,所有a_索引值都可用,所以我想要那一行。这使得_索引2对下一行不可用 第二个b_索引是2,查找所有可用的a_索引值,在这种情况下,我只有一个可用的a_
CREATE TABLE #example
(
a_index int,
b_index int,
a varchar(max),
b varchar(max),
accu int
)
INSERT INTO #example (a_index, b_index, a, b, accu)
VALUES
(3,1,'dddd','dddd',95),
(1,1,'aaaa','dddd',0),
(2,1,'bbbb','dddd',0),
(1,2,'aaaa','aaaa',95),
(3,2,'dddd','aaaa',0),
(2,2,'bbbb','aaaa',0),
(1,3,'aaaa','aaaa',95),
(3,3,'dddd','aaaa',0),
(2,3,'bbbb','aaaa',0)
它也可以以另一种方式工作,但从b_索引列开始,列表被预先排序为更容易
a_index 1 -> b_index 3
a_index 3 -> b_index 1 (because a_index 3 has a higher max accu then 2)
a_index 2 -> b_index 2
您可以忽略a列和b列,它们是i相关的每次a是否存在于其他父组中时,您都需要类似于下面相关子查询的内容来检查
Select Max(aindex),
bindex,max(a) , b,
max(accunit)
From (Select a.index, * from table
where bindex, b in (Select 1,2 from(
Select bindex
, b,
max(accunit)
From table group by bindex,
b)
)) t group
by bindex, b having
1=max( Case when aindex <>
t.aindex then 1
Else 0 end) ;
我可能会继续尝试寻找一个递归的CTE解决方案,我对rCTE还是新手。但是,我认为这个游标解决方案会起作用:
declare @example table
(
a_index int,
b_index int,
a varchar(max),
b varchar(max),
accu int
)
INSERT INTO @example (a_index, b_index, a, b, accu)
VALUES
(3,1,'dddd','dddd',95),
(1,1,'aaaa','dddd',0),
(2,1,'bbbb','dddd',0),
(1,2,'aaaa','aaaa',95),
(3,2,'dddd','aaaa',0),
(2,2,'bbbb','aaaa',0),
(1,3,'aaaa','aaaa',95),
(3,3,'dddd','aaaa',0),
(2,3,'bbbb','aaaa',0)
declare @output table (a_index int, b_index int)
declare @b_index int
declare out_cursor cursor
for
select distinct b_index
from @example
order by b_index
;
open out_cursor
fetch next from out_cursor into @b_index
while @@fetch_status = 0
begin
insert into @output
select min(a_index) a_index, a.b_index
from (
select b_index, max(accu) accu
from @example
where b_index = @b_index
and a_index not in (select a_index from @output)
group by b_index
) a
inner join @example e
on a.b_index=e.b_index
and a.accu=e.accu
and a_index not in (select a_index from @output)
group by a.b_index
fetch next from out_cursor into @b_index
end
close out_cursor
deallocate out_cursor
select *
from @output
示例数据、所需结果和适当的数据库标记都会有所帮助。如果存在没有唯一a_索引的b_索引,则不应选择该特定b_索引?这感觉像是递归CTE。递归CTE可能是解决方案,但我不知道如何解决。我希望有更多经验的人能看到解决方案。可能是最简单的光标。真不敢相信我刚刚写的。我不认为hot会起作用,而且sql会抛出以下错误:当子查询没有引入EXISTS时,在选择列表中只能指定一个表达式。嗯。。它应该已经工作了,我会尝试更新我的查询。调整它以工作,你忘记了buggest accu。但我们非常感谢您的努力。使用cursor会有一些性能损失,我更喜欢性能更好的版本,但目前这是可行的。谢谢最内部的子查询有select b_索引,maxaccu accu,它首先获取最大的accu。我的意思是首先获取accu最高的一对,而不是从第一个b_索引中获取最高的accu。不这样做可能会排除较高的accu对,因为该对的_索引已被消除。我希望我能让别人明白我的意思。话虽如此,我想我并不清楚自己想要的是什么:再次感谢你的帮助!你的意思是总体?你想从与最高累计相关的任何b_指数开始,然后是下一个最高累计?而不是像我想象的那样,仅仅是按照b_指数的升序进行?那确实会有点不同。不管怎样-很高兴你能成功。我不知道是否存在一个好的递归CTE解决方案,因为您必须考虑整个先前的答案集,而不仅仅是先前递归迭代的结果集。
declare @example table
(
a_index int,
b_index int,
a varchar(max),
b varchar(max),
accu int
)
INSERT INTO @example (a_index, b_index, a, b, accu)
VALUES
(3,1,'dddd','dddd',95),
(1,1,'aaaa','dddd',0),
(2,1,'bbbb','dddd',0),
(1,2,'aaaa','aaaa',95),
(3,2,'dddd','aaaa',0),
(2,2,'bbbb','aaaa',0),
(1,3,'aaaa','aaaa',95),
(3,3,'dddd','aaaa',0),
(2,3,'bbbb','aaaa',0)
declare @output table (a_index int, b_index int)
declare @b_index int
declare out_cursor cursor
for
select distinct b_index
from @example
order by b_index
;
open out_cursor
fetch next from out_cursor into @b_index
while @@fetch_status = 0
begin
insert into @output
select min(a_index) a_index, a.b_index
from (
select b_index, max(accu) accu
from @example
where b_index = @b_index
and a_index not in (select a_index from @output)
group by b_index
) a
inner join @example e
on a.b_index=e.b_index
and a.accu=e.accu
and a_index not in (select a_index from @output)
group by a.b_index
fetch next from out_cursor into @b_index
end
close out_cursor
deallocate out_cursor
select *
from @output