This is an old revision of the document!
with ff as (select folderid,count(*) as cnt, sum(length(msgdata)) as totsize, sum(case when newflag then 1 else 0 end) as newcnt, sum(case when read and not hold then 0 else 1 end) as unreadcnt, sum(case when hold then 1 else 0 end) as holdcnt from messages.foldermessages inner join messages.msgdata using(msgdataid) where deletedtime is null group by folderid) update messages.folders f set msgcount=ff.cnt, totalsize=totsize, newcount=newcnt, unreadcount=unreadcnt, holdcount=holdcnt from ff where f.folderid=ff.folderid;