$levelsql =$this->db->query("SELECT pro_id,pro_xp,pro_label
FROM
(
SELECT *,
(
SELECT SUM(pro_xp)
FROM tbl_program
WHERE pro_xp <= t.pro_xp
) total
FROM tbl_program t
) q
WHERE total <= $ProfileXp
ORDER BY pro_xp desc");
I wrote this query using CI function but want it to be written in Active records way.