已知条件 表t里有多行记录,同一个GroupID简称一个组,每一组的多行数字和其他其他一组里的数字是重复的就保留一次。
问题:如何用SQL判断不同的一组里每一行notext数字都是和其他一组里的notext数字是重复的,去重复后保留一次,请专家解答。
;with t(ID,GroupID,NoText) as(
select '1','1','1 2 3'
union all select '2','1','4 5 6'
union all select '3','1','7 8 9'
union all select '4','2','1 2 3'
union all select '5','2','4 5 6'
union all select '6','2','7 8 9'
union all select '7','3','2 3 4'
union all select '8','3','4 5 6'
union all select '9','3','7 8 9'
union all select '10','4','2 3 4'
union all select '11','4','4 5 6'
union all select '12','4','7 8 9'
union all select '13','5','1 2 3'
union all select '14','5','4 5 6'
union all select '15','5','6 7 8')
select ID,GroupID,NoText from t
WHERE ID in
(select min(id) from t group by notext)
order by GroupID
题主的代码执行的结果是错误的,实际上想要的结果
---------------------------
ID GroupID NoText
1 1 1 2 3
2 1 4 5 6
3 1 7 8 9
7 3 2 3 4
8 3 4 5 6
9 3 7 8 9
13 5 1 2 3
14 5 4 5 6
15 5 6 7 8
DROP TABLE IF EXISTS #t
SELECT *, cast ('' AS NVARCHAR(200)) AS AllText INTO #t FROM(
SELECT '1' ID,'1' GroupID,'1 2 3' NoText
union all select '2','1','4 5 6'
union all select '3','1','7 8 9'
union all select '4','2','1 2 3'
union all select '5','2','4 5 6'
union all select '6','2','7 8 9'
union all select '7','3','2 3 4'
union all select '8','3','4 5 6'
union all select '9','3','7 8 9'
union all select '10','4','2 3 4'
union all select '11','4','4 5 6'
union all select '12','4','7 8 9'
union all select '13','5','1 2 3'
union all select '14','5','4 5 6'
union all select '15','5','6 7 8'
) t
DECLARE @allText NVARCHAR(max)='',@GroupID VARCHAR(10)='0'
update #t SET @allText=CASE WHEN @GroupID=GroupID THEN @allText+NoText ELSE NoText END,@GroupID=GroupID,AllText=@allText
SELECT ID, GroupID, NoText
FROM #t
WHERE GroupID IN ( SELECT MIN(GroupID)FROM #t WHERE LEN(AllText) = 15 GROUP BY AllText );
如果组2是456,123,789 ,查询结果会有组2么
可以用listagg函数处理一下,但是有个缺陷,这个函数是根据查询结果排序的,所以要根据大小先进行排序:
delete from t where groupid in(
select max(groupid) from (select groupid,listagg(notext) mm from (select * from t order by groupid,NoText) group by groupid) m
group by m.mm having count(1)>1);
或者用排序开窗一个一个比较。。
比如rn=1的notext and rn=2的notext and rn=3的notext同时满足