Stringsql="update Balance b, (select sum(DebitBal) as sDebitBal, sum(CreditBal) as sCreditBal ,acctcode, 1 as PeriodId from Balance where PeriodId = 1 "+
" group by acctcode "+
" union all "+
" select sum(DebitBal) as sDebitBal, sum(CreditBal) as sCreditBal ,acctcode, 2 as PeriodId from Balance where PeriodId <= 2 "+
" group by acctcode "+
" union all "+
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 3 as PeriodId from Balance where PeriodId <= 3 "+
" group by acctcode "+
" union all "+
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 4 as PeriodId from Balance where PeriodId <= 4 "+
" group by acctcode "+
" union all "+
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 5 as PeriodId from Balance where PeriodId <= 5 "+
" group by acctcode "+
" union all "+
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 6 as PeriodId from Balance where PeriodId <= 6 "+
" group by acctcode "+
" union all "+
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 7 as PeriodId from Balance where PeriodId <= 7 "+
" group by acctcode "+
" union all "+
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 8 as PeriodId from Balance where PeriodId <= 8 "+
" group by acctcode "+
" union all "+
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 9 as PeriodId from Balance where PeriodId <= 9 "+
" group by acctcode "+
" union all "+
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 10 as PeriodId from Balance where PeriodId <= 10 "+
" group by acctcode "+
" union all "+
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 11 as PeriodId from Balance where PeriodId <= 11 "+
" group by acctcode "+
" union all "+
" select sum(DebitBal) as sDebitBal,sum(CreditBal) as sCreditBal ,acctcode, 12 as PeriodId from Balance where PeriodId <=12 "+
" group by acctcode "+
" ) as tmp "+
" set YearDebitBal = sDebitBal, YearCreditBal = sCreditBal where b.acctcode = tmp.acctcode and b.PeriodId = tmp.PeriodId ";