Calculation of median

I calculate AVG value using query below:
select AVG(recordedPerUser) from (
select count(*) as recordedPerUser from accountMgmt as act
inner join accountMgmt as ci
on META(act).id = ci.accountToken
where
act.type=“account” and
act.status != ‘ACTIVE’ and
ci.type=“catalogItem” and
ci.status=“RECORDED”
group by
ci.accountToken)
as t1;

How can I calculate MEDIAN value? I know that in CB 6.5 median function was introduced, however we have CB 5.5
If it is possible can it be done with the single query that calculates both AVG and median

If your groups are reasonable number.

SELECT ARRAY_AVG(a1) AS avg,
       (CASE WHEN ARRAY_LENGTH(a1) <= 0 TEHN NULL 
             WHEN (MOD(ARRAY_LENGTH(a1),2) == 0)
             TEHN ARRAY_AVG([a1[FLOOR(ARRAY_LENGTH(a1)/2)-1, FLOOR(ARRAY_LENGTH(a1)/2)])
             ELSE a1[FLOOR(ARRAY_LENGTH(a1)/2)]
        END) AS median
LET a1 = ARRAY_SORT((SELECT RAW count(1)
          FROM accountMgmt AS act
          INNER JOIN accountMgmt AS ci
               ON META(act).id = ci.accountToken
          WHERE act.type="account" AND act.status != "ACTIVE" AND ci.type="catalogItem" AND ci.status="RECORDED"
          GROUP BY ci.accountToken));