small changes to query
calculate medians