SQL:标记具有重复值但在备用列中的多个记录

时间:2017-04-22 04:52:12

标签: sql sql-server-2008 sas

我有问题。我有SQL表,它有两个变量,但交替重复。 所以如果我们有一张桌子

A_ID   OTHER_ID   
1111      2222   
2222      1111    
0000      0101  
3333      5555   
5555      3333

我想要一张独特的桌子

A_ID   OTHER_ID       
1111    2222          
0000    0101   
3333    5555

或者,

A_ID   OTHER_ID  FINAL_ID   
1111    2222     2222      
2222    1111     2222          
0000    0101     0101        
3333    5555     3333     
5555    3333     5555

感谢。

2 个答案:

答案 0 :(得分:1)

 Private Sub control1_mouseclick1(sender As Object, e As EventArgs) Handles PictureBox2.Click, PictureBox3.Click, PictureBox1.Click, PictureBox4.Click
    Dim p As PictureBox = CType(sender, PictureBox)
    Select Case CInt(p.Tag)
        Case 1



    End Select
End Sub

表temp创建一个键变量,一旦它们按数字排序,就是A_ID和OTHER_ID的八位串联。然后通过查看此关键变量可以找到不同的组合。

表格temp看起来像这样:

data dat;
    input A_ID $ OTHER_ID $;
    datalines;                      
1111      2222   
2222      1111    
0000      0101  
3333      5555   
5555      3333
;

proc sql;
    create table temp as
    select A_ID, OTHER_ID, 
    case 
        when (A_ID<OTHER_ID) then cat(A_ID,OTHER_ID)
        when (OTHER_ID<A_ID) then cat(OTHER_ID,A_ID)
        end as key
    from dat
    order by A_ID;
quit;

data final;
    retain A_ID OTHER_ID;
    set temp;
    by key;
    if first.key;
run;

答案 1 :(得分:0)

首次输出: -

/***In SQL server***/    
CREATE TABLE TABLES2(A_ID varchar(20), OTHER_ID varchar(20));

INSERT INTO TABLES2 VALUES('1111','2222');
INSERT INTO TABLES2 VALUES('2222','1111');
INSERT INTO TABLES2 VALUES('0000','0101');
INSERT INTO TABLES2 VALUES('3333','5555');
INSERT INTO TABLES2 VALUES('5555','3333');


select A_ID ,OTHER_ID 
from
(
Select distinct A_ID ,OTHER_ID,(lag(OTHER_ID) over (order by A_ID)) as col_lag
from tables2
) a
where A_ID <> col_lag or col_lag is null


 /****OUTPUT****/
A_ID    OTHER_ID
0000    0101
1111    2222
3333    5555

select A_ID ,OTHER_ID 
from
(
Select distinct A_ID ,OTHER_ID,(lag(OTHER_ID) over (order by A_ID)) as col_lag
from tables2
) a
where A_ID <> col_lag or col_lag is null    


/***In SAS with same logic using lag function***/

data have;
    input A_ID  OTHER_ID ;
    datalines;                   
1111      2222   
2222      1111  
0000      0101 
3333      5555  
5555      3333  
;

data have;
set have;
col_lag =lag(OTHER_ID);
run;

/* if character columns*/
proc sql;
create table answer as
select A_ID ,OTHER_ID from
(
Select distinct A_ID ,OTHER_ID,col_lag
from have
) a
where col_lag not= A_ID or col_lag not= ' ';
quit;

/* if numeric columns*/
proc sql;
create table answer as
select A_ID ,OTHER_ID from
(
Select distinct A_ID ,OTHER_ID,col_lag
from have
) a
where col_lag not= A_ID or col_lag not=.;
quit;

希望这会有所帮助: - )