如何添加 SQL 以检查是否仅在最近的工作日停电?
How to add SQL to check if there's outage in the recent weekdays only?
我 运行 对企业进行审计,以查看它们是否缺少收入或客户,并生成一个数据集,该数据集由企业未通过任何 revenue/client 指标(=$0/ 0) 生成以下数据集:
Date |Business|Revenue1|Revenue2|Num_Clients1|Num_Clients2
4-18-2022 |1111 |[=11=] |[=11=] |0 |0
4-15-2022 |1111 |[=11=] |[=11=] |0 |0
4-18-2022 |2222 |[=11=] |[=11=] |0 |0
4-14-2022 |2222 |[=11=] |[=11=] |0 |0
4-15-2022 |3333 |[=11=] |[=11=] |0 |0
4-13-2022 |3333 |[=11=] |[=11=] |0 |0
今天 = 4-19-2022
如果实践在给定的工作日有一个非零度量值,那么它不会在上述数据集中有一行。
我想获取在今天之前的最后两个连续工作日(前天和昨天)指标全为零的企业列表。
在这种情况下,这将是企业 1111。企业 2222 不符合条件,因为 4-15-2022 没有通过审核。业务 3333 不符合条件,因为 4-15-2022 和 4-13-2022 不是相对于今天的最近两个连续工作日。
如何在 SQL 中完成此操作?我正在考虑检查是否 运行ning total = 2 但我不知道相对于过去两个工作日如何做。
使用您提供的示例数据。考虑下面的查询,它计算当前日期的前一天和前 2 天。过滤具有前一天和前 2 天的数据并按 Business
计数。如果一条记录得到 count = 2
那么它有前 2 天的数据。
CREATE TEMPORARY FUNCTION working_days_diff(the_date DATE, num_of_days INT64) AS
(
DATE_SUB(CASE WHEN EXTRACT(DAYOFWEEK FROM the_date) IN (2,3,4,5,6,7)
THEN
CASE WHEN (EXTRACT(DAYOFWEEK FROM the_date) - MOD(num_of_days, 5)) > 1
THEN DATE_SUB(the_date, INTERVAL MOD(num_of_days, 5) DAY)
ELSE DATE_SUB(the_date, INTERVAL (MOD(num_of_days, 5) + 2) DAY)
END
ELSE
DATE_SUB(the_date, INTERVAL (MOD(num_of_days, 5) +1) DAY)
END, INTERVAL DIV(num_of_days, 5) WEEK)
); -- I got this function here ->
with data as (
select date('2022-04-18') as Date, 1111 as Business, 0 as Revenue1, 0 as Revenue2, 0 as Num_Clients1, 0 as Num_Clients2,
union all select date('2022-04-15') as Date, 1111 as Business, 0 as Revenue1, 0 as Revenue2, 0 as Num_Clients1, 0 as Num_Clients2,
union all select date('2022-04-18') as Date, 2222 as Business, 0 as Revenue1, 0 as Revenue2, 0 as Num_Clients1, 0 as Num_Clients2,
union all select date('2022-04-14') as Date, 2222 as Business, 0 as Revenue1, 0 as Revenue2, 0 as Num_Clients1, 0 as Num_Clients2,
union all select date('2022-04-15') as Date, 3333 as Business, 0 as Revenue1, 0 as Revenue2, 0 as Num_Clients1, 0 as Num_Clients2,
union all select date('2022-04-13') as Date, 3333 as Business, 0 as Revenue1, 0 as Revenue2, 0 as Num_Clients1, 0 as Num_Clients2,
),
add_prev_days as (
select *,
date('2022-04-19') as today, -- replace with current_date()
working_days_diff(DATE('2022-04-19'), 1) as prev_day, -- replace with current_date()
working_days_diff(DATE('2022-04-19'), 2) as prev_2_day, -- replace with current_date()
from data
),
add_count as (
select *,
count(Business) over (partition by Business) as count_bus
from add_prev_days
where (prev_day = Date or prev_2_day = Date)
)
select Date,
Business,
Revenue1,
Revenue2,
Num_Clients1,
Num_Clients2
from add_count
where count_bus = 2
示例输出:
注意:我将“2022-04-19”硬编码为当前日期,以便查询正常运行。将此值替换为 current_date()
以适合您的用例。
我 运行 对企业进行审计,以查看它们是否缺少收入或客户,并生成一个数据集,该数据集由企业未通过任何 revenue/client 指标(=$0/ 0) 生成以下数据集:
Date |Business|Revenue1|Revenue2|Num_Clients1|Num_Clients2
4-18-2022 |1111 |[=11=] |[=11=] |0 |0
4-15-2022 |1111 |[=11=] |[=11=] |0 |0
4-18-2022 |2222 |[=11=] |[=11=] |0 |0
4-14-2022 |2222 |[=11=] |[=11=] |0 |0
4-15-2022 |3333 |[=11=] |[=11=] |0 |0
4-13-2022 |3333 |[=11=] |[=11=] |0 |0
今天 = 4-19-2022
如果实践在给定的工作日有一个非零度量值,那么它不会在上述数据集中有一行。
我想获取在今天之前的最后两个连续工作日(前天和昨天)指标全为零的企业列表。
在这种情况下,这将是企业 1111。企业 2222 不符合条件,因为 4-15-2022 没有通过审核。业务 3333 不符合条件,因为 4-15-2022 和 4-13-2022 不是相对于今天的最近两个连续工作日。
如何在 SQL 中完成此操作?我正在考虑检查是否 运行ning total = 2 但我不知道相对于过去两个工作日如何做。
使用您提供的示例数据。考虑下面的查询,它计算当前日期的前一天和前 2 天。过滤具有前一天和前 2 天的数据并按 Business
计数。如果一条记录得到 count = 2
那么它有前 2 天的数据。
CREATE TEMPORARY FUNCTION working_days_diff(the_date DATE, num_of_days INT64) AS
(
DATE_SUB(CASE WHEN EXTRACT(DAYOFWEEK FROM the_date) IN (2,3,4,5,6,7)
THEN
CASE WHEN (EXTRACT(DAYOFWEEK FROM the_date) - MOD(num_of_days, 5)) > 1
THEN DATE_SUB(the_date, INTERVAL MOD(num_of_days, 5) DAY)
ELSE DATE_SUB(the_date, INTERVAL (MOD(num_of_days, 5) + 2) DAY)
END
ELSE
DATE_SUB(the_date, INTERVAL (MOD(num_of_days, 5) +1) DAY)
END, INTERVAL DIV(num_of_days, 5) WEEK)
); -- I got this function here ->
with data as (
select date('2022-04-18') as Date, 1111 as Business, 0 as Revenue1, 0 as Revenue2, 0 as Num_Clients1, 0 as Num_Clients2,
union all select date('2022-04-15') as Date, 1111 as Business, 0 as Revenue1, 0 as Revenue2, 0 as Num_Clients1, 0 as Num_Clients2,
union all select date('2022-04-18') as Date, 2222 as Business, 0 as Revenue1, 0 as Revenue2, 0 as Num_Clients1, 0 as Num_Clients2,
union all select date('2022-04-14') as Date, 2222 as Business, 0 as Revenue1, 0 as Revenue2, 0 as Num_Clients1, 0 as Num_Clients2,
union all select date('2022-04-15') as Date, 3333 as Business, 0 as Revenue1, 0 as Revenue2, 0 as Num_Clients1, 0 as Num_Clients2,
union all select date('2022-04-13') as Date, 3333 as Business, 0 as Revenue1, 0 as Revenue2, 0 as Num_Clients1, 0 as Num_Clients2,
),
add_prev_days as (
select *,
date('2022-04-19') as today, -- replace with current_date()
working_days_diff(DATE('2022-04-19'), 1) as prev_day, -- replace with current_date()
working_days_diff(DATE('2022-04-19'), 2) as prev_2_day, -- replace with current_date()
from data
),
add_count as (
select *,
count(Business) over (partition by Business) as count_bus
from add_prev_days
where (prev_day = Date or prev_2_day = Date)
)
select Date,
Business,
Revenue1,
Revenue2,
Num_Clients1,
Num_Clients2
from add_count
where count_bus = 2
示例输出:
注意:我将“2022-04-19”硬编码为当前日期,以便查询正常运行。将此值替换为 current_date()
以适合您的用例。