drop table userinfo;
drop table checkinfo;
create table userinfo(id int,username varchar2(32),u_id int);
create table checkinfo(id int,checktype varchar2(32),u_id int);
insert into userinfo values(1,'张三',1);
insert into userinfo values(2,'李四',2);
insert into userinfo values(3,'王五',3);
insert into userinfo values(4,'赵六',4);
insert into checkinfo values(1,'正常',1);
insert into checkinfo values(2,'正常',1);
insert into checkinfo values(3,'病假',1);
insert into checkinfo values(4,'正常',2);
insert into checkinfo values(5,'事假',2);
insert into checkinfo values(6,'病假',2);
insert into checkinfo values(7,'正常',2);
insert into checkinfo values(8,'病假',2);
insert into checkinfo values(9,'正常',3);
insert into checkinfo values(10,'事假',3);
insert into checkinfo values(11,'病假',3);
insert into checkinfo values(12,'正常',3);
insert into checkinfo values(13,'正常',3);
insert into checkinfo values(14,'正常',3);
insert into checkinfo values(15,'正常',3);
insert into checkinfo values(16,'病假',3);
insert into checkinfo values(17,'正常',4);
insert into checkinfo values(18,'事假',4);
insert into checkinfo values(19,'病假',4);
insert into checkinfo values(20,'正常',4);
insert into checkinfo values(21,'事假',4);
insert into checkinfo values(22,'病假',4);
insert into checkinfo values(23,'事假',4);
insert into checkinfo values(24,'病假',4);
commit;
我现在有两张表:
两张表通过 u_id 关联
怎么查询出每个用户的某个月出勤的情况: :
比如说,1 月份,正常出勤多少天,事假多少天,病假多少天
例如:
用户 id 正常(天数) 事假(天数) 病假(天数)
1 2 0 1
(1)
select u.u_id,
sum(decode(checktype, '正常', 1, 0)) "正常",
sum(decode(checktype, '事假', 1, 0)) "事假",
sum(decode(checktype, '病假', 1, 0)) "病假"
from userinfo u
inner join checkinfo c
on (u.u_id = c.u_id)
group by u.u_id;
U_ID 正常 事假 病假
--------------------------------------- ---------- ---------- ----------
1 2 0 1
2 2 1 2
4 2 3 3
3 5 1 2
(2)
select u.u_id,
nvl(max(decode(checktype, '正常', cnt)),0)"正常",
nvl(max(decode(checktype, '事假', cnt)),0) "事假",
nvl(max(decode(checktype, '病假', cnt)),0) "病假"
from userinfo u
inner join (select u_id, checktype, count(*) cnt
from checkinfo
group by u_id, CHECKTYPE) c
on (u.u_id = c.u_id) group by u.u_id order by 1;
U_ID 正常 事假 病假
--------------------------------------- ---------- ---------- ----------
1 2 0 1
2 2 1 2
3 5 1 2
4 2 3 3
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31402276/viewspace-2127253/
,如需转载,请注明出处,否则将追究法律责任。