如何使用两个别名设置 WITH 查询并选择
How to setup WITH queries with two aliases and selects
在 SQL 的开头使用 WITH 构造来执行几个子查询,然后可用于 Oracle 查询的主体(后续)部分的多个位置。
你能否在你的示例中加入或匹配 t2 与 t1,即翻译成我的代码,
WITH t1 as (select * from AA where FIRSTNAME like 'Kermit%'),
t2 as (select * from BB B join t1 on t1.ID = B.ID)
我想我很清楚 t1 可以在 WITH 的第二段内引用。
括号内的两个 select 是否几乎是可以引用不同表的成熟查询,可以在需要条件的地方使用?
我不清楚在这种情况下是否可以使用 JOIN 来匹配来自 AA 和 BB 的记录,或者可能是在第二个 WITH 子句中使用不同的连接方法。我看到的一些示例在 select "below" 两个 WITH 子句的正文中有一个 WHERE A=B。
我在遵循这些 WITH 声明时遇到的错误是无法识别 t2 中的标识符(字段名称),但来自 t1 的 SQL 引用的其余部分很好。似乎 WITH 语法运行正常,但无法访问第二个 WITH 段 (t2) 的结果。
这是整个无效查询。
with S as (select * from all_records where RESP_MGR like 'Smith%'),
CHL as (select * from change_log CL JOIN S on S.SERVER_ID = CL.SERVER_ID)
select * from (
select
SERVER_ID as ServerID,
'Current Environment' as Event,
SYSDATE as "Date",
NEW_VALUE as "Data"
from CHL
where OBJECT_DESCR = 'Production'
)
where "Data" is not null
甲骨文说,
ORA-00904: "SERVER_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
我想这是一个歧义问题。在
CHL as (select * from change_log CL JOIN S on S.SERVER_ID = CL.SERVER_ID)
SELECT *
中有两列名为 SERVER_ID
:S.SERVER_ID
和 CL.SERVER_ID
。我猜 Oracle 在其结果集中给它们起了类似 S_SERVER_ID
和 CL_SERVER_ID
的别名。
解决这个问题的一个简单方法是 USING
子句
CHL as (select * from change_log CL JOIN S using (SERVER_ID))
ANSI SQL has table references (correlation names) scoped to just one level deep
换句话说,您的主查询(第一级)将能够引用该列,但由于您的子查询(第二级)不止一层深,它无法引用该列,即您收到错误的原因。
我不知道你的表的结构,但我认为你并不真的需要子查询,而是可以使用这样的东西(空气代码):
with S as (select * from all_records where RESP_MGR like 'Smith%'),
CHL as (select * from change_log CL JOIN S on S.SERVER_ID = CL.SERVER_ID)
select
SERVER_ID as ServerID,
'Current Environment' as Event,
SYSDATE as "Date",
NEW_VALUE as "Data"
from CHL
where OBJECT_DESCR = 'Production' and NEW_VALUE is not null
;
在 SQL 的开头使用 WITH 构造来执行几个子查询,然后可用于 Oracle 查询的主体(后续)部分的多个位置。
你能否在你的示例中加入或匹配 t2 与 t1,即翻译成我的代码,
WITH t1 as (select * from AA where FIRSTNAME like 'Kermit%'),
t2 as (select * from BB B join t1 on t1.ID = B.ID)
我想我很清楚 t1 可以在 WITH 的第二段内引用。
括号内的两个 select 是否几乎是可以引用不同表的成熟查询,可以在需要条件的地方使用?
我不清楚在这种情况下是否可以使用 JOIN 来匹配来自 AA 和 BB 的记录,或者可能是在第二个 WITH 子句中使用不同的连接方法。我看到的一些示例在 select "below" 两个 WITH 子句的正文中有一个 WHERE A=B。
我在遵循这些 WITH 声明时遇到的错误是无法识别 t2 中的标识符(字段名称),但来自 t1 的 SQL 引用的其余部分很好。似乎 WITH 语法运行正常,但无法访问第二个 WITH 段 (t2) 的结果。
这是整个无效查询。
with S as (select * from all_records where RESP_MGR like 'Smith%'),
CHL as (select * from change_log CL JOIN S on S.SERVER_ID = CL.SERVER_ID)
select * from (
select
SERVER_ID as ServerID,
'Current Environment' as Event,
SYSDATE as "Date",
NEW_VALUE as "Data"
from CHL
where OBJECT_DESCR = 'Production'
)
where "Data" is not null
甲骨文说,
ORA-00904: "SERVER_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
我想这是一个歧义问题。在
CHL as (select * from change_log CL JOIN S on S.SERVER_ID = CL.SERVER_ID)
SELECT *
中有两列名为 SERVER_ID
:S.SERVER_ID
和 CL.SERVER_ID
。我猜 Oracle 在其结果集中给它们起了类似 S_SERVER_ID
和 CL_SERVER_ID
的别名。
解决这个问题的一个简单方法是 USING
子句
CHL as (select * from change_log CL JOIN S using (SERVER_ID))
ANSI SQL has table references (correlation names) scoped to just one level deep
换句话说,您的主查询(第一级)将能够引用该列,但由于您的子查询(第二级)不止一层深,它无法引用该列,即您收到错误的原因。
我不知道你的表的结构,但我认为你并不真的需要子查询,而是可以使用这样的东西(空气代码):
with S as (select * from all_records where RESP_MGR like 'Smith%'),
CHL as (select * from change_log CL JOIN S on S.SERVER_ID = CL.SERVER_ID)
select
SERVER_ID as ServerID,
'Current Environment' as Event,
SYSDATE as "Date",
NEW_VALUE as "Data"
from CHL
where OBJECT_DESCR = 'Production' and NEW_VALUE is not null
;