我必须开发一份报告,需要将员工表和保留表合并为一个
员工表
employeecode calldate doctorvisit E001 2017/08/03 Doc001 E002 2017/08/05 Doc002 E003 2017/08/10 Doc003
离开表
employeecode leavedate leavetype E001 2017/08/09 casualleave E002 2017/08/17 sickleave E003 2017/08/15 casualleve
我的输出表
employeecode Dates fulldetails E001 2017/08/03 1 E001 2017/08/09 casualleave E002 2017/08/05 1 E002 2017/08/17 sickleave E003 2017/08/10 1 E003 2017/08/15 casualleve
查询
select emp.employeecode,
case when emp.startdate = emp.startdate then emp.startdate = emp.startdate
when lea.calldate = lea.calldate then lea.calldate = lea.calldate
else null
end as dates,
case when emp.startdate = emp.startdate then count(doctorvisit)
when lea.calldate =lea.calldate then lea.leavetype
end as fulldetails
from employee emp
inner join leave lea on lea.employeecode = emp.employeecode
group by emp.employeecode,emp.calldate,lea.leavedate
当我执行此查询时,它只显示员工表详细信息而不是保留详细信息,但我需要两个详细信息。
所以请帮我解释一下这段代码。非常感谢!
答案 0 :(得分:0)
这是一种可能性:
drop table if exists employee;
create table employee(employeecode text, calldate date, doctorvisit text);
insert into employee values
('E001','2017/08/03','Doc001'),
('E002','2017/08/05','Doc002'),
('E003','2017/08/10','Doc003');
drop table if exists leave;
create table leave(employeecode text, leavedate date, leavetype text);
insert into leave values
('E001','2017/08/09','casualleave'),
('E002','2017/08/17','sickleave'),
('E003','2017/08/15','casualleve');
select * from
(
select employeecode,calldate as dates,'1' as fulldetails from employee
union
select employeecode,leavedate as dates,leavetype as fulldetails from leave
) t
order by employeecode,dates;