Fixed bug with sql query when demand progs