简体   繁体   English

Bigquery 计算当前日期的星期几

[英]Bigquery Count Days of Week in Current Date

how do I calculate days of week in this current date?如何计算当前日期的星期几?

if I use如果我使用

select 
date_trunc(salesdate, month)mt,  
count(distinct case when extract(dayofweek from salesdate) in (6,7,1) then salesdate end) weekend, 
count(distinct case when extract(dayofweek from salesdate) not in (6,7,1) then salesdate end) weekday

from `my_table` group by 1

salesdate is the date I got from November-now. salesdate 是我从 11 月到现在的日期。 and the answer I got和我得到的答案

在此处输入图像描述

since this month is not completed yet.因为这个月还没有完成。 the numbers of weekend and weekday is depend on the current date周末和工作日的数量取决于当前日期

what I expected is will be我期望的是

mt           weekend   weekday
2023-01-01   13        18
2022-12-01   14        17

Can you try this:你能试试这个吗:

with my_table as (
select * 
from unnest(generate_date_array('2022-11-01', current_date(), interval 1 day))salesdate
)

select mt
,count(distinct case when extract(dayofweek from salesdate) in (6,7,1) then salesdate end) weekend
,count(distinct case when extract(dayofweek from salesdate) not in (6,7,1) then salesdate end) weekday
 from (
  select 
  date_trunc(salesdate, month)mt,
  LAST_DAY(salesdate, MONTH)ld
  from my_table
  group by mt,ld
  ),
unnest(generate_date_array(mt, ld, interval 1 day))salesdate
group by mt

Query results:查询结果:

在此处输入图像描述

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

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