gpt4 book ai didi

mysql - 每 20 次迭代更新 SQL 中的行

转载 作者:可可西里 更新时间:2023-11-01 07:06:52 24 4
gpt4 key购买 nike

我在 Google BigQuery 中有一个包含约 100 万行的表,来自 the NYC Yellow TaxiCab public dataset .从该链接可以看出,架构没有主键。每行代表一次旅行/交易,但没有 customer_id 字段。

我想添加一列 customer_id 并向其分配随机数,以便:

For rows 1-20, `customer_id` should be assigned `1`
For rows 21-40, `customer_id` should be assigned `2`
and so on..

换句话说,我希望表中恰好(和任意)20 行具有特定值 customer_id

最佳答案

为每一行分配一个随机 ID,为每个 new_id 获取大约 20 行的组:

#standardSQL
SELECT CAST(FLOOR(COUNT(*) OVER()/20*RAND()) AS INT64) new_id, *
FROM (
SELECT login
FROM `ghtorrent-bq.ght_2017_04_01.users`
LIMIT 1000000
)

证明在超过一百万行时生成了 50,000 个“customers_id”:

enter image description here

关于mysql - 每 20 次迭代更新 SQL 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46315629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com