将行拆分为 2 列

Splitting row in 2 columns

尝试按存在“/”定界符的语言拆分数据使用此查询它有效但是当没有“/”时行转到法语列如果有,我希望法语列为空没有'/',数据应该放在英文栏中。它按站点 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 语句,但当没有“/”分隔符时它开始给我 null。

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

假设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

以下适用于 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