sql server - T-SQL INSERT into UNLESS -


i have following table:

account              | period   | amount  -------------------- | -------- | ------ umbrella corporation | 201601   | 100 umbrella corporation | 201602   | 50 umbrella corporation | 201608   | 100 acme inc             | 201504   | 85 acme inc             | 201504   | 90 acme inc             | 201512   | 40 

[period] plain text, represents date yyymm , may include yyymmdd too, purpose, ignore day.

goal

insert new row each unique combination of [account] , 12 possible time-periods each year (yyyymm).

example

-> if unique combination exists in table, nothing -> if unique combination not exist, insert new row account, period , amount (the amount being 0 newly inserted rows).  

desired outcome

account                 | period    | amount  --------------------    | --------  | ------ umbrella corporation    | 201601    | 100 umbrella corporation    | 201602    | 100 umbrella corporation    | 201603    | 0 umbrella corporation    | 201604    | 0 umbrella corporation    | 201605    | 0 umbrella corporation    | 201606    | 0 umbrella corporation    | 201607    | 0 umbrella corporation    | 201608    | 100 umbrella corporation    | 201609    | 0 umbrella corporation    | 201610    | 0 umbrella corporation    | 201611    | 0 umbrella corporation    | 201612    | 0 acme inc                | 201501    | 0 acme inc                | 201502    | 0 acme inc                | 201503    | 0 acme inc                | 201504    | 85 acme inc                | 201504    | 90 acme inc                | 201605    | 0 acme inc                | 201506    | 0 acme inc                | 201507    | 0 acme inc                | 201508    | 0 acme inc                | 201509    | 0 acme inc                | 201510    | 0 acme inc                | 201511    | 0 acme inc                | 201512    | 40 

i've not been able figure out solid starting point doing this. i've found similar asks here use insert into....where not exists or merge or join. ideally, i'd achieve result without needing table, if @ possible.

any guidance appreciated. i'm using sql server 2008r2.

*i've tried make question, if think better, please let me know.

one option use cte create tally table determine possible month intervals. following example demonstrates approach.

-- create example table , sample data set. create table accounts (      account    nvarchar(100)     ,period     nvarchar(10)     ,amount     float )  insert accounts (  account ,period  ,amount  ) select 'umbrella corporation' , '201601'   , 100    union select 'umbrella corporation' , '201602'   , 50     union select 'umbrella corporation' , '201608'   , 100    union select 'acme inc'             , '201504'   , 85     union select 'acme inc'             , '201504'   , 90     union select 'acme inc'             , '201512'   , 40;  declare @endperiod nvarchar(10) = '201612';  e1(n) (select 1 (values (1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) dt(n)), e2(n) (select 1 e1 a, e1 b),  e4(n) (select 1 e2 a, e2 b),  tally(n)     -- create tally table. tally table return maximum of 1000 rows. (     select  (row_number() on (order (select null)) - 1) e4 ) , possibleperiods    --determine possible monthly intervals year. implementation assumes maximum fill of 5 years (60 months). (     select   account             ,left(convert(nvarchar(10), dateadd(month, [t].n, [a].startyear), 112), 6) period        (                 select   account                         ,convert(datetime, left(min(period), 4), 112) startyear                    accounts                 group account             ) [a]     cross join              (                 select top 60 n tally  -- modify top statement control number of months fill (up 1000 months)             ) t ) , newperiods -- determine new periods add. (     select   [p].account             ,[p].period             ,[a].amount        possibleperiods [p]     left outer join accounts [a]  on [a].account = p.account , a.period = [p].period       [p].period <= @endperiod ) insert accounts (      account     ,period      ,amount  ) select  account         ,period          ,0      newperiods   amount null   --  select out result. select      *        accounts order    account, period  drop table accounts 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -