sql實作功能,將a表中的a5資料根據a4的標志進行加減求和,將得到的和與b表中的b欄位進行求和并更新b表
update tb b
set b.b = b.b+(select sum(decode(a.a4, '0', a.a5, '1',(0-a.a5), 0)) from ta a
where a.a1 = b.b1 and a.a2 = b.b2 and a.a3=b.b3)
where b.b1 ='4343' and b.b2 in (select a.a2 from ta a where a.a1 = b.b1 and a.a2 = b.b2 and a.a3=b.b3)
這種sql我感覺好丑啊,好多條件都重復了。于是我又寫的下面的這個sql
merge into tb b
using (select a.a1,a.a2,a.a3,sum(decode(a.a4, '0', a.a5, '1',(0-a.a5), 0) tall from ta a group by a.a1,a.a2,a.a3 ) c)
on(b.b1= c.c1 and b.b2 = c.c2 and b,b3 = c.c3 and b.b1 = '4343')
when matched then
update set b.b = b.b+c.tall
群里大神有沒有更好的更新陳述句,大家交流一下。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/269226.html
標籤:高級技術
