使用出勤记录和休息日表。如何显示现在,缺席和休息日?

时间:2018-10-31 06:48:18

标签: sql postgresql

我从设备的指纹日志中获取了此文件:

Id User_id      CheckTime
--------------------------
1   152      2018-07-17 09:38:03
2   184      2018-07-17 16:56:43
3   152      2018-07-17 16:57:18
4   165      2018-07-17 16:57:43
5   70       2018-07-17 16:57:59
6   134      2018-07-17 16:58:28
7   276      2018-07-17 16:59:04
8   278      2018-07-17 16:59:05
9   271      2018-07-17 16:59:10
10  268      2018-07-17 16:59:13
11  284      2018-07-17 16:59:16
12  364      2018-07-17 16:59:35
13  19       2018-07-17 16:59:38
14  381      2018-07-17 17:01:12
15  73       2018-07-17 17:12:31
16  126      2018-07-17 17:12:36
17  382      2018-07-17 17:13:50
18  53       2018-07-18 06:34:13
19  284      2018-07-18 08:05:17

如何在Postgres查询中进行查询以提取数据,如下所示:

User_id  Check_Date   DAY       TimeIN    TimeOUT   Hours   status
-----------------------------------------------------------------
152      2018-07-17   Tuesday    09:38:03 16:56:43  7.8     present
152      2018-07-18   Wednesday                              Absent

使用此查询获取结果

with from_thru as (
  select
    min (checktime)::date as from_date, 
    max (checktime)::date as thru_date
  from attendance_FHLHR
),
users as (
  select distinct userid, name from attendance_FHLHR
)
select
  gs.date::date, u.userid, u.name, to_char(gs.date::date, 'day') days,
  min (a.checktime) as TimeIn,
  max (a.checktime) as TimeOut,
uso.off_days as offf,
  extract (epoch from max (a.checktime) - min (a.checktime))/3600 as Hours,
  case
    when min (a.checktime) is null then 'Absent'
    when count (1) = 1 then 'Half Day' 
    else 'Present'
  end as status
from
  from_thru
  cross join generate_series (from_date, thru_date, interval '1 day') gs (date)
  cross join users u
  left join attendance_FHLHR a on
    a.checktime::date = gs.date and
    a.userid = u.userid
   left join users_staffuser us on a.emp_id::varchar = us.emp_id 
   left join users_staffuseroffdays uso on uso.staff_user_id = us.id
and us.emp_id is not null
group by
  gs.date, u.userid, u.name, uso.off_days
order by u.name ASC

现在我有另一个表,我可以在此关闭用户休假的商店

name   userid   days
---------------------------
adnan   152     monday
kasih   182     tuesday

如何在用户休息日添加状态列。 id用户是否没有休息日检查数据?

User_id  Check_Date   DAY       TimeIN    TimeOUT   Hours   status
-----------------------------------------------------------------
152      2018-07-17   Tuesday    09:38:03 16:56:43  7.8     present
152      2018-07-18   Wednesday                             OFF DAY
152      2018-07-19   thursday                              Absent

1 个答案:

答案 0 :(得分:0)

@ S-MAN我解决了。雅虎

ld