繁体   English   中英

BigQuery 排序规则

[英]BigQuery Collation

如何在 BigQuery 中设置排序顺序?

我想要这样的东西

SELECT Place
FROM Locations
ORDER BY Place COLLATE "en_CA"

除了 COLLATE 是 BigQuery 中的保留字外,我找不到任何文档。

BigQuery 正在按 [a..zA..Z] 顺序对以下字符串进行排序:

例如

  • ant
  • 蜜蜂
  • 苹果
  • 香蕉
  • 哈密瓜

有没有办法让 BigQuery 按 [aA..zZ] 顺序排序?

  • ant
  • 苹果
  • 蜜蜂
  • 香蕉
  • 哈密瓜

以下示例适用于 BigQuery 标准 SQL

#standardSQL
create temp function collate_order(text string) as ((
  select string_agg(chr(1000 * ascii(lower(c)) - ascii(c)), '' order by offset)
  from unnest(split(text)) c with offset
));
with `project.dataset.Locations` as (
  select 'ant' as Place union all
  select 'Apple' union all
  select 'bee' union all
  select 'apple' union all
  select 'cat' union all
  select 'Banana' union all
  select 'Cantaloupe' 
)
select Place
from `project.dataset.Locations`
order by collate_order(Place)

output

在此处输入图像描述

忘记提及 - 显然您可以通过将ascii替换为unicode function 来扩展此方法来处理 unicode 文本

您可以尝试以下查询,它将满足您的要求,它将按 [aA..zZ] 顺序对数据进行排序:-

SELECT Place
FROM Locations
ORDER BY upper(Place)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM