Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在sql中拆分单元格并创建新行_Sql_Db2 - Fatal编程技术网

如何在sql中拆分单元格并创建新行

如何在sql中拆分单元格并创建新行,sql,db2,Sql,Db2,我有一个存储多个逗号分隔值的列。我需要以某种方式将其拆分,以便将其拆分为与该列中的值以及该行中的剩余值一样多的行 例如: 输出: John 111 2Jan Sam 222 3Jan Sam 333 3Jan Jame 444 2Jan Jame 555 2Jan Jame 666 2Jan Jen 777 4Jan 附言:我见过许多类似的问题,但找不到一种方法来进行拆分。这个解决方案是建立在Vertica上的,但它适用于提供与拆分部分对应的功能的每个数据库 它的一部分对应于与每个符合A

我有一个存储多个逗号分隔值的列。我需要以某种方式将其拆分,以便将其拆分为与该列中的值以及该行中的剩余值一样多的行

例如:

输出:

John 111 2Jan
Sam  222 3Jan
Sam  333 3Jan
Jame 444 2Jan
Jame 555 2Jan
Jame 666 2Jan
Jen  777 4Jan

附言:我见过许多类似的问题,但找不到一种方法来进行拆分。

这个解决方案是建立在Vertica上的,但它适用于提供与拆分部分对应的功能的每个数据库

它的一部分对应于与每个符合ANSI标准的数据库平台配合使用的取消数据透视技术,我在这里只解释了脚本的取消数据透视部分:

所以我会像下面这样做。我假设最小日期表示是两列输入表第二列的一部分。因此,我首先在第一个公共表表达式中拆分短日期文字,并在注释中列出CTE的输出,然后将逗号分隔的列表拆分为标记

下面是:

WITH
-- input
input(name,the_string) AS (
          SELECT 'John', '111 2Jan'
UNION ALL SELECT 'Sam' , '222,333 3Jan'
UNION ALL SELECT 'Jame', '444,555,666 2Jan'
UNION ALL SELECT 'Jen' , '777 4Jan'
)
,
-- put the strange date literal into a separate column
the_list_and_the_date(name,list,datestub) AS (
SELECT
  name
, SPLIT_PART(the_string,' ',1)
, SPLIT_PART(the_string,' ',2)
FROM input
)
-- debug
-- SELECT * FROM the_list_and_the_date;
-- name|list       |datestub
-- John|111        |2Jan
-- Sam |222,333    |3Jan
-- Jame|444,555,666|2Jan
-- Jen |777        |4Jan
,
-- ten integers (too many for this example) to use as pivoting value and as "index"
ten_ints(idx) AS (
          SELECT  1 
UNION ALL SELECT  2 
UNION ALL SELECT  3 
UNION ALL SELECT  4 
UNION ALL SELECT  5 
UNION ALL SELECT  6 
UNION ALL SELECT  7 
UNION ALL SELECT  8 
UNION ALL SELECT  9 
UNION ALL SELECT 10
)
-- the final query - pivoting prepared input using a CROSS JOIN with ten_ints
-- and filter out where the SPLIT_PART() expression evaluates to the empty string
SELECT
  name
, SPLIT_PART(list,',',idx) AS token
, datestub
FROM the_list_and_the_date
CROSS JOIN ten_ints
WHERE SPLIT_PART(list,',',idx) <> ''
;

name|token|datestub
John|111  |2Jan
Jame|444  |2Jan
Jame|555  |2Jan
Jame|666  |2Jan
Sam |222  |3Jan
Sam |333  |3Jan
Jen |777  |4Jan
玩得开心


理智的马可

如果使用oracle,您可以使用解释的方法拆分字符串。。。应该能够在一个sql语句上执行。。。幸运的是,对于Postgres,您可以使用unneststring_to_arrayP.S。修复您的数据结构,规范化您的表,从此快乐起来。使用SQL Server,您可以在可能需要注册的SQLServerCentral上使用Jeff Moden所述的数字表。。。值得一提的是,我们正在使用基于DB2的Dell Toad应用程序。
WITH
-- input
input(name,the_string) AS (
          SELECT 'John', '111 2Jan'
UNION ALL SELECT 'Sam' , '222,333 3Jan'
UNION ALL SELECT 'Jame', '444,555,666 2Jan'
UNION ALL SELECT 'Jen' , '777 4Jan'
)
,
-- put the strange date literal into a separate column
the_list_and_the_date(name,list,datestub) AS (
SELECT
  name
, SPLIT_PART(the_string,' ',1)
, SPLIT_PART(the_string,' ',2)
FROM input
)
-- debug
-- SELECT * FROM the_list_and_the_date;
-- name|list       |datestub
-- John|111        |2Jan
-- Sam |222,333    |3Jan
-- Jame|444,555,666|2Jan
-- Jen |777        |4Jan
,
-- ten integers (too many for this example) to use as pivoting value and as "index"
ten_ints(idx) AS (
          SELECT  1 
UNION ALL SELECT  2 
UNION ALL SELECT  3 
UNION ALL SELECT  4 
UNION ALL SELECT  5 
UNION ALL SELECT  6 
UNION ALL SELECT  7 
UNION ALL SELECT  8 
UNION ALL SELECT  9 
UNION ALL SELECT 10
)
-- the final query - pivoting prepared input using a CROSS JOIN with ten_ints
-- and filter out where the SPLIT_PART() expression evaluates to the empty string
SELECT
  name
, SPLIT_PART(list,',',idx) AS token
, datestub
FROM the_list_and_the_date
CROSS JOIN ten_ints
WHERE SPLIT_PART(list,',',idx) <> ''
;

name|token|datestub
John|111  |2Jan
Jame|444  |2Jan
Jame|555  |2Jan
Jame|666  |2Jan
Sam |222  |3Jan
Sam |333  |3Jan
Jen |777  |4Jan