将字符串列表转换为表格

时间:2018-07-04 15:31:30

标签: sql google-bigquery

我只是sql的初学者,我想将字符串转换为表

  

申请日期:2018年2月1日要求:购买书籍联系人:电子邮件:hi@gmail.com电话:0123456789订购查询:订单ID:12345678 BL:87654321产品:123456图书

     

申请日期:2018年1月4日要求:重访表联系人:RodionRaskólnikov电子邮件:hello@outlook.com电话:9876543210订购查询:订单ID:87654321 BL:12345678产品:654321桌子

赞: enter image description here

我尝试过:

{0,1}

它无法按我的意愿工作,而且我也不知道如何继续构建表。

2 个答案:

答案 0 :(得分:1)

以下是用于BigQuery标准SQL

假定示例中的行中的字段顺序都已设置

#standardSQL
WITH raw_messages AS (
  SELECT lines
  FROM `my_table` 
  WHERE REGEXP_CONTAINS(lines, '^Date of application: ')
)
SELECT 
  REGEXP_EXTRACT(lines, r'(?i)^Date of application: ([0-9]{2}/[0-9]{2}/[0-9]{4})') AS DATE,
  REGEXP_EXTRACT(lines, r'(?i) Request: (.*?) Contact: ') AS request,
  REGEXP_EXTRACT(lines, r'(?i) Contact: (.*?) email: ') AS contact,
  REGEXP_EXTRACT(lines, r'(?i) email: (.*?) Tel: ') AS email,
  REGEXP_EXTRACT(lines, r'(?i) Tel: (.*?) Ordered inquiry: ') AS phone,
  REGEXP_EXTRACT(lines, r'(?i) Order ID: (.*?) BL: ') AS id,
  REGEXP_EXTRACT(lines, r'(?i) BL: (.*?) Product: ') AS bl,
  REGEXP_EXTRACT(lines, r'(?i) Product: (.*?)$') AS product
FROM raw_messages   

您可以使用下面的问题中的伪数据来测试,并在上面玩

#standardSQL
WITH `project.dataset.my_table` AS (
  SELECT 'Date of application: 01/02/2018 Request: Buy books Contact: email: hi@gmail.com Tel: 0123456789 Ordered inquiry: Order ID: 12345678 BL: 87654321 Product: 123456 Books' lines UNION ALL
  SELECT 'Date of application: 01/04/2018 Request: Retour table Contact: Rodion Raskólnikov email: hello@outlook.com Tel: 9876543210 Ordered inquiry: Order Id: 87654321 BL: 12345678 Product: 654321 Tables'
), raw_messages AS (
  SELECT lines
  FROM `project.dataset.my_table` 
  WHERE REGEXP_CONTAINS(lines, '^Date of application: ')
)
SELECT 
  REGEXP_EXTRACT(lines, r'(?i)^Date of application: ([0-9]{2}/[0-9]{2}/[0-9]{4})') AS DATE,
  REGEXP_EXTRACT(lines, r'(?i) Request: (.*?) Contact: ') AS request,
  REGEXP_EXTRACT(lines, r'(?i) Contact: (.*?) email: ') AS contact,
  REGEXP_EXTRACT(lines, r'(?i) email: (.*?) Tel: ') AS email,
  REGEXP_EXTRACT(lines, r'(?i) Tel: (.*?) Ordered inquiry: ') AS phone,
  REGEXP_EXTRACT(lines, r'(?i) Order ID: (.*?) BL: ') AS id,
  REGEXP_EXTRACT(lines, r'(?i) BL: (.*?) Product: ') AS bl,
  REGEXP_EXTRACT(lines, r'(?i) Product: (.*?)$') AS product
FROM raw_messages    

有结果

Row DATE        request         contact             email               phone       id          bl          product  
1   01/02/2018  Buy books       null                hi@gmail.com        0123456789  12345678    87654321    123456 Books     
2   01/04/2018  Retour table    Rodion Raskólnikov  hello@outlook.com   9876543210  87654321    12345678    654321 Tables    

答案 1 :(得分:1)

如果未知/保证字符串中字段的顺序,但是您知道它们中的所有字段-下面的代码足够聪明,可以正确解析这些字段

#standardSQL
WITH raw_messages AS (
  SELECT lines FROM `project.dataset.my_table` 
  WHERE REGEXP_CONTAINS(lines, '^Date of application: ')
), fields AS (
  SELECT 'Date of application' field, 'date' column UNION ALL
  SELECT 'Request', 'request' UNION ALL
  SELECT 'Contact', 'contact' UNION ALL
  SELECT 'email', 'email' UNION ALL
  SELECT 'Tel', 'phone' UNION ALL
  SELECT 'Order ID', 'id' UNION ALL
  SELECT 'BL', 'bl' UNION ALL
  SELECT 'Product', 'product' UNION ALL
  SELECT 'Ordered inquiry', '' UNION ALL
  SELECT 'Boundary of string', ''
), patterns AS ( 
  SELECT f1.field, f1. column, CONCAT(r'(?i) ',f1.field,': (.*)',f2.field,': ') pattern
  FROM fields f1 CROSS JOIN fields f2
), splits AS (SELECT ARRAY(
      SELECT AS STRUCT column, ARRAY_AGG(value ORDER BY LENGTH(value) LIMIT 1)[OFFSET(0)] value
      FROM (SELECT column, REGEXP_EXTRACT(CONCAT(' Boundary of string: ', lines, ' Boundary of string: '), pattern) value
        FROM patterns ) 
      WHERE NOT value IS NULL AND NOT column = '' GROUP BY column 
    ) arr FROM raw_messages
) SELECT 
  (SELECT value FROM UNNEST(arr) WHERE column='date')     AS DATE,
  (SELECT value FROM UNNEST(arr) WHERE column='request')  AS request,
  (SELECT value FROM UNNEST(arr) WHERE column='contact')  AS contact,
  (SELECT value FROM UNNEST(arr) WHERE column='email')    AS email,
  (SELECT value FROM UNNEST(arr) WHERE column='phone')    AS phone,
  (SELECT value FROM UNNEST(arr) WHERE column='id')       AS id,
  (SELECT value FROM UNNEST(arr) WHERE column='bl')       AS bl,
  (SELECT value FROM UNNEST(arr) WHERE column='product')  AS product
FROM splits     

您可以使用与我的另一个答案相同的虚拟数据进行测试,显然结果应该相同

注意:如您所见-您需要显式设置fields AS (...) CTE,字符串中的所有字段以及相应的列名以任何顺序使用,但很重要-您需要在此添加一个条目-{{ 1}}