Aggregate.php
4 years ago
CRUD.php
4 years ago
FromClause.php
4 years ago
GroupByStatement.php
4 years ago
HavingClause.php
4 years ago
JoinClause.php
4 years ago
LimitStatement.php
4 years ago
MetaQuery.php
4 years ago
OffsetStatement.php
4 years ago
OrderByStatement.php
4 years ago
SelectStatement.php
4 years ago
TablePrefix.php
4 years ago
UnionOperator.php
4 years ago
WhereClause.php
3 years ago
Aggregate.php
90 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\QueryBuilder\Concerns; |
| 4 | |
| 5 | use Give\Framework\QueryBuilder\Clauses\RawSQL; |
| 6 | |
| 7 | /** |
| 8 | * @since 2.19.0 |
| 9 | */ |
| 10 | trait Aggregate |
| 11 | { |
| 12 | /** |
| 13 | * Returns the number of rows returned by a query |
| 14 | * |
| 15 | * @since 2.19.0 |
| 16 | * @param null|string $column |
| 17 | * |
| 18 | * @return int |
| 19 | */ |
| 20 | public function count($column = null) |
| 21 | { |
| 22 | $column = ( ! $column || $column === '*') ? '1' : trim($column); |
| 23 | |
| 24 | $this->selects[] = new RawSQL('SELECT COUNT(%1s) AS count', $column); |
| 25 | |
| 26 | return +$this->get()->count; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Returns the total sum in a set of values |
| 31 | * |
| 32 | * @since 2.19.0 |
| 33 | * @param string $column |
| 34 | * |
| 35 | * @return int|float |
| 36 | */ |
| 37 | public function sum($column) |
| 38 | { |
| 39 | $this->selects[] = new RawSQL('SELECT SUM(%1s) AS sum', $column); |
| 40 | |
| 41 | return +$this->get()->sum; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Get the average value in a set of values |
| 47 | * |
| 48 | * @since 2.19.0 |
| 49 | * @param string $column |
| 50 | * |
| 51 | * @return int|float |
| 52 | */ |
| 53 | public function avg($column) |
| 54 | { |
| 55 | $this->selects[] = new RawSQL('SELECT AVG(%1s) AS avg', $column); |
| 56 | |
| 57 | return +$this->get()->avg; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns the minimum value in a set of values |
| 62 | * |
| 63 | * @since 2.19.0 |
| 64 | * @param string $column |
| 65 | * |
| 66 | * @return int|float |
| 67 | */ |
| 68 | public function min($column) |
| 69 | { |
| 70 | $this->selects[] = new RawSQL('SELECT MIN(%1s) AS min', $column); |
| 71 | |
| 72 | return +$this->get()->min; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the maximum value in a set of values |
| 77 | * |
| 78 | * @since 2.19.0 |
| 79 | * @param string $column |
| 80 | * |
| 81 | * @return int|float |
| 82 | */ |
| 83 | public function max($column) |
| 84 | { |
| 85 | $this->selects[] = new RawSQL('SELECT MAX(%1s) AS max', $column); |
| 86 | |
| 87 | return +$this->get()->max; |
| 88 | } |
| 89 | } |
| 90 |