1

The semiMonthBegin(X, [dayOfMonth=15], [offset], [n=1]) function works fine with the default dayOfMonth=15 day cycle. However, if I want to calculate on ten-day basis and set dayOfMonth=10, the result is wrong.

Is there any way to fix it?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Polly
  • 603
  • 3
  • 13

1 Answers1

1

semiMonthBegin is not suitable for ten-day-based calculation. You can use a user-defined function with a group by clause. For example:

def f(x) {
    Count = x.size()
    res = array(STRING, Count)
    for (i in 0:Count){
        if(int(x[i].format("dd"))<=10){
            res[i] = x[i].format("yyyy.MM.")+"10"
        }
        else if(int(x[i].format("dd")) <=20){
            res[i] = monthEnd(x[i]).format("yyy.MM.dd")
        }
    return res
}
t1 = select *, date(f(date)) as xun from t1
select avg(price), sum(qty) from t1 group by xun;

You can also use dayOfMonth function to calculate by any day.

select *, date-(dayofMonth(date)-1)%10 as xun from t1
Polly
  • 603
  • 3
  • 13