这是我的问题 我有1+ mil行和50 +列的表,其中保留所有记录更改 因为需要删除列的表大小并且需要删除所有重复的记录
----------------------------------------
ID otherID a b | c
1 10 1 2 | 1
2 10 1 2 | 2
3 20 2 2 | 3
4 10 1 2 | 4
5 10 10 2 | 5
6 10 10 2 | 6
7 10 1 2 | 7
---------------------------------------
所以我要说删除C行,我只需要保留第一条唯一记录 我需要的记录是1,3,5,7 我的问题是我如何分组1和2和4而不是第6行
答案 0 :(得分:0)
您可以使用row_number()
方法的差异:
select min(Id), otherId, a, b
from (select t.*,
(row_number() over (order by id) -
row_number() over (partition by otherId, a, b order by id)
) as grp
) t
group by otherId, a, b, grp;
对于相邻行中具有相同值的行,行号的不同是常量。
答案 1 :(得分:0)
U可以使用以下查询获得第1,3和5行
SELECT * FROM (SELECT tbl.*,
ROW_NUMBER() OVER (PARTITION BY otherId, a, b ORDER BY id)
AS rn
FROM [tablename] tbl
) t
WHERE rn = 1
但是获得第7行的要求并不明确,因为它不是唯一的行(与第1,2,4行相同
答案 2 :(得分:0)
如果我做对了
declare @T TABLE
(ID int, otherID int, a int, b int)
;
INSERT INTO @T(ID, otherID, a, b)
VALUES
(1, 10, 1, 2),
(2, 10, 1, 2),
(3, 20, 2, 2),
(4, 10, 1, 2),
(5, 10, 10, 2),
(6, 10, 10, 2),
(7, 10, 1, 2)
;
with grp as (
select ID, otherID, a, b,
grp = ROW_NUMBER()OVER(PARTITION BY otherID ORDER BY (id))
- ROW_NUMBER()OVER(PARTITION BY otherID, a, b ORDER BY (id))
from @T
), filter as (
select ID, otherID, a, b, grp,
rn = ROW_NUMBER()OVER(PARTITION BY grp, otherID, a, b ORDER BY (id))
from grp
)
delete from filter
where rn >1;
select *
from @T
order by id