[英]Return in only one table info from two tables into rows when info is in columns
在 PostgreSQL 中,我有 2 個帶有字段的表:
id
(自動數字)、 employee_code
(varchar(6))、 working_date
(日期)、 working_hour
(時間)、id
(自動數字), employee_code
(varchar(6)), attendance_date
日期(日期), attendance_hour
時間(時間),數據示例:
工作日期
ID | employee_code | working_date | working_hour
1 | 12345 | 2015-07-09 | 08:00
2 | 12345 | 2015-07-09 | 13:00
3 | 12345 | 2015-07-09 | 14:00
4 | 12345 | 2015-07-09 | 17:00
5 | 12345 | 2015-07-10 | 08:00
6 | 12345 | 2015-07-10 | 13:00
7 | 12345 | 2015-07-10 | 14:00
8 | 12345 | 2015-07-10 | 17:00
9 | 12345 | 2015-07-11 | 08:00
10 | 12345 | 2015-07-11 | 13:00
11 | 12345 | 2015-07-11 | 14:00
12 | 12345 | 2015-07-11 | 17:00
13 | 12345 | 2015-07-12 | 08:00
14 | 12345 | 2015-07-12 | 13:00
15 | 12345 | 2015-07-12 | 14:00
16 | 12345 | 2015-07-12 | 17:00
17 | 12345 | 2015-07-13 | 08:00
18 | 12345 | 2015-07-13 | 13:00
19 | 12345 | 2015-07-13 | 14:00
20 | 12345 | 2015-07-13 | 17:00
出勤率
ID | employee_code | attendance_date | attendance_hour
1 | 12345 | 2015-07-09 | 07:56:53
2 | 12345 | 2015-07-09 | 10:33:31
3 | 12345 | 2015-07-09 | 13:00:42
4 | 12345 | 2015-07-09 | 13:00:47
5 | 12345 | 2015-07-09 | 13:30:21
6 | 12345 | 2015-07-09 | 17:00:01
7 | 12345 | 2015-07-10 | 07:48:35
8 | 12345 | 2015-07-10 | 12:15:20
9 | 12345 | 2015-07-10 | 13:58:42
10 | 12345 | 2015-07-10 | 17:02:00
11 | 12345 | 2015-07-11 | 08:06:46
12 | 12345 | 2015-07-11 | 12:00:01
13 | 12345 | 2015-07-11 | 13:52:01
14 | 12345 | 2015-07-11 | 17:05:08
15 | 12345 | 2015-07-12 | 07:55:02
16 | 12345 | 2015-07-12 | 12:03:22
17 | 12345 | 2015-07-12 | 13:37:40
18 | 12345 | 2015-07-12 | 17:05:01
19 | 12345 | 2015-07-13 | 07:54:25
20 | 12345 | 2015-07-13 | 10:44:15
21 | 12345 | 2015-07-13 | 13:59:21
22 | 12345 | 2015-07-13 | 17:01:17
在“考勤”表中有一些重復的行,因為員工多次輸入考勤。 例如在 2015-07-09 有 2 個出勤時間(13:00:42, 13:00:47)到了出去吃午飯的時間。 在這種情況下,我應該只得到兩條記錄中的一條。
2015-07-09 的另一個案例是 10:33:31。 當員工請求離開工作的許可,然后在這種情況下在 13:00:42 / 13:00:47 返回時記錄。
有沒有辦法只使用純 SQL 查詢(可能是某種類型的子查詢)在一個表中獲取working_date
、 working_hour
及其各自的attendance_hour
時間?
例子:
ID | employee_code | working_date | working_hour1 | attendance_time_1 | working_hour2 | attendance_time_2 | working_hour3 | attendance_time_3 | working_hour4 | attendance_time_4
1 | 12345 | 2015-07-09 | 08:00 | 07:56:53 | 13:00:00 | 13:00:42 or 13:00:47 | 14:00 | 13:30:21 | 17:00 | 17:00:01
2 | 12345 | 2015-07-10 | 08:00 | 07:48:35 | 13:00:00 | 12:15:20 | 14:00 | 13:58:42 | 17:00 | 17:02:00
3 | 12345 | 2015-07-11 | 08:00 | 08:06:46 | 13:00:00 | 12:00:01 | 14:00 | 13:52:01 | 17:00 | 17:05:08
4 | 12345 | 2015-07-12 | 08:00 | 07:55:02 | 13:00:00 | 12:03:22 | 14:00 | 13:37:40 | 17:00 | 17:05:01
5 | 12345 | 2015-07-13 | 08:00 | 07:54:25 | 13:00:00 | 10:44:15 | 14:00 | 13:59:21 | 17:00 | 17:01:17
如果無法通過純 SQL 查詢獲得它,那么如何通過 PL/PGSQL 實現?
目前我用 PHP 制作它是這樣的:
working_date
表中查詢employee_code
和working_date
字段。 此查詢在 2 個日期之間運行: from_date
, to_date
。working_hour1
、 working_hour2
、 working_hour3
、 working_hour4
。 對每一行運行一個 SQL 查詢。 對於這個查詢,我向它發送了employee_code
和working_date
參數。working_hour
,我都會使用參數對“attendance_date”表運行查詢: employee_code
、 working_date
和working_hour
。 它返回每個working_hour
的attendance_hour
時間。這種方式(使用嵌套的“for 語句”從 PHP 調用 SELECTS)對於獲取和顯示信息來說太慢了。 我在執行它時看到了進程,進程占用了 100% 的 CPU。
您可以按日期加入這些表,並通過按日期和員工代碼分組將出勤時間聚合到一個數組中,有點像這樣:
SELECT
w.employee_code,
w.working_date,
array_agg(distinct(w.working_hour)) working_hours,
array_agg(distinct(a.attendance_hour)) attendance_hours
FROM Working_date w
LEFT JOIN attendance a
ON (w.working_date = a.attendance_date)
GROUP BY w.working_date, w.employee_code
ORDER BY w.working_date
您可以使用 postgres 的unnest()函數來取消嵌套這些數組,但它會將它們放入新行而不是列中。 將它們放入不同的列是很困難的,因為這些數組的長度可能不同,並且所有行都必須具有相同的列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.