Bigquery/Python: SQL 使用列识别 table 方向的逻辑

Bigquery/Python: SQL Logic for identifying table direction using a column

输入数据:

Store   Aisle   Bay     Angle
11      33      1       0.0
11      33      2       360.0
11      33      3       90.0
11      33      4       180.0
11      33      5       270.0
11      34      1       360.0
11      34      2       90.0
11      34      3       180.0
11      34      4       270.0
12      34      1       89.83
12      34      2       179.83
12      34      3       269.83
12      34      4       269.83
12      34      5       359.83

场景: 需要在 Bigquery SQL 查询或使用 Python 中构建此逻辑以识别 table 方向(方向顺序 - Front/Right End/Back/Left 结束) 使用 Angle 列。

条件:

  1. 商店的角度从 0 开始。需要制定一个逻辑来识别最低角度并成为正面(0 角度)
  2. 为此添加 90 度以获得其他 table 的方向。

预期输出:

Store   Aisle   Bay     Angle       Direction
11      33      1       0.0         Front
11      33      2       360.0       Front
11      33      3       90.0        Right End
11      33      4       180.0       Back
11      33      5       270.0       Left End
11      34      1       360.0       Front
11      34      2       90.0        Right End
11      34      3       180.0       Back
11      34      4       270.0       Left End
12      34      1       89.83       Front
12      34      2       179.83      Right End
12      34      3       269.83      Back
12      34      4       269.83      Back
12      34      5       359.83      Left End

如有任何帮助,我们将不胜感激。谢谢!

考虑以下方法

select *,
  case div(cast(angle - min(if(angle = 360, 0, angle)) over(partition by store, aisle) as int64), 90)
    when 1 then 'Right'
    when 2 then 'Back'
    when 3 then 'Left'
    else 'Front'
  end as direction
from your_table          

如果应用于我们问题中的样本数据 - 输出是