Aggregate.php
3 years ago
CRUD.php
3 years ago
FromClause.php
4 years ago
GroupByStatement.php
4 years ago
HavingClause.php
3 years ago
JoinClause.php
4 years ago
LimitStatement.php
4 years ago
MetaQuery.php
3 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
94 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 | if (empty($this->selects)) { |
| 25 | $this->selects[] = new RawSQL('SELECT COUNT(%1s) AS count', $column); |
| 26 | } else { |
| 27 | $this->selects[] = new RawSQL('COUNT(%1s) AS count', $column); |
| 28 | } |
| 29 | |
| 30 | return +$this->get()->count; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Returns the total sum in a set of values |
| 35 | * |
| 36 | * @since 2.19.0 |
| 37 | * @param string $column |
| 38 | * |
| 39 | * @return int|float |
| 40 | */ |
| 41 | public function sum($column) |
| 42 | { |
| 43 | $this->selects[] = new RawSQL('SELECT SUM(%1s) AS sum', $column); |
| 44 | |
| 45 | return +$this->get()->sum; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Get the average value in a set of values |
| 51 | * |
| 52 | * @since 2.19.0 |
| 53 | * @param string $column |
| 54 | * |
| 55 | * @return int|float |
| 56 | */ |
| 57 | public function avg($column) |
| 58 | { |
| 59 | $this->selects[] = new RawSQL('SELECT AVG(%1s) AS avg', $column); |
| 60 | |
| 61 | return +$this->get()->avg; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the minimum value in a set of values |
| 66 | * |
| 67 | * @since 2.19.0 |
| 68 | * @param string $column |
| 69 | * |
| 70 | * @return int|float |
| 71 | */ |
| 72 | public function min($column) |
| 73 | { |
| 74 | $this->selects[] = new RawSQL('SELECT MIN(%1s) AS min', $column); |
| 75 | |
| 76 | return +$this->get()->min; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns the maximum value in a set of values |
| 81 | * |
| 82 | * @since 2.19.0 |
| 83 | * @param string $column |
| 84 | * |
| 85 | * @return int|float |
| 86 | */ |
| 87 | public function max($column) |
| 88 | { |
| 89 | $this->selects[] = new RawSQL('SELECT MAX(%1s) AS max', $column); |
| 90 | |
| 91 | return +$this->get()->max; |
| 92 | } |
| 93 | } |
| 94 |