sql server - Looping in Sql to insert data in to table -
i have sql statement:
select tms.team, count(tms.msapprovednpc) julynpcresults tmsorder2 tms msapprovednpc not in ('n/a','no') , month(month) = 11 , year(month) = '2013' group tms.team
these results specific month of july 2013.
now want load results in table nprdata other months in 1 table has columns
team, julynprresults, augnprresults, sepnprresults, octnprresults
how can loop above query insert data other months?
thanks in advance
i advise against storing data in denormalized format. better have table like:
nprdata (team,yearmonth,results)
but can without looping via conditional sum()
or count()
:
select tms.team ,sum(case when month(month)=11 , year(month) ='2013' 1 end) novnprresults ,sum(case when month(month)=12 , year(month) ='2013' 1 end) decnprresults tmsorder2 tms msapprovednpc not in ('n/a','no') group tms.team
Comments
Post a Comment