我想将一行中的多个值拆分为 sap hana sql中的单独行。 表:
id name
1 kabil,arasan
但我想要这样的输出:
id name
1 kabil
1 arasan
答案 0 :(得分:2)
没有过程,您可以使用以下SQL(限制:它假定您的csv字段中有最大数量的值)。我只会将它用于临时目的。
CREATE COLUMN TABLE "TEST_SPLIT"(
"SOME_KEY" VARCHAR(10),
"CSV_STR" VARCHAR(1000) );
INSERT INTO "TEST_SPLIT" ("SOME_KEY", "CSV_STR") VALUES ('1', 'hello world');
INSERT INTO "TEST_SPLIT" ("SOME_KEY", "CSV_STR") VALUES ('2', 'one,two,three');
INSERT INTO "TEST_SPLIT" ("SOME_KEY", "CSV_STR") VALUES ('3', NULL);
Select * from
( select "SOME_KEY" "KEY", "ELEMENT_NUMBER" "ORD",
SUBSTR_REGEXPR('(?<=^|,)([^,]*)(?=,|$)' IN "TEST_SPLIT"."CSV_STR" OCCURRENCE "SERIES"."ELEMENT_NUMBER" GROUP 1) "VAL"
from "TEST_SPLIT",
SERIES_GENERATE_INTEGER(1, 1, 10 ) "SERIES" -- replace 10 with your max. number of values in CSV-Field
)
where "VAL" is not null
order by "KEY", "ORD"
答案 1 :(得分:0)
对SUBSTR_REGEXPR函数的使用进行了微调。上周,我在哈纳(Hana)将其用作Script_View,可以确认它是否有效。
Begin
VAR_OUT =
select *
from ( select
ST.PARAM, -- Source table id
NT.Element_Number, -- Occurrence number within source table row
SUBSTR_REGEXPR( '([;])([^;]*)(?=;)'
IN CONCAT(CONCAT(';',ST.VALUE),';')
OCCURRENCE NT.Element_Number
GROUP 2
) splitted -- string piece
from
"_SYS_BIC"."Financial_Planning_and_Analysis/ZZTPARPAR_SOURCE" as ST, -- source table
SERIES_GENERATE_INTEGER(1, 0, 10 ) as NT -- numbers table
) tbl
where splitted is not null
order by PARAM, Element_Number;
End