How to translate a database query with a subquery to a db_select statement?
SelectQuery::where() takes a string (not an object).

You probably meant to use SelectQuery::condition():

$subquery-&gt;fields('a')
  -&gt;condition('language', $language)
  -&gt;orderBy('eid')
  -&gt;orderBy('vid', 'DESC');


Your execution is slightly off too; at the moment you're discarding the results of the execution, and instead invoking fetchAllAssoc() on the wrong type of object.

What you need is

$keywords = $query-&gt;fields('ev')
  -&gt;groupBy('keyword')
  -&gt;execute()
  -&gt;fetchAllAssoc('keyword', PDO::FETCH_ASSOC);


or

$result = $query-&gt;fields('ev')-&gt;groupBy('keyword')-&gt;execute();
$keywords = $result-&gt;fetchAllAssoc('keyword', PDO::FETCH_ASSOC);


Or some other derivative of the same logic.

After those changes you should wind up with

SELECT  ev.*
FROM 
(SELECT  a.*
FROM 
{custom_keywords_entity_values} a
WHERE  (language = 'en')
ORDER BY eid ASC, vid DESC) ev
GROUP BY keyword


Pretty close to your original, and functionality identical.