-
Notifications
You must be signed in to change notification settings - Fork 17
/
aggregates.go
51 lines (41 loc) · 1.45 KB
/
aggregates.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
47
48
49
50
51
package buildsqlx
// Count counts resulting rows based on clause
func (r *DB) Count() (cnt int64, err error) {
bldr := r.Builder
bldr.columns = []string{"COUNT(*)"}
query := bldr.buildSelect()
err = r.Sql().QueryRow(query, prepareValues(r.Builder.whereBindings)...).Scan(&cnt)
return
}
// Avg calculates average for specified column
func (r *DB) Avg(column string) (avg float64, err error) {
bldr := r.Builder
bldr.columns = []string{"AVG(" + column + ")"}
query := bldr.buildSelect()
err = r.Sql().QueryRow(query, prepareValues(r.Builder.whereBindings)...).Scan(&avg)
return
}
// Min calculates minimum for specified column
func (r *DB) Min(column string) (min float64, err error) {
bldr := r.Builder
bldr.columns = []string{"MIN(" + column + ")"}
query := bldr.buildSelect()
err = r.Sql().QueryRow(query, prepareValues(r.Builder.whereBindings)...).Scan(&min)
return
}
// Max calculates maximum for specified column
func (r *DB) Max(column string) (max float64, err error) {
bldr := r.Builder
bldr.columns = []string{"MAX(" + column + ")"}
query := bldr.buildSelect()
err = r.Sql().QueryRow(query, prepareValues(r.Builder.whereBindings)...).Scan(&max)
return
}
// Sum calculates sum for specified column
func (r *DB) Sum(column string) (sum float64, err error) {
bldr := r.Builder
bldr.columns = []string{"SUM(" + column + ")"}
query := bldr.buildSelect()
err = r.Sql().QueryRow(query, prepareValues(r.Builder.whereBindings)...).Scan(&sum)
return
}