Jer*_*emy 1 sql oracle temp-tables
我正在尝试创建一个"存储"两个值之间的解码的子表,因为我需要多次使用该解码.让我们说这些是我的表:
Table Person
Name Number_name
Jeremy One
Thomas Two
Stephen Three
Run Code Online (Sandbox Code Playgroud)
我当前的SQL看起来像这样:
SELECT
decode (number_name,
'one',1,
'two',2,
'three',3,
'four',4)
num
FROM person where name = 'Jeremy'
and (some other condition)
UNION SELECT
decode (number_name,
'one',1,
'two',2,
'three',3,
'four,4)
num
FROM Person
where Name <> "Jeremy"
and (some other condition)
Run Code Online (Sandbox Code Playgroud)
我希望能做的是这样的:
SELECT num from my_temp_table where name = "Jeremy" and (some other condition)
union select num from my_temp_table where name <> "Jeremy" and (some other condition)
...
Run Code Online (Sandbox Code Playgroud)
其中my_temp_table是在该查询期间构造的(它在查询完成运行时不再存在)并且看起来像
Table my_temp_table
Name num
One 1
Two 2
Three 3
Four 4
Run Code Online (Sandbox Code Playgroud)
希望我能做到这一点,而不是"选择一个名字,1个来自双联盟的数字......"
这可行吗?
该WITH条款听起来与你所描述的最接近.但这需要您以某种方式生成数据.从中选择DUAL可能是最简单的选择
WITH my_temp_table AS (
SELECT 'One' name, 1 num from dual union all
SELECT 'Two', 2 from dual union all
SELECT 'Three', 3 from dual union all
SELECT 'Four', 4 from dual
)
SELECT *
FROM my_temp_table
JOIN person ON (<<some join condition>>)
WHERE <<some predicate>>
Run Code Online (Sandbox Code Playgroud)
既然你不想结合大量的查询,你可以做类似的事情
WITH my_temp_table AS (
select level num,
initcap( to_char( to_date( level, 'J' ),
'JSP' )) name
from dual
connect by level <= 4
)
...
Run Code Online (Sandbox Code Playgroud)