Skip to content

Instantly share code, notes, and snippets.

@fullybaked
Created June 17, 2015 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fullybaked/22ecd9eef0777260d390 to your computer and use it in GitHub Desktop.
Save fullybaked/22ecd9eef0777260d390 to your computer and use it in GitHub Desktop.
SQL Snippet
select count(DISTINCT project_id), sum(amount), user_id
from payments
group by user_id
order by sum(amount) desc;
@davidyell
Copy link

$query = $this->Payments->find()
    ->select([
        'count' => 'COUNT(DISTINCT(project_id))',
        'sum' => 'SUM(amount)'
    ])
    ->group('user_id')
    ->order(['sum' => 'DESC']);

@davidyell
Copy link

$query = $this->Payments->find()
    ->select([
        'count' => $query->func()->count('DISTINCT project_id'),
        'sum' => $query->func()->sum('amount')
    ])
    ->group('user_id')
    ->order(['sum' => 'DESC']);

http://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment