nguyendh
(Duynguyen0511)
#1
I have to do sum per column then add them all up. like this

```
$totalHours = Entry::find()->where(['type' => $type])->sum('st_hours') +
Entry::find()->where(['type' => $type])->sum('ot_hours') +
Entry::find()->where(['type' => $type])->sum('dt_hours') +
Entry::find()->where(['type' => $type])->sum('travel_time_hours');
```

Bad thing is I have to make 4 sql queries. Can it be done in 1 query ?

phtamas
(Phtamas)
#2
Active Record just complicates things when you only need a scalar value, try to use yii\db\Query instead:

```
$totalHours = (new \yii\db\Query())
->select('SUM(st_hours) + SUM(ot_hours) + SUM(dt_hours) + SUM(travel_time_hours)')
->from(Entry::tableName())
->where(['type' => $type])
->scalar();
```

EDIT:

On second thought, it might be possible with ActiveQuery (not tested):

```
Entry::find()
->where(['type' => $type])
->sum('st_hours + ot_hours + dt_hours + travel_time_hours');
```

nguyendh
(Duynguyen0511)
#3
both work. Thanks alot

I tweak the ActiveQuery abit by ‘quoting’ the column name as suggested (http://www.yiiframework.com/doc-2.0/yii-db-query.html#sum()-detail)

```
Entry::find()
->where(['type' => $type])
->sum('[[st_hours]] + [[ot_hours]] + [[dt_hours]] + [[travel_time_hours]]');
```