sql - adding the sum of tables into one of the tables with a new date -


i have 2 tables, examples below

tblorders

id   quantity abc  5     xyz  25 mno  -50 wwl  -35 

tblholdings

date   id   quantity 2-jan  abc  100     2-jan  ppp  12 2-jan  mno  200 2-jan  wwl  35 

what need take current holdings tblholdings , add data tblorders & insert results tblholdings new date. result should below. please note wwl has 0 quantity there should new record wwl new date.

tblholdings

date   id   quantity 2-jan  abc  100     2-jan  ppp  12 2-jan  mno  200 2-jan  wwl  35 3-feb  abc  105     3-feb  ppp  12 3-feb  mno  150 3-feb  xyz  25    

is possible using sql?

try such query:

insert tblholdings (date, id, quantity) select '3-feb', h.id, h.quantity + coalesce(o.quantity, 0) tblholdings h left join tblorders o on h.id = o.id h.quantity <> o.quantity 

the idea use select statement data need, , use select insert data holdings table


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -