sql server - Insert into based on a select query with joins issue -
based on other table called tablecodes have 3 records
code 1508 1509 1510
and have result query temporal table....
capacitydate installercode workareadid foxtelcodeid value 2/8/12 baw 7 1508 3 3/8/12 baw 7 1508 1 4/8/12 baw 7 1509 1
i need insert in same table other records missing based on tablecodes value = 0
i want
capacitydate installercode workareadid foxtelcodeid value 2/8/12 baw 7 1508 3 2/8/12 baw 7 1509 0 2/8/12 baw 7 1510 0 3/8/12 baw 7 1508 1 3/8/12 baw 7 1509 0 3/8/12 baw 7 1510 0 4/8/12 baw 7 1508 1 4/8/12 baw 7 1509 0 4/8/12 baw 7 1510 0
i'm using following code
insert #tmpadjustmenttable ( capacitydate, installerid, installercode, installername, bthisisanadjustment, sstatusofcapacity, workareadid, workarea, capacitystate, foxtelcodeid, sinternallabelforcapacity, slabelforcapacity, capacityunits, capacity, maxjobs, commentsforday ) select ta.capacitydate, ta.installerid, ta.installercode, ta.installername, 1, ta.sstatusofcapacity, ta.workareadid, ta.workarea, ta.capacitystate, foxtelcodes.sysid, foxtelcodes.sinternalshortlabelforcapacity, foxtelcodes.slabelforcapacity, 0, 0, 0, ta.commentsforday #tmpadjustmenttable ta inner join foxtelcodes on foxtelcodes.sysid <> ta.foxtelcodeid , foxtelcodes.bcollectthisforcapacity = 1
but got ...
capacitydate installercode workareadid foxtelcodeid value 2/8/12 baw 7 1508 3 2/8/12 baw 7 1509 0 2/8/12 baw 7 1510 0 3/8/12 baw 7 1508 1 3/8/12 baw 7 1508 0 3/8/12 baw 7 1509 0 3/8/12 baw 7 1509 0 3/8/12 baw 7 1510 0 3/8/12 baw 7 1510 0 4/8/12 baw 7 1508 1 4/8/12 baw 7 1508 0 4/8/12 baw 7 1509 0 4/8/12 baw 7 1509 0 4/8/12 baw 7 1510 0 4/8/12 baw 7 1510 0
hi check query below output in requested format.
--here tried getting output without using while loop not sucees.i try mean while can check , hope useful.
declare @min int,@max int select @min=min(idn),@max=max(idn) results select @min,@max declare @tbl_out table (capacitydate datetime,installercode varchar(100),workareadid int,foxtelcodeid int,value int) while(@min<=@max) begin ;with cte(capacitydate,installercode,workareadid,foxtelcodeid,value) (select capacitydate,installercode,workareadid,foxtelcodeid,value results idn = @min) ,cte1(code,value) (select c.code,isnull(t.value,0) codes c left outer join cte t on c.code=t.foxtelcodeid) insert @tbl_out select r.capacitydate,r.installercode,r.workareadid,c.code,c.value cte1 c,cte r set @min+=1 end select * @tbl_out select * results
Comments
Post a Comment