Google BigQuery中的姓名

问题描述 投票:0回答:1

将全名拆分为名字和姓氏。

我想将基于“,”的全名分为firstName和LastName。 “,”之前的任何内容都应选择为firstName,“,”之前的任何内容均应为lastName。我使用split(fullName)函数,并使用offset来获取firstName和lastName,但是当fullName中有2个“,”时遇到错误。我用了这段代码:

WITH getfirstName AS 
(
  SELECT ID,Name[offset(1)] AS firstName FROM
    (SELECT * FROM 
    (SELECT ID,Name,ARRAY_LENGTH(Name) as length  FROM
    (
    SELECT ID, SPLIT(OPR_FULL) as Name FROM `project.Dataset.Name` )
    )
    WHERE length >=2)
)


, getLastName AS 
(
  SELECT  A.ID,  NAME[OFFSET(0)] AS lastName
  FROM(SELECT SPLIT(OPR_FULL) as Name,ID FROM `project.Dataset.Name`) A
  LEFT JOIN getfirstName B
  ON A.ID=B.ID
)

是否有更好的方法?我该如何解决“,”问题?如果我使用OFFSET(2),则会收到OffBound错误。

sql google-bigquery bigdata
1个回答
0
投票

下面的示例适用于BigQuery Standard SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 'Geogre,Bailey' fullName UNION ALL
  SELECT 'Kat,JR,Cruz'
)
SELECT fullName,
  REGEXP_EXTRACT(fullName, r'(.*?),') lastName,
  REGEXP_EXTRACT(fullName, r',(.*)') firstName
FROM `project.dataset.table`  

有输出

Row fullName        lastName    firstName    
1   Geogre,Bailey   Geogre      Bailey   
2   Kat,JR,Cruz     Kat         JR,Cruz  
© www.soinside.com 2019 - 2024. All rights reserved.