如何在case条件下编写选择查询?我正在用case语句编写SQL语句,在其中我可以在case中运行select查询。
如果当前时间是凌晨3点之前,下面的查询将返回sysdate-1的条目,否则返回sysdate-2的条目。
select case when to_number(to_char(trunc(sysdate, 'hh'), 'h24')) in (00, 01, 02) then
select * from table where datetime > sysdate-1
else
select * from table where datetime > sysdate-2
end我收到一个语法错误。有人能帮帮我吗?
发布于 2022-06-09 12:08:28
使用常规的AND/OR代替:
select *
from table
where (to_number(to_char(trunc(sysdate, 'hh'), 'h24')) not in (00, 01, 02)
and datetime > sysdate - 2)
or datetime > sysdate - 1 -- i.e. "in (00, 01, 02)"(大多数产品发现和/或比case表达式更容易优化。)
发布于 2022-06-09 12:08:21
你的逻辑有点颠倒。将大小写移到where子句中,它将按预期工作。
select * from table
where
datetime > sysdate - case when to_number(to_char(trunc(sysdate, 'hh'), 'h24')) in (00, 01, 02) then -1 else -2 endhttps://stackoverflow.com/questions/72559266
复制相似问题