我需要一些查詢簡化幫助(想避免重複等)PostgreSQL的工時簡化查詢
http://www.sqlfiddle.com/#!17/3607d/1/0
我們有2個對象:
working hours
定義:mon_from
,mon_to
,tue_from
...;如果值是null
然後不工作與它的name
和timezone
和working_hours_id
需要簡化認定,如果該辦公室的工作現在一個查詢,如果辦公室沒有工作規定的那一天
office
指定小時,然後全天候開放。使用子查詢,而不是橫向
SELECT
name,
(CURRENT_TIMESTAMP::time AT TIME ZONE timezone)::time,
(workinghours.id IS NULL) OR ((
((CURRENT_TIMESTAMP::time AT TIME ZONE timezone)::time BETWEEN mon_from AND mon_to
AND EXTRACT(isodow FROM CURRENT_TIMESTAMP AT TIME ZONE timezone) = 1) OR
((CURRENT_TIMESTAMP::time AT TIME ZONE timezone)::time BETWEEN tue_from AND tue_to
AND EXTRACT(isodow FROM CURRENT_TIMESTAMP AT TIME ZONE timezone) = 2) OR
((CURRENT_TIMESTAMP::time AT TIME ZONE timezone)::time BETWEEN wed_from AND wed_to
AND EXTRACT(isodow FROM CURRENT_TIMESTAMP AT TIME ZONE timezone) = 3) OR
((CURRENT_TIMESTAMP::time AT TIME ZONE timezone)::time BETWEEN thu_from AND thu_to
AND EXTRACT(isodow FROM CURRENT_TIMESTAMP AT TIME ZONE timezone) = 4) OR
((CURRENT_TIMESTAMP::time AT TIME ZONE timezone)::time BETWEEN fri_from AND fri_to
AND EXTRACT(isodow FROM CURRENT_TIMESTAMP AT TIME ZONE timezone) = 5) OR
((CURRENT_TIMESTAMP::time AT TIME ZONE timezone)::time BETWEEN sat_from AND sat_to
AND EXTRACT(isodow FROM CURRENT_TIMESTAMP AT TIME ZONE timezone) = 6) OR
((CURRENT_TIMESTAMP::time AT TIME ZONE timezone)::time BETWEEN sun_from AND sun_to
AND EXTRACT(isodow FROM CURRENT_TIMESTAMP AT TIME ZONE timezone) = 7)
) IS True) AS in_workinghours
FROM office
LEFT JOIN workinghours ON (workinghours.id = office.workinghours_id)
'(CURRENT_TIMESTAMP ::時間AT TIME ZONE時區):: time'可以簡化爲'(CURRENT_TIMESTAMP AT TIME ZONE時區)::時間。 – markusk