对于第一输出: -
/***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;
希望这有助于:-)
你尝试过这么远吗? –
其实我需要做到这一点是sas,提出这只是为了看看你如何工作这是SQL。 – ammosperros
如果你的数据源有一个类似'(1111,5678)'的记录呢? – toonice