Select DISTINCT on a particular field I want to return distinct values based on a particular column - 'company_code'. How do I achieve this? My code is as below.

$query = \Drupal::database()-&gt;select('actuarial_bases', 'bases')
                    -&gt;fields('bases', ['company_code','basis_id','mortality','commission','initial_expenses','recurring_expenses','date_saved','discount_rate'])
                    -&gt;condition('approved_by',!"")
                    -&gt;orderBy('date_saved', 'DESC')
                    -&gt;distinct();
My online research led me to this page. 

I created a view to include 'row_count' using the following query:

SELECT
    `company_code`,
    `basis_id`,
    `mortality`,
    `commission`,
    `initial_expenses`,
    `recurring_expenses`,
    `discount_rate`,
    `date_saved`,
    (
    SELECT
        COUNT(0)
    FROM
        `actuarial_bases` `b`
    WHERE
        (
            (`a`.`date_saved` &lt;= `b`.`date_saved`) AND(
                `a`.`company_code` = `b`.`company_code`
            )
        )
) AS `row_number`
FROM
    `actuarial_bases` `a`


then modified my query to:

$query = \Drupal::database()-&gt;select('bases_in_force', 'b')
         -&gt;fields('b', ['company_code','basis_id','mortality','commission','initial_expenses','recurring_expenses','date_saved','discount_rate'])
                    -&gt;condition('row_number',1)
                    -&gt;orderBy('date_saved', 'DESC')
                    ;
$results = $query-&gt;execute();