如何将 csv 字符串拆分为多个值
How to split a csv string to multiple values
我正在使用 vertica,但在尝试将条目拆分为多个条目时遇到了问题。为了清楚起见,请考虑以下示例。假设我有以下 table 个条目(我们称之为 pizza_toppings)
customer_name | order_date | toppings
--------------+------------+----------
sally | 1/1/2011 | peppers, olives
mike | 2/2/2011 | pepperoni, mushrooms
如何将浇头条目分成多行?下面的table就是我想要的:
customer_name | order_date | toppings
--------------+------------+----------
sally | 1/1/2011 | peppers
sally | 1/1/2011 | olives
mike | 2/2/2011 | pepperoni
mike | 2/2/2011 | mushrooms
我一直在浏览多个论坛并遇到了 SPLIT_PART,但这需要您知道分隔条目中有多少部分。最重要的是,我不知道如何在 INSERT 中使用它。
谢谢。
使用 Vertica 的 txt 索引包可以轻松做到这一点。假设你有一个像这样的 table:
SQL> select * from stest;
customer_name | order_date | toppings
---------------+------------+----------------------
sally | 2011-01-01 | peppers, olives
Robert | 2011-04-04 | olives
mike | 2011-02-02 | pepperoni, mushrooms
John | 2011-03-03 | one, two, three
(4 rows)
您可以使用以下 SQL 来获取您要查找的内容:
SQL> select customer_name, order_date, trim(words) as toppings from (
select customer_name, order_date,
v_txtindex.StringTokenizerDelim(toppings,',')
over(partition by customer_name, order_date)
from stest ) a ;
customer_name | order_date | toppings
---------------+------------+-----------
mike | 2011-02-02 | pepperoni
mike | 2011-02-02 | mushrooms
Robert | 2011-04-04 | olives
sally | 2011-01-01 | peppers
sally | 2011-01-01 | olives
John | 2011-03-03 | one
John | 2011-03-03 | two
John | 2011-03-03 | three
(8 rows)
如果您没有安装 txt 索引包...作为 dbadmin:
$ cd /opt/vertica/packages/txtindex/ddl
$ vsql -f install.sql
我正在使用 vertica,但在尝试将条目拆分为多个条目时遇到了问题。为了清楚起见,请考虑以下示例。假设我有以下 table 个条目(我们称之为 pizza_toppings)
customer_name | order_date | toppings
--------------+------------+----------
sally | 1/1/2011 | peppers, olives
mike | 2/2/2011 | pepperoni, mushrooms
如何将浇头条目分成多行?下面的table就是我想要的:
customer_name | order_date | toppings
--------------+------------+----------
sally | 1/1/2011 | peppers
sally | 1/1/2011 | olives
mike | 2/2/2011 | pepperoni
mike | 2/2/2011 | mushrooms
我一直在浏览多个论坛并遇到了 SPLIT_PART,但这需要您知道分隔条目中有多少部分。最重要的是,我不知道如何在 INSERT 中使用它。
谢谢。
使用 Vertica 的 txt 索引包可以轻松做到这一点。假设你有一个像这样的 table:
SQL> select * from stest;
customer_name | order_date | toppings
---------------+------------+----------------------
sally | 2011-01-01 | peppers, olives
Robert | 2011-04-04 | olives
mike | 2011-02-02 | pepperoni, mushrooms
John | 2011-03-03 | one, two, three
(4 rows)
您可以使用以下 SQL 来获取您要查找的内容:
SQL> select customer_name, order_date, trim(words) as toppings from (
select customer_name, order_date,
v_txtindex.StringTokenizerDelim(toppings,',')
over(partition by customer_name, order_date)
from stest ) a ;
customer_name | order_date | toppings
---------------+------------+-----------
mike | 2011-02-02 | pepperoni
mike | 2011-02-02 | mushrooms
Robert | 2011-04-04 | olives
sally | 2011-01-01 | peppers
sally | 2011-01-01 | olives
John | 2011-03-03 | one
John | 2011-03-03 | two
John | 2011-03-03 | three
(8 rows)
如果您没有安装 txt 索引包...作为 dbadmin:
$ cd /opt/vertica/packages/txtindex/ddl
$ vsql -f install.sql