【发布时间】:2021-06-15 04:07:58
【问题描述】:
我当前的 SQL 根据 Workdays Month 和 YTD 以及 FTE Month 和 YTD 的一些计算返回以下结果。
| Version | CALENDAR_MONTH | CALENDAR_DATE | AMOUNT | Workdays_Month | FTE_MONTH | Workdays_YTD | FTE_YTD | Correct FTE_YTD |
|---|---|---|---|---|---|---|---|---|
| ACTA | 201907 | 31/07/2019 | 31062.9 | 23 | 183.75 | 23 | 183.75 | 183.75 |
| ACTA | 201908 | 31/08/2019 | 30509.1 | 22 | 188.68 | 45 | 92.24 | 275.99 |
| ACTA | 201909 | 30/09/2019 | 28712.49 | 21 | 186.02 | 66 | 59.19 | 335.18 |
| ACTA | 201910 | 31/10/2019 | 47656.37 | 23 | 281.91 | 89 | 72.85 | 408.03 |
| ACTA | 201911 | 30/11/2019 | 32033.98 | 21 | 207.54 | 110 | 39.62 | 447.65 |
| ACTA | 201912 | 31/12/2019 | 32788.36 | 22 | 202.77 | 132 | 33.80 | 481.45 |
| ACTA | 202001 | 31/01/2020 | 31983.77 | 23 | 189.20 | 155 | 28.07 | 509.52 |
| ACTA | 202002 | 29/02/2020 | 37647.87 | 20 | 256.11 | 175 | 29.27 | 538.79 |
| ACTA | 202003 | 31/03/2020 | 35203.83 | 22 | 217.71 | 197 | 24.31 | 563.11 |
| ACTA | 202004 | 30/04/2020 | 35476.7 | 22 | 219.40 | 219 | 22.04 | 585.15 |
| ACTA | 202005 | 31/05/2020 | -1083.81 | 21 | -7.02 | 240 | -0.61 | 584.53 |
| ACTA | 202006 | 30/06/2020 | 20766.45 | 22 | 128.43 | 262 | 10.78 | 595.32 |
我想根据 CALENDAR_MONTH CALENDAR_DATE 将我的 FTE_YTD 计算更新为上个月/秒的累积总和
注意:财政年度为 1/07 至 30/6
My current Calculations
(select count(*) from unnest(generate_date_array(date_trunc(CALENDAR_DATE, month), last_day(CALENDAR_DATE, month ))) day
where not extract(dayofweek from day) in (1, 7)) as Workdays_Month,
(SELECT SAFE_DIVIDE(AMOUNT,(SAFE_MULTIPLY((select count(*) from unnest(generate_date_array(date_trunc(CALENDAR_DATE, month), last_day(CALENDAR_DATE, month ))) day
where not extract(dayofweek from day) in (1, 7)),7.35)))) as FTE_MONTH,
(select count(*)
from unnest([struct(extract(year from CALENDAR_DATE) as year, extract(month from CALENDAR_DATE) as month)]),
unnest(generate_date_array(if(month < 7, date(year - 1, 7, 1), date(year, 7, 1)), last_day(date(year, month, 1)))) day
where not extract(dayofweek from day) in (1, 7)) as Workdays_YTD,
(SELECT SAFE_DIVIDE(AMOUNT,(SAFE_MULTIPLY((select count(*)
from unnest([struct(extract(year from CALENDAR_DATE) as year, extract(month from CALENDAR_DATE) as month)]),
unnest(generate_date_array(if(month < 7, date(year - 1, 7, 1), date(year, 7, 1)), last_day(date(year, month, 1)))) day
where not extract(dayofweek from day) in (1, 7)),7.35)))) as FTE_YTD,
我想根据 CALENDAR_MONTH CALENDAR_DATE 将我的 FTE_YTD 计算更新为上个月/秒的累积总和
【问题讨论】:
-
请出示您的输入数据!
-
你的意思是计算公式吗? FTE_MONTH = sum(Amount)/max(workDays_Month*7.35) 和 FTE_YTD = Sum(YTD_Amount)/max(workDays_YTD*7.35) ......所以我想弄清楚如何计算 YTD Amount
-
我的意思是您产生显示结果的初始数据。但没关系 - 我认为我实际上不需要它:o)
标签: sql google-bigquery