求助!!!

llhlhy 2015-11-20 11:36:14
表如:


想查询如:甲+乙时能得到下面的结果:


求各路大神帮忙。
...全文
119 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
苍松 2015-11-20
  • 打赏
  • 举报
回复
SELECT DISTINCT * FROM #test WHERE 单位 in ('甲公司','乙公司') and 对方单位 in ('甲公司','乙公司')
Yole 2015-11-20
  • 打赏
  • 举报
回复

 
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 行受影响)
*/


Liyp92 2015-11-20
  • 打赏
  • 举报
回复
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

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧