(: file: freq_1var_stgPop_by_year.xq date: 23-August-2010 author: Gary Lewis purpose: Frequency distribution by year for a specified staging variable in stgPop.xml. usage: Run from path where program is located. example: zorba -e var:="control" -f -q freq_1var_stgPop_by_year.xq -o ../xml/staging/freq_control_stgPop_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/stgPop.xml")/ipeds/survey let $year := $i/@year, $datatype := doc("../xml/metadata/ipedsVars_metadata.xml")/variables/var[@name = $var]/detail[@year = $year]/@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/metadata/ipedsVars_metadata_codes.xml")/variables/var[@name = $var][@year = $year]/code[@value = $value]/@label return } }