所以我必须从用户的电子邮件中创建用户ID,所有数据都显示在BigQuery上。在python中,pandas是一个简单的单行程序,如下所示:
all_data['user_id'] = all_data['email'].factorize()[0]
但我无法在BigQuerySQL中找到实现这一点的方法。我试着使用RANK()
函数,但它的效果并不理想。目前,我正在尝试使用带有RANK()
的窗口函数,但对于这样一个简单的任务,使用这种方法似乎有点牵强。所有数据都已经在BigQuery上了,所以任何关于以其他方式(即使是在SQL之外)执行此操作的建议都是很好的
函数的作用是:根据提供的列分配一个唯一的ID,所以如果电子邮件像email1@example.com, email2@example.com, email1@example.com, email3@example.com, email1@example.com, email2@example.com
,它将返回:[0, 1, 0, 2, 0, 1]
等等
我在数据库中还有其他列,所以RANK()
和ROW_NUMBER()
似乎并没有单独的帮助。我正试着绕过那个
为此,可以使用
DENSE_RANK()
窗口函数:这将产生如下结果(使用Mikhail Berlyant's example data作为起点):
考虑以下两个选项
注意,我使用的是稍加修改的数据示例-您将看到原因(我希望)
备选案文1:
在这种情况下,如果在分配唯一的_id之前应该设置这些电子邮件的顺序-例如通过
sent
列。在这种情况下考虑以下有输出
备选案文2:
如果排序不是很重要,你可以按字母顺序排序,下面考虑一下使用内置^ ^ a2}函数的简单查询。有输出
显然,在这种情况下,您可以跳过使用udf,只需在最终选择中使用ragge_bucket(而不是在udf中)
相关问题 更多 >
编程相关推荐