[英]How to add a dummy column with different values to the result of a select query?
I have a table containing a column Bucket
, I want to add one more column with some random time stamp values for each Bucket in the select query so as to get a table like below one.我有一个包含Bucket
列的表,我想在 select 查询中为每个 Bucket 添加一个带有一些随机时间戳值的列,以便获得如下表。 How can I achieve this?我怎样才能做到这一点?
Bucket桶 | created_on创建于 |
---|---|
bucket-1桶-1 | 2000-06-02 00:37:12 2000-06-02 00:37:12 |
bucket-2 bucket-2 | 2005-06-02 23:50:19 2005-06-02 23:50:19 |
bucket-5桶-5 | 2020-06-02 12:21:12 2020-06-02 12:21:12 |
bucket-3 bucket-3 | 2019-06-02 20:28:19 2019-06-02 20:28:19 |
You can calculate a unixtime between two dates and transform it into a date, adding a randon number makes it random您可以计算两个日期之间的unixtime并将其转换为日期,添加随机数使其随机
#standardSQL
WITH parameters AS (
SELECT DATE '2010-01-01' start_date, DATE '2022-04-08' finish_date
)
SELECT t1.Bucket, DATE_FROM_UNIX_DATE(CAST(start_date + (finish_date - start_date) * RAND() AS INT64)) random_date
FROM table1 t1 CROSS JOIN parameters p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.