这就是我正在使用的表的性质:
IF OBJECT_ID('TEMPDB..#TEMP') IS NOT NULL
DROP TABLE #TEMP
CREATE TABLE #TEMP (
CategoryA NVARCHAR(10),
CategoryB NVARCHAR(10),
CategoryC NVARCHAR(10),
IntegerA INT,
);
INSERT INTO #TEMP(CategoryA,CategoryB,CategoryC,IntegerA)
VALUES
('A','H','G',20),
('A','H','G',-15),
('F','L','C',10),
('N','U','X',12),
('K','G','G',15),
('K','G','G',-10);
SELECT * FROM #TEMP请注意,顶部2行和底部2行具有相同的类别,但是它们具有相反极性的整数。中间2行与正整数不同。
我需要一种方法来选择所有没有重复的记录(例如中间的2行)。并且我需要选择具有负整数的记录,而不选择它们的正对等项。
在这种情况下,期望的输出将是:

我试过是否可以创建自己的表,只插入我想要的记录,但我又遇到了同样的问题,我不知道如何区分所有类别都相同的记录。
发布于 2020-04-09 05:46:32
对于此数据集,您只需使用row_number()
select categoryA, categoryB, categoryC, integerA
from (
select
t.*,
row_number() over(partition by categoryA, categoryB, categoryC order by integerA) rn
from temp t
) t
where rn = 1发布于 2020-04-09 05:43:36
嗯。。。我认为你想要:
select t.*
from #temp t
where t.integerA < 0 or
not exists (select 1
from #temp t2
where t2.A = t.A and t2.B = t.B and
t2.C = t.c and t2.integerA < 0
);Here是一个db<>fiddle。
https://stackoverflow.com/questions/61110478
复制相似问题