简体   繁体   English

如何在 BigQuery 中添加显示星期几的列?

[英]How can add a column that shows the day of the week in BigQuery?

Im looking to show the day of the week that pertains to the date in my table.我希望在我的表格中显示与日期相关的星期几。 The table is under bellabeat-case-study-2-376122.fitbit_data.daily_activity and the column I'm pulling from is ActivityDate.该表位于bellabeat-case-study-2-376122.fitbit_data.daily_activity ,我从中提取的列是 ActivityDate。 I'm looking to create a column titled DayofWeek.我想创建一个名为 DayofWeek 的专栏。

I've attempted to create a column and insert the data into the column, but it keeps coming back null for the results.我试图创建一个列并将数据插入该列,但结果一直返回 null。

##Created a column
ALTER TABLE `bellabeat-case-study-2-376122.fitbit_data.daily_activity`
ADD COLUMN DayofWeek INT64;```

##Attempted to insert the data
INSERT INTO `bellabeat-case-study-2-376122.fitbit_data.daily_activity` (DayofWeek)
SELECT EXTRACT(DAYOFWEEK FROM ActivityDate)
FROM `bellabeat-case-study-2-376122.fitbit_data.daily_activity`

As @JNevill mentioned in the comments:正如@JNevill 在评论中提到的:

You don't want to INSERT , you want to UPDATE :你不想INSERT ,你想UPDATE

UPDATE bellabeat-case-study-2-376122.fitbit_data.daily_activity SET DayofWeek =  EXTRACT(DAYOFWEEK FROM ActivityDate)

That being said, your INSERT should have still worked, but it would have created a set of new records equal in row count to your existing table (essentially doubling the rows each time you run it).话虽这么说,您的INSERT应该仍然有效,但它会创建一组与现有表的行数相等的新记录(每次运行它时,行数基本上加倍)。

Posting the answer as community wiki for the benefit of the community that might encounter this use case in the future.将答案发布为社区 wiki ,以造福于将来可能会遇到此用例的社区。

Feel free to edit this answer for additional information.请随意编辑此答案以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM