Как отображать имена, начинающиеся с определенного символа в BQ - PullRequest
0 голосов
/ 02 мая 2018

Как отобразить названия стран, начинающиеся с A, G и E?

#standardSQL
with table1 as(
select "America" as country_name union all
select "Germany" as country_name union all
select "England" as country_name union all
select "Nauru" as country_name union all
select "Brunei" as country_name union all
select "Kiribati" as country_name union all
select "Djibouti" as country_name union all
select "Malta" as country_name
)
select * from table1

1 Ответ

0 голосов
/ 02 мая 2018

Ниже для BigQuery Standard SQL

#standardSQL
WITH table1 AS(
  SELECT "America" AS country_name UNION ALL
  SELECT "Germany" AS country_name UNION ALL
  SELECT "England" AS country_name UNION ALL
  SELECT "Nauru" AS country_name UNION ALL
  SELECT "Brunei" AS country_name UNION ALL
  SELECT "Kiribati" AS country_name UNION ALL
  SELECT "Djibouti" AS country_name UNION ALL
  SELECT "Malta" AS country_name
)
SELECT *
FROM table1
ORDER BY CASE 
  WHEN LOWER(SUBSTR(country_name, 1, 1)) IN ('a', 'g', 'e') 
  THEN CONCAT(' ', country_name) ELSE country_name 
END  

ожидаемый результат -

Row country_name     
1   America  
2   England  
3   Germany  
4   Brunei   
5   Djibouti     
6   Kiribati     
7   Malta    
8   Nauru    

В случае, если вам нужны ТОЛЬКО те страны

#standardSQL
WITH table1 AS(
  SELECT "America" AS country_name UNION ALL
  SELECT "Germany" AS country_name UNION ALL
  SELECT "England" AS country_name UNION ALL
  SELECT "Nauru" AS country_name UNION ALL
  SELECT "Brunei" AS country_name UNION ALL
  SELECT "Kiribati" AS country_name UNION ALL
  SELECT "Djibouti" AS country_name UNION ALL
  SELECT "Malta" AS country_name
)
SELECT *
FROM table1
WHERE LOWER(SUBSTR(country_name, 1, 1)) IN ('a', 'g', 'e') 

или

#standardSQL
WITH table1 AS(
  SELECT "America" AS country_name UNION ALL
  SELECT "Germany" AS country_name UNION ALL
  SELECT "England" AS country_name UNION ALL
  SELECT "Nauru" AS country_name UNION ALL
  SELECT "Brunei" AS country_name UNION ALL
  SELECT "Kiribati" AS country_name UNION ALL
  SELECT "Djibouti" AS country_name UNION ALL
  SELECT "Malta" AS country_name
)
SELECT * 
FROM table1
WHERE REGEXP_CONTAINS(country_name, r'(?i)^(a|g|e)') 

оба приведенных выше возврата:

Row country_name     
1   America  
2   Germany  
3   England  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...