I need to be able to calculate the median of numeric fields. I know that we aren't able to do it natively so I'm hoping that someone from the community has had to do this before and can help. There'...
Here is a JavaScript function getMedian that takes (1) a table dbid, (2) a query string, and (3) a fid of a numeric field and returns a promise that resolves to the median:
function getMedian(dbid, query, fid) { return Promise.resolve( $.get(dbid, { act: "API_DoQuery", query, clist: fid, fmt: "structured" }) ).then(function(xml) { var data = $('qdbapi table records record f[id=${fid}]', xml).map(function() { return parseFloat($(this).text()); }); data.sort((x, y) => x - y); var ilm = Math.floor((data.length - 1) / 2); var ihm = Math.ceil((data.length - 1) / 2); var median = (data[ilm] + data[ihm]) / 2; return median; }) }
You call it like this:
var dbid = "bnxssrg9s"; var dbidTable = "bnzd357bm"; var apptoken = "cnurzq5cgchgjgdsdiup6cgkq6ct"; $.ajaxSetup({data: {apptoken}}); var query = "{3.GT.0}"; var fid = "8"; getMedian(dbidTable, query, fid) .then(function(median) { console.log(median); });
When called on a sample application with this numeric data:
Hey Dan! One more question for you, first your code RAN BEAUTIFULLY, but question how do I pass this value created by the javascript to a numeric field so that it can be reported on.