Move SQL query calculation into separate general function