按序列号命名行

Name rows by serial number

我有表A -

NRow
----
   1
   2
 ...
1000000

和表 B -

RowCount  Name
--------------
     100    A
      10    B
     200    C
     ...   ...

我想向 TableA 添加一个标记,前 100 行的值为 "A",接下来的 10 行的值为 "B",接下来的 200 行的值为 "C",依此类推. Hive 可以做到这一点吗?

回答我自己的问题 - 可以通过交叉连接实现。

select a.*, b.Name
from tablea a,
(select RowCount, sum(RowCount) rows over (order by Name) as CumRow from tableb) b
where a.NRow between b.CumRow-b.RowCount+1 and b.CumRow

UDF 也可以做到 - 但它必须有效地模拟交叉连接(因为它需要携带 TableB 的全部信息并扫描 TableA)。