Add second expression to query I have a probably trivial question, but I could not find an answer.
I have this query, which works fine:

$query = db_select('books, 't');
$query
  -&gt;fields('t', ['publication_year'])
  -&gt;groupBy('t.publication_year')
  -&gt;orderBy('publication_year', 'ASC')
  -&gt;addExpression('COUNT(t.publication_year)', 'n');
$data = $query-&gt;execute();
$result = $data-&gt;fetchAll();


I want to add a second expression:

(SELECT COUNT(*) FROM books WHERE open_access=1) AS n_oa


But I have two problems:
When adding a second expression I get the error Method 'addExpression' not found in null|string regardless of the content.

When only adding the single expression -&gt;addExpression('SELECT COUNT(*) from books WHERE open_access=1', 'n_oa'); I get a syntax error.

Edit: 

I tried this, and it generally works except, that the seond expression can't find the 'year' alias. 

 $query
    -&gt;addField('t', 'publication_year', 'year');
  $query
    -&gt;condition('wg', 'ag_zzexternal', '=')
    -&gt;groupBy('t.publication_year')
    -&gt;orderBy('publication_year', 'ASC')
    -&gt;addExpression('COUNT(t.publication_year)', 'n');
  $query
    -&gt;addExpression('SUM(IF(open_access=1,1,0) AND IF(publication_year=year,1,0))', 'n_oa');
}
I think the easiest solution will be to use db_query, where you can write raw SQL, like this:

$query = db_query('SELECT publication_year FROM books etc.');