特定のタイムスタンプ範囲の場合:
1 時間の単位を考慮します。分数は無視します。式は次のとおりです:SELECT count(*) AS work_hours FROM generate_series(timestamp '2013-06-24 13:30', timestamp '2013-06-24 15:29' - interval '1h', interval '1h') h WHERE EXTRACT(ISODOW FROM h) < 6 AND h::time >= '08:00' AND h::time <= '14:00';
テーブル全体の場合:
-- Table Creation and Data Insertion CREATE TABLE t (t_id int PRIMARY KEY, t_start timestamp, t_end timestamp); INSERT INTO t VALUES (1, '2009-12-03 14:00', '2009-12-04 09:00') , (2, '2009-12-03 15:00', '2009-12-07 08:00') , (3, '2013-06-24 07:00', '2013-06-24 12:00') , (4, '2013-06-24 12:00', '2013-06-24 23:00') , (5, '2013-06-23 13:00', '2013-06-25 11:00') , (6, '2013-06-23 14:01', '2013-06-24 08:59'); -- Main Query SELECT t_id, count(*) AS work_hours FROM ( SELECT t_id, generate_series(t_start, t_end - interval '1h', interval '1h') AS h FROM t ) sub WHERE EXTRACT(ISODOW FROM h) < 6 AND h::time >= '08:00' AND h::time <= '14:00' GROUP BY 1 ORDER BY 1;
-- Precision with 5-minute Slices SELECT t_id, count(*) * interval '5 min' AS work_interval FROM ( SELECT t_id, generate_series(t_start, t_end - interval '5 min', interval '5 min') AS h FROM t ) sub WHERE EXTRACT(ISODOW FROM h) < 6 AND h::time >= '08:00' AND h::time <= '14:55' -- 15.00 - interval '5 min' GROUP BY 1 ORDER BY 1;
Postgres 8.4 :
以上がPostgreSQL で 2 つの日付の間の労働時間を計算するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。