简体   繁体   English

如何将具有不同值的虚拟列添加到 select 查询的结果中?

[英]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.

相关问题 Google BigQuery 如何动态添加列到 Bigquery 结果 - Google BigQuery How to dynamically Add Column to Bigquery Result 如何在另一列中显示具有特定值的 select 行 - How to select rows that have certain values present in another column 如何添加一个列,该列根据另一列的条件对其他列的值进行字符串加法 - How to add a column which does a string addition of values from other column based on condition from another column 如何在aws lambda中保存select的结果 - How to save the result of the select in aws lambda 将查询请求中的列添加到表 - Add Column from a Query request to a Table 当我的结果可能是 FLOAT 而不是 INTEGER 时,如何在计算一组值的平均值时将 FLOAT 合并到我的查询中以避免错误 - How do I incorporate FLOAT in my query when calculating the average of a set of values when my result could be a FLOAT and not INTEGER to avoid error 如何在数据库中添加值 - How to add values in database SQL中如何查询这个结果? 或者也许在 python? - How to query this result in SQL? or maybe in python? 添加具有基于另一个日期时间列的值的日期时间列 - Add datetime column with values based on another datetime column 匹配不同行中相似的列值 - Match similar column values in different rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM