有一个考勤表,结构如下,(数据库为oracle 或postgresql,下同)
create table t_attendance(
n_id num, -- 考勤编号,关键字,自增
n_user number, -- 用户编号
s_type varchar2(1), --考勤类型 1=上午签到 2=上午签退 3=下午签到 4=下午签退
d_time timestamp, --签到或签退时间
);
员工可能有多次签到,如某个员工,上午签到了两次,则以第一次为准,如果上午签退了两次,
则以第二次为准,下午同样如此。
现给定一个时间段 d_beg 和 d_end ,请用一条sql语句,查询出这个时间段内每为员工的工作时间总数(小时)
select * from t_attendance;
select * from t_attendance where timestamp>=d_beg and timestamp<d_end;
select n_user ,
min(decode(s_type,'1',d_time)) time1,
max(decode(s_type,'2',d_time)) time2,
min(decode(s_type,'3',d_time)) time3,
max(decode(s_type,'4',d_time)) time4,
from t_attendance group by n_user,s_type
with t1 as (
select
数据库·拓展1
最新推荐文章于 2021-09-01 10:11:01 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)