How to split multiple values from a row into separate rows?

前端 未结 2 863
我寻月下人不归
我寻月下人不归 2021-01-25 08:57

I want to split multiple values from a row into a separate row in sap hana sql. table :

 id  name
 1   kabil,arasan

but I want

2条回答
  •  悲&欢浪女
    2021-01-25 09:33

    1. try to avoid csv-like data in columns, e.g. normalize during ETL process
    2. you can use a procedure, see [HANA: Split string?
    3. without a procedure you can use the following SQL (restriction: it assumes a maximum number of values in your csv-field). I would use it only for adhoc purposes.

      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]: HANA: Split string?

提交回复
热议问题