SQL 用于根据字符将单行数据拆分为多行
SQL for splitting a single row data into multiple rows based on character
到目前为止,我已经编写了一个将第一行拆分为多行的查询,但接下来 N 行的结果将是 N 行 returning 空值。
场景是这样的。
select address from sample;
这将 return 跟随 4 行,
Stack Overflow# is a# question and# answer site
Stack Overflow# is a# question and# answer site
Stack Overflow# is a# question and# answer site
Stack Overflow# is a# question and# answer site
当尝试使用以下查询将每一行拆分为多行时,
with test as (select address as str from sample)
select regexp_substr (str, '[^#]+', 1, rownum) split
from test
connect by level <= length (regexp_substr (str, '[^#]+', 1, rownum)) + 1
;
以下值将被 return编辑。
Stack Overflow
is a
question and
answer site
(null)
(null)
(null)
为什么我无法获得所有行的结果?
Why cant I get the results for all rows?
您的查询有两处不正确。
因为ROWNUM的使用不正确。您在同一查询中将 ROWNUM 用作条件,但是,ROWNUM 尚未递增到下一个值。所以,它的价值只有一个。所以,你只得到 1 行。
您需要对所有行进行拆分,而不仅仅是第一行。您需要遍历所有行。但是,同时你应该避免循环并摆脱重复。
有多种方法可以对多行进行字符串拆分。我已经在我的文章中展示了这里 http://lalitkumarb.wordpress.com/2015/03/04/split-comma-delimited-strings-in-a-table-using-oracle-sql/
例如,您可以这样做:
SQL> WITH t AS(
2 SELECT 'Stack Overflow# is a# question and# answer site' text FROM dual UNION ALL
3 SELECT 'Stack Overflow# IS a# question and# answer site' text FROM dual UNION ALL
4 SELECT 'Stack Overflow# is a# question and# answer site' text FROM dual UNION ALL
5 SELECT 'Stack Overflow# IS a# question and# answer site' text FROM dual
6 )
7 SELECT trim(regexp_substr(t.text, '[^#]+', 1, lines.column_value)) text
8 FROM t,
9 TABLE (CAST (MULTISET
10 (SELECT LEVEL FROM dual CONNECT BY LEVEL <= regexp_count(t.text, '#')+1)
11 AS sys.odciNumberList
12 )
13 ) lines
14 /
TEXT
-----------------------------------------------
Stack Overflow
is a
question and
answer site
Stack Overflow
IS a
question and
answer site
Stack Overflow
is a
question and
answer site
Stack Overflow
IS a
question and
answer site
16 rows selected.
SQL>
因此,您现在得到 16 行。完美运行!
到目前为止,我已经编写了一个将第一行拆分为多行的查询,但接下来 N 行的结果将是 N 行 returning 空值。
场景是这样的。
select address from sample;
这将 return 跟随 4 行,
Stack Overflow# is a# question and# answer site
Stack Overflow# is a# question and# answer site
Stack Overflow# is a# question and# answer site
Stack Overflow# is a# question and# answer site
当尝试使用以下查询将每一行拆分为多行时,
with test as (select address as str from sample)
select regexp_substr (str, '[^#]+', 1, rownum) split
from test
connect by level <= length (regexp_substr (str, '[^#]+', 1, rownum)) + 1
;
以下值将被 return编辑。
Stack Overflow
is a
question and
answer site
(null)
(null)
(null)
为什么我无法获得所有行的结果?
Why cant I get the results for all rows?
您的查询有两处不正确。
因为ROWNUM的使用不正确。您在同一查询中将 ROWNUM 用作条件,但是,ROWNUM 尚未递增到下一个值。所以,它的价值只有一个。所以,你只得到 1 行。
您需要对所有行进行拆分,而不仅仅是第一行。您需要遍历所有行。但是,同时你应该避免循环并摆脱重复。
有多种方法可以对多行进行字符串拆分。我已经在我的文章中展示了这里 http://lalitkumarb.wordpress.com/2015/03/04/split-comma-delimited-strings-in-a-table-using-oracle-sql/
例如,您可以这样做:
SQL> WITH t AS(
2 SELECT 'Stack Overflow# is a# question and# answer site' text FROM dual UNION ALL
3 SELECT 'Stack Overflow# IS a# question and# answer site' text FROM dual UNION ALL
4 SELECT 'Stack Overflow# is a# question and# answer site' text FROM dual UNION ALL
5 SELECT 'Stack Overflow# IS a# question and# answer site' text FROM dual
6 )
7 SELECT trim(regexp_substr(t.text, '[^#]+', 1, lines.column_value)) text
8 FROM t,
9 TABLE (CAST (MULTISET
10 (SELECT LEVEL FROM dual CONNECT BY LEVEL <= regexp_count(t.text, '#')+1)
11 AS sys.odciNumberList
12 )
13 ) lines
14 /
TEXT
-----------------------------------------------
Stack Overflow
is a
question and
answer site
Stack Overflow
IS a
question and
answer site
Stack Overflow
is a
question and
answer site
Stack Overflow
IS a
question and
answer site
16 rows selected.
SQL>
因此,您现在得到 16 行。完美运行!