[英]Using query results as In clause parameter
I know I've seen this before, but can't come up with the search terms to find it.我知道我以前见过这个,但无法想出搜索词来找到它。
I have a CTE returning a comma separated list from the below table:我有一个 CTE 从下表中返回一个逗号分隔的列表:
Create table Table
(
ID Number
, Name varchar2(100)
);
insert all
into Table (ID, Name) values (1, 'Alex')
into Table (ID, Name) values (2, 'Amy')
into Table (ID, Name) values (3, 'Jim')
select * from dual;
ID ![]() |
Name![]() |
---|---|
1 ![]() |
Alex![]() |
2 ![]() |
Amy![]() |
3 ![]() |
Jim![]() |
select substr(
listagg(Table.ID || ',') within group (order by null)
, 1
, length(listagg(Table.ID || ',') within group (order by null)) - 1
) IDs
from Table
where Name like 'A%'
Which gives me the results: 1,2
这给了我结果:
1,2
I'm trying to use this result in a query's in
clause:我正在尝试在查询的
in
子句中使用此结果:
with CTE as
(
select substr(
listagg(tbl.ID || ',') within group (order by null)
, 1
, length(listagg(tbl.ID || ',') within group (order by null)) - 1
) IDs
from Table
where Name like 'A%'
)
select *
from Table
where cast(ID as varhcar2(1000)) in (select IDs from CTE) --Use results here
--believe the cast is required to compare, otherwise get a ORA-01722: invalid number
Which I want to return:我想返回:
ID ![]() |
Name![]() |
---|---|
1 ![]() |
Alex![]() |
2 ![]() |
Amy![]() |
How can I use the CTE's resulting IDs
string as the parameter of my in
clause?如何使用 CTE 生成的
IDs
字符串作为in
子句的参数?
I'm afraid I don't understand your "problem".恐怕我不明白你的“问题”。 CTE is really strange;
CTE真的很奇怪; SUBSTR of something ?
SUBSTR 的东西? Why?
为什么? LISTAGG returns the same result anyway.
LISTAGG 无论如何都会返回相同的结果。 Then you want to... what?
那你想……什么? split that result so that you could use it in another query?
拆分该结果以便您可以在另一个查询中使用它? As if you want to make it as complex as possible (and beyond) to solve something "simple".
好像你想让它尽可能复杂(甚至更复杂)来解决“简单”的问题。 Therefore: what real problem are you trying to solve?
因此:你想解决什么真正的问题?
Anyway, here you go: you'll have to split aggregated string into rows if you want to use it in IN
clause:无论如何,在这里你 go:如果你想在
IN
子句中使用它,你必须将聚合字符串拆分成行:
SQL> with CTE as
2 (select listagg(ID || ',') within group (order by null) IDs
3 from Test
4 where Name like 'A%'
5 )
6 select *
7 from Test
8 where id in (select regexp_substr(IDs, '[^,]+', 1, level)
9 from CTE
10 connect by level <= regexp_count(IDS, ',') + 1
11 );
ID NAME
---------- ----------
1 Alex
2 Amy
SQL>
The same result is returned by a simple一个简单的返回相同的结果
SQL> select *
2 from Test
3 where Name like 'A%';
ID NAME
---------- ----------
1 Alex
2 Amy
SQL>
That's why I asked: what problem are you trying to solve?这就是为什么我问:你想解决什么问题?
[EDIT] As of trailing comma: there's none, at least not any Oracle version I used (11g, 12c, 18cXE, 21cXE): [编辑]截至尾随逗号:没有,至少没有我使用的任何 Oracle 版本(11g、12c、18cXE、21cXE):
SQL> select listagg(id, ',') within group (order by null) result from test;
RESULT
------------------------------
1,2,3
SQL> select listagg(name, ',') within group (order by null) result from test;
RESULT
------------------------------
Alex,Amy,Jim
SQL>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.