将行分为2列

时间:2019-03-21 16:16:04

标签: sql google-bigquery google-query-language

尝试使用此查询在存在'/'分隔符的情况下按语言拆分数据,但是可以工作,但是当不存在'/'时,该行转到法语列,我希望如果不存在,则法语列为空“ /”,数据应在英文列中显示。它是根据网站ID排序的,因此只有ID 412中包含法语。

SELECT
s.siteid,
s.notes, --This is the column that CSI uses for the description.
split(s.notes,'/') [safe_OFFSET(0)] French,
split(s.notes,'/') [safe_OFFSET(1)] english
FROM AloomaTestBeta.SCSERVICES s

siteid  notes                           French                  english
412     Le cardio-/ Cardio Tennis .     Le cardio-tennis        Cardio Tennis 
412     Le cardio-/Cardio Tennis        Le cardio-tennis        Cardio Tennis 
412     La ligue de / Drop-In Tennis    La ligue de tennis      Drop-In Tennis 
411     An extended duration            An extended duration    null                    
411     Increase flexibility            Increase flexibility    Null    

也尝试使用case语句,但是当没有'/'分隔符时,它开始为我提供空值。

SELECT
s.siteid,
s.notes, --This is the column that CSI uses for the description.
case when  s.siteid = 412 then split(s.notes,'/') [safe_OFFSET(0)] else null end as French,
split(s.notes,'/') [safe_OFFSET(1)] english
FROM AloomaTestBeta.SCSERVICES s

siteid  notes                           French                  english
412     Le cardio-/ Cardio Tennis .     Le cardio-tennis        Cardio Tennis 
412     Le cardio-/Cardio Tennis        Le cardio-tennis        Cardio Tennis 
412     La ligue de / Drop-In Tennis    La ligue de tennis      Drop-In Tennis 
411     An extended duration            null                     null                   
411     Increase flexibility            null                      Null                  

这就是我想要的结果

siteid  notes                           French                  english
412     Le cardio-/ Cardio Tennis .     Le cardio-tennis        Cardio Tennis 
412     Le cardio-/Cardio Tennis        Le cardio-tennis        Cardio Tennis 
412     La ligue de / Drop-In Tennis    La ligue de tennis      Drop-In Tennis 
411     An extended duration            null                    An extended duration 
411     Increase flexibility            Null                    Increase flexibility

2 个答案:

答案 0 :(得分:1)

以下是用于BigQuery标准SQL

#standardSQL
SELECT
  s.siteid,
  s.notes, --This is the column that CSI uses for the description.
  IF(v, SPLIT(s.notes,'/')[OFFSET(0)], NULL) French,
  IF(v, SPLIT(s.notes,'/')[SAFE_OFFSET(1)], SPLIT(s.notes,'/')[OFFSET(0)]) English
FROM `AloomaTestBeta.SCSERVICES` s, UNNEST([s.notes LIKE '%/%']) v

您可以使用问题中的示例数据来测试,玩游戏,如下例所示

#standardSQL
WITH `AloomaTestBeta.SCSERVICES` AS (
  SELECT 412 siteid, 'Le cardio-/ Cardio Tennis' notes UNION ALL
  SELECT 412, 'Le cardio-/Cardio Tennis' UNION ALL
  SELECT 412, 'La ligue de / Drop-In Tennis' UNION ALL
  SELECT 411, 'An extended duration' UNION ALL
  SELECT 411, 'Increase flexibility' 
)
SELECT
  s.siteid,
  s.notes, --This is the column that CSI uses for the description.
  IF(v, SPLIT(s.notes,'/')[OFFSET(0)], NULL) French,
  IF(v, SPLIT(s.notes,'/')[SAFE_OFFSET(1)], SPLIT(s.notes,'/')[OFFSET(0)]) English
FROM `AloomaTestBeta.SCSERVICES` s, UNNEST([s.notes LIKE '%/%']) v  

有结果

Row siteid  notes                           French          English  
1   412     Le cardio-/ Cardio Tennis       Le cardio-      Cardio Tennis    
2   412     Le cardio-/Cardio Tennis        Le cardio-      Cardio Tennis    
3   412     La ligue de / Drop-In Tennis    La ligue de     Drop-In Tennis   
4   411     An extended duration            null            An extended duration     
5   411     Increase flexibility            null            Increase flexibility       

如果您了解上述方法的工作原理,那么您就可以准备更优雅的解决方案了

#standardSQL
SELECT
  s.siteid,
  s.notes, --This is the column that CSI uses for the description.
  SPLIT(s.notes,'/')[SAFE_OFFSET(v)] French,
  SPLIT(s.notes,'/')[SAFE_OFFSET(1 - v)] English
FROM `AloomaTestBeta.SCSERVICES` s, UNNEST([IF(s.notes LIKE '%/%', 0, 1)]) v

答案 1 :(得分:0)

假定siteid将使用'/'标识记录。这应该起作用:

case when s.siteid = 412 then split(s.notes, '/')[SAFE_OFFSET(0)] else null end as French,
case when s.siteid = 412 then split(s.notes, '/')[SAFE_OFFSET(1)] else split(s.notes, '/')[SAFE_OFFSET(0)] end as English

播放一些虚拟数据:

#standardSQL
WITH test_table AS (
  SELECT 412 as siteid, "test/test" as notes union all
  SELECT 413 as siteid, "test" as notes
)
SELECT 
case when siteid = 412 then split(notes, '/')[SAFE_OFFSET(0)] else null end as French,
case when siteid = 412 then split(notes, '/')[SAFE_OFFSET(1)] else split(notes, '/')[SAFE_OFFSET(0)] end as English
FROM test_table

它给出以下结果,根据您的描述,这应该是理想的结果。

Row French  English  
1   test    test     
2   null    test