我知道可以像Select * from table where ID in (1,2,3,4)
一样从列表中过滤查询,但是可以从列表中进行选择吗?
例如,我想从偏移小时列表中选择日期和偏移日期:
select sysdate, sysdate - offset / 24 as offsetSysdate
from dual, (1,2,3,4) as offset
我知道我可以创建一个表并使用值填充它来执行此操作,但我正在寻找一种方法直接在查询中执行它。
答案 0 :(得分:1)
当然,请使用
的子选择select sysdate, sysdate - offset / 24 as offsetSysdate
from
(
select 1 as offset from dual
union
select 2 from dual
union
select 3 from dual
union
select 4 from dual
)
有关生成数字序列的更复杂方法,请参阅例如here
编辑: Brino想出了建议的代码改进,这里的格式可读,略有改进:
select sysdate, sysdate - offset.value / 24, offset.value as offsetSysdate
from (select r as value
from (select level r from dual connect by level <= 4)
) offset;
答案 1 :(得分:1)
使用with子句创建偏移表:
with offset_table(offset) as
( select level lvl
from dual
connect by level <= 4
)
select sysdate, sysdate - offset / 24 as offsetSysdate
from offset_table;