简体   繁体   English

BigQuery 排序规则

[英]BigQuery Collation

How can I set a collation order in BigQuery?如何在 BigQuery 中设置排序顺序?

I want something like this我想要这样的东西

SELECT Place
FROM Locations
ORDER BY Place COLLATE "en_CA"

I can't find any documentation other than COLLATE is a reserved word in BigQuery.除了 COLLATE 是 BigQuery 中的保留字外,我找不到任何文档。

BigQuery is sorting the following Strings in [a..zA..Z] order: BigQuery 正在按 [a..zA..Z] 顺序对以下字符串进行排序:

Eg例如

  • ant ant
  • bee蜜蜂
  • cat
  • Apple苹果
  • Banana香蕉
  • Cantaloupe哈密瓜

Is there a way to ask BigQuery to sort in [aA..zZ] order?有没有办法让 BigQuery 按 [aA..zZ] 顺序排序?

  • ant ant
  • Apple苹果
  • bee蜜蜂
  • Banana香蕉
  • cat
  • Cantaloupe哈密瓜

Below example is for BigQuery Standard SQL以下示例适用于 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)

with output output

在此处输入图像描述

Forgot to mention - obviously you can extend this approach to handle unicode text by replacing ascii to unicode function忘记提及 - 显然您可以通过将ascii替换为unicode function 来扩展此方法来处理 unicode 文本

You can try following query it will work for your requirement, it will sort data in [aA..zZ] order:-您可以尝试以下查询,它将满足您的要求,它将按 [aA..zZ] 顺序对数据进行排序:-

SELECT Place
FROM Locations
ORDER BY upper(Place)

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

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