Skip to content

Instantly share code, notes, and snippets.

@vianhanif
Created September 29, 2019 14:32
Show Gist options
  • Save vianhanif/a205b574a3162e94388977975b4cce3a to your computer and use it in GitHub Desktop.
Save vianhanif/a205b574a3162e94388977975b4cce3a to your computer and use it in GitHub Desktop.
sample
// GroupByUserAndType : grouping cashback by userId and type
func (service *App) GroupByUserAndType(ctx context.Context, params ...QueryFilter) ([]*SumCashback, error) {
queryable, ok := data.QueryableFromContext(ctx)
if !ok {
queryable = service.queryable
}
where, args := BuildFilter(params...)
statement := fmt.Sprintf(`
SELECT "userId", SUM(amount) AS "amount", "type"
FROM "%s" %s GROUP BY "userId", "type";
`, TableName, where)
rows, errSelect := queryable.Query(statement, args...)
if errSelect != nil {
return nil, errSelect
}
defer rows.Close()
var records []*SumCashback
for rows.Next() {
sumCashback := &SumCashback{}
err := rows.Scan(&sumCashback.UserID, &sumCashback.Amount, &sumCashback.Type)
if err != nil {
return nil, err
}
records = append(records, sumCashback)
}
return records, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment