我正在使用Oracle 11g R2。有没有办法在同一表达式中的SQL查询之前为从表中选择的单个值赋予名称(别名)?这是一个单独的SQL命令,我的意思是,没有PL / SQL。
我最接近的是:
WITH
Approved AS (SELECT c.value FROM configuration c WHERE c.code = 'Approved'),
Arrived AS (SELECT c.value FROM configuration c WHERE c.code = 'Arrived'),
Scheduled AS (SELECT c.value FROM configuration c WHERE c.code = 'Scheduled')
SELECT *
FROM list l WHERE l.status_key > (SELECT value FROM Approved);
我正在寻找类似的东西,比如说:
WITH
Approved AS CONSTANT (SELECT c.value FROM configuration c WHERE c.code = 'Approved'),
Arrived AS CONSTANT (SELECT c.value FROM configuration c WHERE c.code = 'Arrived'),
Scheduled AS CONSTANT (SELECT c.value FROM configuration c WHERE c.code = 'Scheduled')
SELECT *
FROM list l WHERE l.status_key > Approved;
我不想内联值的select语句的原因是我的查询很复杂,如果可能的话我宁愿采取一些复杂性。
答案 0 :(得分:5)
我有时会使用这样的结构:
WITH const as
(select max(case when c.code = 'Approved' then c.value end) as Approved,
max(case when c.code = 'Approved' then c.value end) as Approved,
max(case when c.code = 'Scheduled' then c.value end) as Scheduled
from configuration c
),
. . .
SELECT
FROM const cross join
list l
WHERE status_key > Approved;
有时如果我需要查询中不同位置的常量,那么我必须不止一次地引入const
CTE。
答案 1 :(得分:0)
简短的回答是否定的 - 你不能这样做。
你可以创建一个视图来隐藏一些初始的复杂性。
...或者如果你真的想要你可以创建一个函数,并将其用作常量......类似于(不包括任何错误处理):
CREATE OR REPLACE FUNCTION config_code (code IN VARCHAR2)
RETURN configuration.value%TYPE AS
value configuration.value%TYPE;
BEGIN
SELECT c.value INTO value FROM configuration c WHERE c.code = code;
RETURN value;
END;
然后您可以将其用作:
SELECT * FROM list l WHERE l.status_key > config_code('Approved');