(: file: freq_1var_stg2whPop_by_year.xq date: 24-August-2010 author: Gary Lewis purpose: Frequency distribution by year for a specified staging variable in stg2whPop.xml. usage: Run from path where program is located. example: zorba -e var:="rControl" -f -q freq_1var_stg2whPop_by_year.xq -o ../xml/staging/freq_rControl_stg2whPop_by_year.xml -z indent=yes revision history: date: 30-August-2010 author: Gary Lewis revised: 1. Added logic to sort code values by numeric order if the datatype of the variable is N (numeric). :) declare variable $var external; { for $i in fn:doc("../xml/staging/stg2whPop.xml")/ipeds/survey let $year := $i/@year, $datatype := doc("../xml/staging/rVariables.xml")/variables/var[@name = $var]/@dataType order by $year descending return { let $tmp1 := for $j in $i/inst let $unitid := $j/@unitid, $value := using $j, $var eval{concat("$j/@", $var)} group by $value order by (if ($datatype eq "N") then xs:integer(if (string($value) eq "") then "-99" else $value) else $value) return return for $k in $tmp1 let $value := $k/@value, $count := $k/@count, $label := doc("../xml/staging/rVariables.xml")/variables/var[@name = $var]/code[@value = $value]/@label return } }