# Orphans included, Rejects not - since they can be below diff, or invalid, or too late or ...
valblk="expirydate>'5555-05-05' and confirmed!='R'"
#
# START
if["$1"="0"];then
shift
selwm1="ms.markerid>=(select min(markerid) from workmarkers where workinfoidstart>=(select min(workinfoid) from shares))"
selsh1=""
selblk1="and b.workinfoid>=(select min(workinfoid) from shares)"
else
if["$1"="s"];then
shift
selwm1="ms.markerid>=(select min(markerid) from workmarkers where description like 'Shift fin: $1 %')"
selsh1="and workinfoid>=(select min(workinfoidstart) from workmarkers where description like 'Shift fin: $1 %')"
selblk1="$selsh1"
shift
else
if["$1"="b"];then
shift
selwm1="ms.markerid>=(select min(markerid) from workmarkers where workinfoidstart>(select min(workinfoid) from blocks where height>=$1))"
selsh1="and s.workinfoid>=(select min(workinfoid) from blocks where height>=$1)"
selblk1="and b.height>$1"
shift
else
echo"ERR: Unknown first option '$1'"
usAge
fi
fi
fi
#
# END
if["$1"="z"];then
selwm2=""
selsh2=""
selblk2=""
else
if[ -z "$1"];then
echo"ERR: missing ending block|z"
usAge
else
selwm2="and wm.markerid<=(select max(markerid) from workmarkers where workinfoidend<=(select max(workinfoid) from blocks where height<=$1))"
selsh2="and s.workinfoid<=(select max(workinfoid) from blocks where height<=$1)"
selblk2="and b.height<=$1"
fi
fi
#
# check all shares have minsdiff > $msd
sql="select count(*) from shares s where s.minsdiff>$msd$selsh1$selsh2 ;"
#
dosql > "$tmp0"
count="`head -n 3 "$tmp0" | tail -n 1`"
if["$count" !="0"];then
count
echo"ERROR: there were '$count' shares with minsdiff>$msd - that must be '0'"
usAge
fi
#
# summarise markersummary into tmp1 for any user or worker with 'msd' diffacc or more
if[ -z "$work"];then
sql="select ms.userid,substring(max(ms.workername) from '(^[^\._]*)'),to_char(sum(ms.diffacc),'999G999G999G999G999.99'),to_char(sum(ms.sharecount),'999G999G999G999G999') from markersummary ms where $selwm1$selwm2 group by ms.userid having sum(ms.diffacc)>$msd order by sum(ms.diffacc) desc;"
else
sql="select ms.workername,to_char(sum(ms.diffacc),'999G999G999G999G999.99'),to_char(sum(ms.sharecount),'999G999G999G999G999.99') from markersummary ms where $selwm1$selwm2 group by ms.workername having sum(ms.diffacc)>$msd order by sum(ms.diffacc) desc;"
fi
#
dosql > "$tmp1"
#
# summarise the shares into tmp2
if[ -z "$work"];then
sql="select s.userid,count(*),to_char(min(s.sdiff),'999G999G999G999G999.99'),to_char(max(s.sdiff),'999G999G999G999G999.99'),to_char(avg(s.sdiff),'999G999G999G999G999.99') from shares s where $valsh$selsh1$selsh2 group by s.userid;"
else
sql="select s.workername,count(*),to_char(min(s.sdiff),'999G999G999G999G999.99'),to_char(max(s.sdiff),'999G999G999G999G999.99'),to_char(avg(s.sdiff),'999G999G999G999G999.99') from shares s where $valsh$selsh1$selsh2 group by s.workername;"
fi
#
dosql > "$tmp2"
#
# summarise the blocks into tmp3
if[ -z "$work"];then
sql="select b.userid,count(*)||' B' from blocks b where $valblk$selblk1$selblk2 group by b.userid;"
else
sql="select b.workername,count(*)||' B' from blocks b where $valblk$selblk1$selblk2 group by b.workername;"
fi
#
dosql > "$tmp3"
#
# summarise diff% into tmp4 (all ids will have a value)
valwm="wm.status='p' and wm.expirydate>'5555-05-05'"
sql="$sql0 from markersummary ms join workmarkers wm on ms.markerid=wm.markerid join workinfo wi on wm.workinfoidend=wi.workinfoid where $valwm and $selwm1$selwm2$sqlz"