sql - Insert data into a column based on a variable -
this may sound little tricky. have table has columns jan2012 - march2015. each month has it's own column.
i creating query loop calculates total number of visits trailing 12 months per doctor. similar more involved.
declare @startdate date = '20110101' declare @enddate date = dateadd(mm, 12, @startdate) declare @column varchar(15) = datename(mm, @enddate) + datename(yy, @enddate) create table #final ( doctorid int january2012 int february2012 int march2012 int .... ) select doctorid count(invoiceid) on (partition hospitalid, doctorid) countofvisits invoice now want insert count of visits proper column based on end date. query loop through each month time period.
thanks,
you need specify column inserting prior executing query, means have sadly entered land of dynamic sql. dynamic sql works setting variable equal text string, , using exec command on variable. it's not rocket science, might bit more of explanation comfortably fits in stack overflow answer. fortunately pinal dave explained already, , better can:
the answer looking half way down page, i'd suggest reading whole thing it's informative.
as fair warning, dynamic sql sucks on performance, when combined loop, looking @ dbas concerned.
Comments
Post a Comment