我正在使用SQL workbench / J连接到amazon redshift。
我在表格中有以下数据(需要保留更多列,但每个唯一的claim_id都是完全相同的值,无论行号如何):
Member ID | Claim_ID | Line_Number |
1 100 1
1 100 2
1 100 1
1 100 2
2 101 13
2 101 13
2 101 13
2 101 13
3 102 12
3 102 12
1 103 2
1 103 2
我希望它成为以下内容,将删除任何基于claim_id的重复项(保留哪个行号并不重要):
Member ID | Claim_ID | Line_Number |
1 100 1
2 101 13
3 102 12
1 103 2
我尝试了以下内容:
select er_main.member_id, er_main.claim_id, er_main.line_number,
temp.claim_id, temp.line_number
from OK_ER_30 er_main
inner join (
select row_number() over (partition by claim_id order by line_number desc) as seqnum
from
OK_ER_30 temp) temp
ON er_main.claim_id = temp.claim_id and seqnum = 1
Order by er_main.claim_id, temp.line_number
和此:
select * from ok_er_30
where claim_id in
(select distinct claim_id
from ok_er_30
group by claim_id
)
order by claim_id desc
我已经检查了许多其他方法,每个不同的claim_id只拉一行,但没有任何效果。
答案 0 :(得分:1)
尝试
select Distant(Member_ID,Claim_ID,max(Line_Number)) group by Member_ID,Claim_ID
答案 1 :(得分:0)
试试这个,
select Member_ID,Claim_ID,max(Line_Number) group by Member_ID,Claim_ID
答案 2 :(得分:0)
查看以下代码。
declare @OK_ER_30 table(Member_ID int, Claim_ID int, Line_Number int);
insert @OK_ER_30 values
(1, 100, 1),
(1, 100, 2),
(1, 100, 1),
(1, 100, 2),
(2, 101, 13),
(2, 101, 13),
(2, 101, 13),
(2, 101, 13),
(3, 102, 12),
(3, 102, 12),
(1, 103, 2),
(1, 103, 2);
with
t as(
select *, row_number() over(
partition by Member_ID, Claim_ID order by (select 0)
) rn
from @OK_ER_30
)
delete from t where rn > 1;
select * from @OK_ER_30;