php - How to sum value column in query builder phalcon
I want to sum values in query builder in framework phalcon
, this is my code builder
$builder = new Builder();
$builder
->columns([
"TABLE1.ID_TABLE1",
"count(distinct TABLE2.SKPD_SUB1_ID) as RESULT_1",
"count(distinct TABLE3.SKPD_SUB1_ID) as RESULT_2",
"count(distinct TABLE_4.SKPD_SUB1_ID) as RESULT_3",
//in there i want to add RESULT_1 + RESUL_2 + RESULT_3
])
->from("TABLE1")
->leftjoin("TABLE2", "TABLE2.SKPD_SUB1_ID=TABLE1.SKPD_SUB1_ID")
->leftjoin("TABLE3", "TABLE3.SKPD_SUB1_ID=TABLE1.SKPD_SUB1_ID")
->leftjoin("TABLE_4", "TABLE_4.SKPD_SUB1_ID=TABLE1.SKPD_SUB1_ID")
->where("TABLE1.SKPD_ID='$skpd_id'");
$result = $builder - > getQuery() - > execute();
Answer
Solution:
You can try something like this :
select sum(a.cnt)
from (
select count(...) as cnt
from ...
group by ...
) as a
Source