22,300
社区成员




SELECT DISTINCT * FROM #test WHERE 单位 in ('甲公司','乙公司') and 对方单位 in ('甲公司','乙公司')
create table #test
(
单位 varchar(20),
对方单位 varchar(20),
应收 int,
应付 int
)
go
insert #test
select '甲公司','乙公司',100,0 union all
select '甲公司','丙公司',200,0 union all
select '甲公司','丁公司',0,320 union all
select '乙公司','甲公司',0,100 union all
select '乙公司','丁公司',500,0 union all
select '丁公司','甲公司',320,0
select distinct a.* from #test a ,#test b
where a.单位=b.对方单位 and a.对方单位=b.单位
and
(
( a.单位='甲公司'
and a.对方单位='乙公司')or
( a.单位='乙公司'
and a.对方单位='甲公司')
)
drop table #test
/*
单位 对方单位 应收 应付
-------------------- -------------------- ----------- -----------
甲公司 乙公司 100 0
乙公司 甲公司 0 100
(2 行受影响)
*/
create table #Table
(
单位 varchar(20),
对方单位 varchar(20),
应收 int,
应付 int
)
go
insert #Table
select '甲公司','乙公司',100,0 union all
select '甲公司','丙公司',200,0 union all
select '甲公司','丁公司',0,320 union all
select '乙公司','甲公司',0,100 union all
select '乙公司','丁公司',500,0 union all
select '丁公司','甲公司',320,0
select distinct a.* from #Table a ,#Table b
where a.单位=b.对方单位 and a.对方单位=b.单位
drop table #Table
单位 对方单位 应收 应付
-------------------- -------------------- ----------- -----------
丁公司 甲公司 320 0
甲公司 丁公司 0 320
甲公司 乙公司 100 0
乙公司 甲公司 0 100