在 Bigquery 中,如何从可用的 table 列表中获取星期几?

In Bigquery, How to get Start Day of the week from available table list?

输入: 在 bigquery 中有一个 table,格式如下

Store   Date        WeekNumber
11      2019-11-14  201953
11      2019-11-12  201953
11      2019-11-17  201953
11      2019-11-15  201953
11      2019-11-11  201953
11      2019-11-13  201953
11      2019-11-16  201953
11      2019-11-19  201954
11      2019-11-21  201954
11      2019-11-22  201954

场景: 从上面的示例 table 中,必须按周数分组并获取周的开始日期,如示例输出中所述。

示例输出:

Store   StartDate       WeekNumber
11      2019-11-11      201953
11      2019-11-19      201954

查询片段:

SELECT
Store,
#StartDate
WeekNumber 
FROM tablename 
GROUP BY WeekNumber,Store

提前致谢!

以下适用于 BigQuery 标准 SQL

#standardSQL
SELECT strore, 
  DATE_TRUNC(date, WEEK(MONDAY)) StartDate,
  EXTRACT(WEEK(MONDAY) FROM date) WeekNumber,
  COUNT(*) cnt
FROM `project.dataset.table`
GROUP BY strore, StartDate, WeekNumber   

如果应用到您问题中的示例数据,如下例

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 11 store, DATE '2019-11-14' date UNION ALL
  SELECT 11, '2019-11-12' UNION ALL
  SELECT 11, '2019-11-17' UNION ALL
  SELECT 11, '2019-11-15' UNION ALL
  SELECT 11, '2019-11-11' UNION ALL
  SELECT 11, '2019-11-13' UNION ALL
  SELECT 11, '2019-11-16' UNION ALL
  SELECT 11, '2019-11-19' UNION ALL
  SELECT 11, '2019-11-21' UNION ALL
  SELECT 11, '2019-11-22' 
)
SELECT store, 
  DATE_TRUNC(date, WEEK(MONDAY)) StartDate,
  EXTRACT(WEEK(MONDAY) FROM date) WeekNumber,
  COUNT(*) cnt
FROM `project.dataset.table`
GROUP BY store, StartDate, WeekNumber  

输出为

Row store   StartDate   WeekNumber  cnt  
1   11      2019-11-11  45          7    
2   11      2019-11-18  46          3      

UPDATE: in initial answer I've missed requirement: start date [should be taken] from the available dates for that week from table

下面的查询正是这样做的:

#standardSQL
SELECT Store, 
  MIN(Date) StartDate,
  WeekNumber,
  COUNT(*) cnt
FROM `project.dataset.table`
GROUP BY Store, WeekNumber     

如果要应用于您问题中的 [更新] 示例数据

WITH `project.dataset.table` AS (
  SELECT 11 Store, '2019-11-14' Date, 201953 WeekNumber UNION ALL
  SELECT 11, '2019-11-12', 201953 UNION ALL
  SELECT 11, '2019-11-17', 201953 UNION ALL
  SELECT 11, '2019-11-15', 201953 UNION ALL
  SELECT 11, '2019-11-11', 201953 UNION ALL
  SELECT 11, '2019-11-13', 201953 UNION ALL
  SELECT 11, '2019-11-16', 201953 UNION ALL
  SELECT 11, '2019-11-19', 201954 UNION ALL
  SELECT 11, '2019-11-21', 201954 UNION ALL
  SELECT 11, '2019-11-22', 201954 
)

结果是

Row Store   StartDate   WeekNumber  cnt  
1   11      2019-11-11  201953      7    
2   11      2019-11-19  201954      3