SQL 根据 Oracle Apex 中当月的两周

SQL according to the fortnight of the current month in Oracle Apex

我的 table 名字是 SESSIONS。我的 table 字段是:SESSION_DATE(类型:日期),SESSIONS.PRICE(类型:数字),STATUS(类型:数字)。

我希望我的查询根据当天显示不同的结果。如果当天是该月的前两周,则显示与该月的后两周不同的结果。

这是我的查询:

select to_char(SESSION_DATE, 'YYYYMM') as CHARGE_MONTH,
  to_char(SESSION_DATE, 'Month YY') as CHARGE,
    sum(SESSIONS.PRICE) as PRICE
 from SESSIONS SESSIONS
 where SESSIONS.STATUS in (4,5)
 and SESSIONS.SESSION_DATE between add_months(trunc(CURRENT_TIMESTAMP,'mm'),-13) and last_day(add_months(trunc(CURRENT_TIMESTAMP,'mm'),-1))
 group by to_char(SESSION_DATE, 'YYYYMM'), to_char(SESSION_DATE, 'Month YY')
 order by to_char(SESSION_DATE, 'YYYYMM') Desc

如果我们在当月的前两周,那么

SESSIONS.SESSION_DATE between add_months(trunc(CURRENT_TIMESTAMP,'mm'),-13) and last_day(add_months(trunc(CURRENT_TIMESTAMP,'mm'),-1))

其他

SESSIONS.SESSION_DATE between add_months(trunc(CURRENT_TIMESTAMP,'mm'),-13) and last_day(add_months(trunc(CURRENT_TIMESTAMP,'mm'),0))

我试过类似的东西:

select to_char(SESSION_DATE, 'YYYYMM') as CHARGE_MONTH,
  to_char(SESSION_DATE, 'Month YY') as CHARGE,
    sum(SESSIONS.PRICE) as PRICE
 from SESSIONS SESSIONS
 where SESSIONS.STATUS in (4,5)
 and SESSION_DATE = case when to_char(current_timestamp, 'dd') < 15 then
between add_months(trunc(CURRENT_TIMESTAMP,'mm'),-13) and last_day(add_months(trunc(CURRENT_TIMESTAMP,'mm'),-1))
else
between add_months(trunc(CURRENT_TIMESTAMP,'mm'),-13) and last_day(add_months(trunc(CURRENT_TIMESTAMP,'mm'),0)) 
end
 group by to_char(SESSION_DATE, 'YYYYMM'), to_char(SESSION_DATE, 'Month YY')
 order by to_char(SESSION_DATE, 'YYYYMM') Desc;

但它不起作用:(ORA-00905: 缺少关键字)

我也试过这个,但还是不行:

select to_char(SESSION_DATE, 'YYYYMM') as CHARGE_MONTH,
  to_char(SESSION_DATE, 'Month YY') as CHARGE,
    sum(SESSIONS.PRICE) as PRICE
 from SESSIONS SESSIONS
 where SESSIONS.STATUS in (4,5)
 and
    case when extract(day from current_timestamp) from dual < 15 then 
    (SESSION_DATE between add_months(trunc(CURRENT_TIMESTAMP,'mm'),-13) 
                  and last_day(add_months(trunc(CURRENT_TIMESTAMP,'mm'),-1)))
    else 
    (SESSION_DATE between add_months(trunc(CURRENT_TIMESTAMP,'mm'),-13) 
                  and last_day(add_months(trunc(CURRENT_TIMESTAMP,'mm'),0))) 
    end
 group by to_char(SESSION_DATE, 'YYYYMM'), to_char(SESSION_DATE, 'Month YY')
 order by to_char(SESSION_DATE, 'YYYYMM') Desc;

我收到这个错误:

ORA-20999: Failed to parse SQL query! ORA-06550: line 8, column 12: ORA-00907: missing right parenthesis

您不需要在 where 子句中使用 case 表达式。您可以只使用正则布尔表达式:

select to_char(SESSION_DATE, 'YYYYMM') as CHARGE_MONTH,
       to_char(SESSION_DATE, 'Month YY') as CHARGE,
       sum(SESSIONS.PRICE) as PRICE
from SESSIONS SESSIONS
where SESSIONS.STATUS in (4, 5) and
      ( (extract(day from SESSIONS.SESSION_DATE) <= 14 and
         SESSIONS.SESSION_DATE between add_months(trunc(CURRENT_TIMESTAMP, 'mm'), -13) and last_day(add_months(trunc(CURRENT_TIMESTAMP, 'mm'), -1))
        ) or
        (extract(day from SESSIONS.SESSION_DATE) > 14) and
         SESSIONS.SESSION_DATE between add_months(trunc(CURRENT_TIMESTAMP, 'mm'), -13) and last_day(add_months(trunc(CURRENT_TIMESTAMP, 'mm'), 0))
        )
      )
group by to_char(SESSION_DATE, 'YYYYMM'), to_char(SESSION_DATE, 'Month YY')
order by to_char(SESSION_DATE, 'YYYYMM') Desc;