Obtaining SUM('field') in PHP code
Use QueryAggregateInterface::execute:

$result = \Drupal::entityQueryAggregate('node')
  -&gt;aggregate('nid', 'sum')
  -&gt;condition('status', NodeInterface::PUBLISHED)
  -&gt;execute();


The result returned is keyed by fieldname and aggregate function:

$sum = $result['0']['nid_sum'];