求一left /right join 语句,请大哥们帮忙哦!在线等待,解决立即结帖!!

rr 2004-09-14 10:27:34
语句如下。。
Select (case when A.total is null then 0 else A.total end) - (case when B.total is null then 0 else B.total end)
from (select sum(LeftAmtOrig) as total,tAccountID
from t_ArBill where Status <> '3' and tAccountID = '22' GROUP BY tAccountID ) as A
--------------------------------下面是关键,请问如何实现??谢谢哦
(case when A.total is null then right join
when B.total is null then left join
else join end)
------------------------------------
(select sum(LeftAmtOrig) as total,tAccountID from tReceiveBill
where Status <> '3' and checkFlag = 'A' and tAccountID = '22' GROUP BY tAccountID) AS B
on A.tAccountID = B.tAccountID
...全文
112 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rr 2004-09-14
  • 打赏
  • 举报
回复
风云大哥,
谢谢你的指点。
其实只要这么写就可以了。。

Select (case when A.total is null then 0 else A.total end) - (case when B.total is null then 0 else B.total end)
from (select sum(LeftAmtOrig) as total,tAccountID
from t_ArBill where Status <> '3' and tAccountID = '22' GROUP BY tAccountID ) as A
--------------------------------下面是关键,请问如何实现??谢谢哦
full join --这么改就可以解决我的问题了。看来我sql还没学好。
------------------------------------
(select sum(LeftAmtOrig) as total,tAccountID from tReceiveBill
where Status <> '3' and checkFlag = 'A' and tAccountID = '22' GROUP BY tAccountID) AS B
on A.tAccountID = B.tAccountID
pbsql 2004-09-14
  • 打赏
  • 举报
回复
a,b都有记录则inner join,否则full join:
if exists(select * from t_ArBill where Status <> '3' and tAccountID = '22')
and exists(select * from tReceiveBill where Status <> '3' and checkFlag = 'A' and tAccountID = '22')
Select A.total - B.total
from (select isnull(sum(LeftAmtOrig),0) as total,tAccountID from t_ArBill
where Status <> '3' and tAccountID = '22' GROUP BY tAccountID ) as A
join
(select isnull(sum(LeftAmtOrig),0) as total,tAccountID from tReceiveBill
where Status <> '3' and checkFlag = 'A' and tAccountID = '22' GROUP BY tAccountID) AS B
on A.tAccountID = B.tAccountID
else
Select isnull(A.total,0) - isnull(B.total,0)
from (select isnull(sum(LeftAmtOrig),0) as total,tAccountID from t_ArBill
where Status <> '3' and tAccountID = '22' GROUP BY tAccountID ) as A
full join
(select isnull(sum(LeftAmtOrig),0) as total,tAccountID from tReceiveBill
where Status <> '3' and checkFlag = 'A' and tAccountID = '22' GROUP BY tAccountID) AS B
on A.tAccountID = B.tAccountID
rr 2004-09-14
  • 打赏
  • 举报
回复
如何实现??请大哥们帮写写呀。。谢谢哦!
lsxaa 2004-09-14
  • 打赏
  • 举报
回复
用动态SQL 语句吧
exec('...................')

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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