gpt4 book ai didi

sql - 如何在 SQL 查询中声明一个常量?

转载 作者:行者123 更新时间:2023-12-04 22:23:20 26 4
gpt4 key购买 nike

我正在使用 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 语句的原因是我的查询已经足够复杂,如果可能的话,我宁愿去掉一些复杂性。

最佳答案

我有时会使用这样的构造:

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不止一次。

关于sql - 如何在 SQL 查询中声明一个常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25064900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com