derived table and row number addition
This commit is contained in:
37
pgm_field.go
37
pgm_field.go
@@ -84,6 +84,43 @@ func (f Field) Desc() Field {
|
||||
return Field(f.String() + " DESC")
|
||||
}
|
||||
|
||||
func (f Field) RowNumber(as string) Field {
|
||||
return rowNumber(&f, nil, true, as)
|
||||
}
|
||||
|
||||
func (f Field) RowNumberDesc(as string) Field {
|
||||
return rowNumber(&f, nil, true, as)
|
||||
}
|
||||
|
||||
// RowNumberPartionBy in ascending order
|
||||
func (f Field) RowNumberPartionBy(partition Field, as string) Field {
|
||||
return rowNumber(&f, &partition, true, as)
|
||||
}
|
||||
|
||||
func (f Field) RowNumberDescPartionBy(partition Field, as string) Field {
|
||||
return rowNumber(&f, &partition, false, as)
|
||||
}
|
||||
|
||||
func rowNumber(f, partition *Field, isAsc bool, as string) Field {
|
||||
var orderBy string
|
||||
if isAsc {
|
||||
orderBy = " ASC"
|
||||
} else {
|
||||
orderBy = " DESC"
|
||||
}
|
||||
|
||||
if as == "" {
|
||||
as = "row_number"
|
||||
}
|
||||
|
||||
col := f.String()
|
||||
if partition != nil {
|
||||
return Field("ROW_NUMBER() OVER (PARTITION BY " + partition.String() + " ORDER BY " + col + orderBy + ") AS " + as)
|
||||
}
|
||||
|
||||
return Field("ROW_NUMBER() OVER (ORDER BY " + col + orderBy + ") AS " + as)
|
||||
}
|
||||
|
||||
func (f Field) IsNull() Conditioner {
|
||||
col := f.String()
|
||||
return &Cond{Field: col, op: " IS NULL", len: len(col) + 8}
|
||||
|
||||
Reference in New Issue
Block a user