Skip to content

Latest commit

 

History

History
119 lines (90 loc) · 1.91 KB

File metadata and controls

119 lines (90 loc) · 1.91 KB

Aggregate Helper Functions

Use aggregate functions for calculations and statistics.

COUNT

Count All Rows

use tommyknocker\pdodb\helpers\Db;

$total = $db->find()
    ->from('users')
    ->select(Db::count())
    ->getValue();

Count Specific Column

$count = $db->find()
    ->from('users')
    ->select(Db::count('id'))
    ->getValue();

Count Distinct

$unique = $db->find()
    ->from('users')
    ->select(Db::count('DISTINCT country'))
    ->getValue();

SUM

Sum Values

$total = $db->find()
    ->from('orders')
    ->select(Db::sum('amount'))
    ->where('status', 'completed')
    ->getValue();

AVG

Average Values

$average = $db->find()
    ->from('products')
    ->select(Db::avg('price'))
    ->getValue();

MIN/MAX

Minimum Value

$minPrice = $db->find()
    ->from('products')
    ->select(Db::min('price'))
    ->getValue();

Maximum Value

$maxPrice = $db->find()
    ->from('products')
    ->select(Db::max('price'))
    ->getValue();

With GROUP BY

Grouped Aggregations

$stats = $db->find()
    ->from('orders')
    ->select([
        'user_id',
        'total_orders' => Db::count(),
        'total_amount' => Db::sum('amount'),
        'avg_amount' => Db::avg('amount')
    ])
    ->groupBy('user_id')
    ->get();

Common Patterns

Sales Statistics

$stats = $db->find()
    ->from('sales')
    ->select([
        'total_sales' => Db::sum('amount'),
        'total_orders' => Db::count(),
        'avg_order' => Db::avg('amount'),
        'min_order' => Db::min('amount'),
        'max_order' => Db::max('amount')
    ])
    ->where('status', 'completed')
    ->getOne();

Next Steps