larisboy
2022-06-17 14:54:26 +08:00
SELECT
'2022-06-18 10:10:10' AS d1,
'2022-06-20 10:10:10' AS d2,
toDate(d1) AS d1_date,
toDate(d2) AS d2_date,
addDays(d1_date, 1) AS d1_next_day,
toInt8('1') AS week_start_day,
toInt8('5') AS week_end_day,
arrayMap(x -> (x + d1_next_day), arrayFilter(x -> (week_start_day > toDayOfWeek(addDays(d1_next_day, x)) OR toDayOfWeek(addDays(d1_next_day, x)) > week_end_day),
CASE WHEN (d2_date - d1_next_day) < 0 THEN [] ELSE range(abs(d2_date - d1_next_day)) END
)) as nonworkdays,
dateDiff('minute', toDateTime(d1), toDateTime(d2)) - 24 * 60 * length(nonworkdays) AS all_minture,
CASE WHEN (week_start_day > toDayOfWeek(d1_date) OR toDayOfWeek(d1_date) > week_end_day) THEN dateDiff('minute', toDateTime(d1), addDays(toStartOfDay(d1_date), 1)) ELSE 0 END AS d1_minture,
CASE WHEN (week_start_day > toDayOfWeek(d2_date) OR toDayOfWeek(d2_date) > week_end_day) THEN dateDiff('minute', toStartOfDay(d2_date), toDateTime(d2)) ELSE 0 END AS d2_minture,
all_minture - d1_minture - d2_minture AS work_minture
2022-06-18 10:10:10 2022-06-20 10:10:10 2022-06-18 2022-06-20 2022-06-19 1 5 ['2022-06-19'] 1440 830 0 610
已用 SQL 实现,欢迎指出 BUG