derived table and row number addition

This commit is contained in:
2025-10-21 00:27:00 +05:30
parent 12d6fface6
commit 9837fb1e37
6 changed files with 112 additions and 7 deletions

View File

@@ -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}