我有下表:
| 实体标识|时间_c|下次_时间_c|
| - ------|- ------|- ------|
| 1个|'2023年1月2日10时34分36秒'|'2023年1月10日15时12分24秒'|
| 第二章|'2023年3月1日16时10分12秒'|'2023年3月20日22时47分59秒'|
我的目标是计算time_c
和next_time_c
之间的间隔(不包括星期六和星期日)以及完整间隔。
我提出以下查询:
WITH parms (entity_id, start_date, end_date) AS
(
SELECT
entity_id,
time_c::timestamp,
next_time_c::timestamp
FROM
test_c
), weekend_days (wkend) AS
(
SELECT
SUM(CASE WHEN EXTRACT(isodow FROM d) IN (6, 7) THEN 1 ELSE 0 END)
FROM
parms
CROSS JOIN
generate_series(start_date, end_date, interval '1 day') dn(d)
)
SELECT
entity_id AS "ID",
CONCAT(
extract(day from diff), ' days ',
extract( hours from diff) , ' hours ',
extract( minutes from diff) , ' minutes ',
extract( seconds from diff)::int , ' seconds '
) AS "Duration (excluding saturday and sunday)",
justify_interval(end_date::timestamp - start_date::timestamp) AS "Duration full"
FROM (
SELECT
start_date,
end_date,
entity_id,
(end_date-start_date) - (wkend * interval '1 day') AS diff
FROM parms
JOIN weekend_days ON true
) sq;
当我的表中只有1行时,它工作得很好,当有1行以上时,结果是错误的。
1行:
| 识别号|持续时间(不包括周六和周日)|持续时间已满|
| - ------|- ------|- ------|
| 1个|6天4小时37分48秒|{"天":8,"小时":4,"分钟":37,"秒":48}|
| 识别号|持续时间(不包括周六和周日)|持续时间已满|
| - ------|- ------|- ------|
| 第二章|13天6小时37分47秒|{"天":19,"小时":6,"分钟":37,"秒":47}|
2行:
| 识别号|持续时间(不包括周六和周日)|持续时间已满|
| - ------|- ------|- ------|
| 1个|0天4小时37分48秒|{"天":8,"小时":4,"分钟":37,"秒":48}|
| 第二章|11天6小时37分47秒|{"天":19,"小时":6,"分钟":37,"秒":47}|
为什么计算多行时结果为false?
演示:https://www.db-fiddle.com/f/mDCS6cSwT1hbvit2WLYZbc/0
1条答案
按热度按时间rsaldnfx1#
应分别计算每个
entity_id
的周末天数:检查Db-Fiddle.中的完整查询
更新。上述解决方案假定
entity_id
是唯一的,因为它用于标识各个行。如果没有唯一的列,则可以使用定制函数或横向子查询。这是第二个选项:在Db<>Fiddle.中检查