如何解决Oracle Apex中根据当月两周的SQL
我的表名是 SESSIONS。我的这个表的字段是:SESSION_DATE(类型:日期)、SESSIONS.PRICE(类型:数字)、状态(类型:数字)。
我希望我的查询根据当天显示不同的结果。如果当前日期在该月的第一个两周,则显示与该月的第二个两周不同的结果。
这是我的查询:
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,-1))
group by to_char(SESSION_DATE,'YYYYMM'),'Month YY')
order by to_char(SESSION_DATE,'YYYYMM') Desc
如果我们在当月的前两周,那么
SESSIONS.SESSION_DATE between add_months(trunc(CURRENT_TIMESTAMP,-1))
其他
SESSIONS.SESSION_DATE between add_months(trunc(CURRENT_TIMESTAMP,0))
我尝试过类似的事情:
select to_char(SESSION_DATE,5)
and SESSION_DATE = case when to_char(current_timestamp,'dd') < 15 then
between add_months(trunc(CURRENT_TIMESTAMP,-1))
else
between add_months(trunc(CURRENT_TIMESTAMP,0))
end
group by to_char(SESSION_DATE,'YYYYMM') Desc;
但是没有用:(ORA-00905: 缺少关键字)
我也试过这个,但仍然没有:
select to_char(SESSION_DATE,5)
and
case when extract(day from current_timestamp) from dual < 15 then
(SESSION_DATE between add_months(trunc(CURRENT_TIMESTAMP,-13)
and last_day(add_months(trunc(CURRENT_TIMESTAMP,-1)))
else
(SESSION_DATE between add_months(trunc(CURRENT_TIMESTAMP,0)))
end
group by to_char(SESSION_DATE,'YYYYMM') Desc;
我收到此错误:
ORA-20999:无法解析 SQL 查询! ORA-06550:第 8 行,第 12 列:ORA-00907:缺少右括号
解决方法
您不需要 case
子句中的 where
表达式。你可以只使用正则布尔表达式:
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,-1))
) or
(extract(day from SESSIONS.SESSION_DATE) > 14) and
SESSIONS.SESSION_DATE between add_months(trunc(CURRENT_TIMESTAMP,0))
)
)
group by to_char(SESSION_DATE,'YYYYMM'),'Month YY')
order by to_char(SESSION_DATE,'YYYYMM') Desc;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。