-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqb_agg.go
46 lines (41 loc) · 1.5 KB
/
qb_agg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package qb
// Count counts resulting rows based on clause
func (q *QbDB) Count() (countRows int64, err error) {
builder := q.Builder
builder.columns = []string{"COUNT(*)"}
query := builder.buildSelect()
err = q.Sql().QueryRow(query, prepareValues(q.Builder.whereBindings)...).Scan(&countRows)
return
}
// Avg calculates average for specified column
func (q *QbDB) Avg(column string) (avg float64, err error) {
builder := q.Builder
builder.columns = []string{"AVG(" + column + ")"}
query := builder.buildSelect()
err = q.Sql().QueryRow(query, prepareValues(q.Builder.whereBindings)...).Scan(&avg)
return
}
// Min calculates minimum for specified column
func (q *QbDB) Min(column string) (min float64, err error) {
builder := q.Builder
builder.columns = []string{"MIN(" + column + ")"}
query := builder.buildSelect()
err = q.Sql().QueryRow(query, prepareValues(q.Builder.whereBindings)...).Scan(&min)
return
}
// Max calculates maximum for specified column
func (q *QbDB) Max(column string) (max float64, err error) {
builder := q.Builder
builder.columns = []string{"MAX(" + column + ")"}
query := builder.buildSelect()
err = q.Sql().QueryRow(query, prepareValues(q.Builder.whereBindings)...).Scan(&max)
return
}
// Sum calculates sum for specified column
func (q *QbDB) Sum(column string) (max float64, err error) {
builder := q.Builder
builder.columns = []string{"SUM(" + column + ")"}
query := builder.buildSelect()
err = q.Sql().QueryRow(query, prepareValues(q.Builder.whereBindings)...).Scan(&max)
return
}