将单个 sql 列拆分为五个
Split a single sql column into five
我试图围绕“>”定界符将一列分成最多五列,但我尝试过的东西没有成功:
我试过了
select
id,
compoundColumn,
split(compoundColumn," > ")[1] as "first"
split(compoundColumn," > ")[2] as "second"
from table
where compoundColumn is not null
没用,
这有点像(无论如何是第一部分,而不是第 n 部分)
select
id,
compoundColumn,
first(split(compoundColumn," > ")) as "first"
nth(compoundColumn," > ")[n] as "second"
from table
我在这里找到了很多例子,但他们似乎都在说要使用括号,但括号会引发错误:
Exception: Malformed SQL. More information: Error with SQL statement:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '[1] as "first" from table where compoundColumn IS NOT NULL' at
line 3.
- 您的 SQL
中的 "first" 后缺少逗号
- 我猜 CloudSQL 是基于某些旧版本的 MySQL 的,它只能使用 substring_index 进行拆分(请参阅下面的查询 - 是的,它冗长且笨拙,case 子句必须清理短字符串)
- 也许尝试使用
[offset(0)]
或 [ordinal(1)]
括号,这对我们有用,尽管我们使用 Postgres 方言,也作为 #standardSql,而不是 #legacySql
SQL 从第二点开始:(fiddle)
select id,
case when substring_index(cc,' > ',0) = cc then null else substring_index(substring_index(cc,' > ',1),' > ',-1) end as a1,
case when substring_index(cc,' > ',1) = cc then null else substring_index(substring_index(cc,' > ',2),' > ',-1) end as a2,
case when substring_index(cc,' > ',2) = cc then null else substring_index(substring_index(cc,' > ',3),' > ',-1) end as a3,
case when substring_index(cc,' > ',3) = cc then null else substring_index(substring_index(cc,' > ',4),' > ',-1) end as a4,
case when substring_index(cc,' > ',4) = cc then null else substring_index(substring_index(cc,' > ',5),' > ',-1) end as a5
from d
我终于在 bigquery pull 中而不是在 appmaker 中使用正则表达式提取到了我需要去的地方:
SELECT
CompoundColumn,
REGEXP_EXTRACT(CompoundColumn+">", r'^(.*?)>') first_number,
REGEXP_EXTRACT(CompoundColumn+">", r'^(?:(?:.*?)>){1}(.*?)>') second_number,
REGEXP_EXTRACT(CompoundColumn+">", r'^(?:(?:.*?)>){2}(.*?)>') third_number,
REGEXP_EXTRACT(CompoundColumn+">", r'^(?:(?:.*?)>){3}(.*?)>') fourth_number
FROM
myTable
WHERE
CompoundColumn IS NOT NULL
代码的 +">" 部分很难看,但我无法让它匹配没有以方括号结尾的字符串(">?" 破坏了整个东西)所以我把它们都做了以括号结尾。
所需的旧版SQL将是:
SELECT id,
compoundColumn,
FIRST(SPLIT(compoundColumn, " > ")) AS "first",
NTH(2, SPLIT(compoundColumn, " > ")) AS "second"
FROM table
有关 SPLIT
、FIRST
和 NTH
函数的详细信息,请参阅 this BigQuery documentation page。
我试图围绕“>”定界符将一列分成最多五列,但我尝试过的东西没有成功:
我试过了
select
id,
compoundColumn,
split(compoundColumn," > ")[1] as "first"
split(compoundColumn," > ")[2] as "second"
from table
where compoundColumn is not null
没用,
这有点像(无论如何是第一部分,而不是第 n 部分)
select
id,
compoundColumn,
first(split(compoundColumn," > ")) as "first"
nth(compoundColumn," > ")[n] as "second"
from table
我在这里找到了很多例子,但他们似乎都在说要使用括号,但括号会引发错误:
Exception: Malformed SQL. More information: Error with SQL statement: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[1] as "first" from table where compoundColumn IS NOT NULL' at line 3.
- 您的 SQL 中的 "first" 后缺少逗号
- 我猜 CloudSQL 是基于某些旧版本的 MySQL 的,它只能使用 substring_index 进行拆分(请参阅下面的查询 - 是的,它冗长且笨拙,case 子句必须清理短字符串)
- 也许尝试使用
[offset(0)]
或[ordinal(1)]
括号,这对我们有用,尽管我们使用 Postgres 方言,也作为 #standardSql,而不是 #legacySql
SQL 从第二点开始:(fiddle)
select id,
case when substring_index(cc,' > ',0) = cc then null else substring_index(substring_index(cc,' > ',1),' > ',-1) end as a1,
case when substring_index(cc,' > ',1) = cc then null else substring_index(substring_index(cc,' > ',2),' > ',-1) end as a2,
case when substring_index(cc,' > ',2) = cc then null else substring_index(substring_index(cc,' > ',3),' > ',-1) end as a3,
case when substring_index(cc,' > ',3) = cc then null else substring_index(substring_index(cc,' > ',4),' > ',-1) end as a4,
case when substring_index(cc,' > ',4) = cc then null else substring_index(substring_index(cc,' > ',5),' > ',-1) end as a5
from d
我终于在 bigquery pull 中而不是在 appmaker 中使用正则表达式提取到了我需要去的地方:
SELECT
CompoundColumn,
REGEXP_EXTRACT(CompoundColumn+">", r'^(.*?)>') first_number,
REGEXP_EXTRACT(CompoundColumn+">", r'^(?:(?:.*?)>){1}(.*?)>') second_number,
REGEXP_EXTRACT(CompoundColumn+">", r'^(?:(?:.*?)>){2}(.*?)>') third_number,
REGEXP_EXTRACT(CompoundColumn+">", r'^(?:(?:.*?)>){3}(.*?)>') fourth_number
FROM
myTable
WHERE
CompoundColumn IS NOT NULL
代码的 +">" 部分很难看,但我无法让它匹配没有以方括号结尾的字符串(">?" 破坏了整个东西)所以我把它们都做了以括号结尾。
所需的旧版SQL将是:
SELECT id,
compoundColumn,
FIRST(SPLIT(compoundColumn, " > ")) AS "first",
NTH(2, SPLIT(compoundColumn, " > ")) AS "second"
FROM table
有关 SPLIT
、FIRST
和 NTH
函数的详细信息,请参阅 this BigQuery documentation page。