我有一个表EMPLOYEE
,它包含这样的数据:
EID JOB JOINING_DATE
------ ---- -----------
1 SE 2015-01-01 00:00:00.0
2 SW 2015-01-02 00:00:00.0
3 SD 2015-01-03 00:00:00.0
4 SX 2015-01-04 00:00:00.0
5 SP 2015-01-05 00:00:00.0
6 SV 2015-01-06 00:00:00.0
7 SM 2015-01-07 00:00:00.0
8 SE 2015-01-04 00:00:00.0
9 SW 2015-01-03 00:00:00.0
10 SD 2015-01-01 00:00:00.0
11 SX 2015-01-03 00:00:00.0
12 SP 2015-01-03 00:00:00.0
13 SV 2015-01-02 00:00:00.0
8 SE 2015-01-04 00:00:00.0
9 SW 2015-01-03 00:00:00.0
10 SD 2015-01-01 00:00:00.0
11 SX 2015-01-03 00:00:00.0
12 SP 2015-01-03 00:00:00.0
13 SV 2015-01-02 00:00:00.0
我需要像这样输出:
EID JOB 01-01-15 02-01-15 03-01-15
1 SE 1 0 0
2 SW 0 0 0
.
.
.
13 SV 0 2 0
如何做到这一点?
答案 0 :(得分:1)
请注意以下几点。
我假设不同员工的开斋节和工作是相同的,否则你的逻辑会失败。
with tbl1 as(
select emp.*,
case when joining_date = to_date('2015-01-01','YYYY-MM-DD') then 1 else 0 end as date_01_01_15,
case when joining_date = to_date('2015-01-02','YYYY-MM-DD') then 1 else 0 end as date_02_01_15,
case when joining_date = to_date('2015-01-03','YYYY-MM-DD') then 1 else 0 end as date_03_01_15
from EMPLOYEE emp)
select tbl1.eid, tbl1.job,
sum(date_01_01_15) as date_01_01_15,
sum(date_02_01_15) as date_02_01_15,
sum(date_03_01_15) as date_03_01_15
from tbl1
group by tbl1.eid,tbl1.job
注意:我将日期格式设为YYYY-MM-DD。您可以根据日期格式进行更改。 您还可以扩展case和sum语句以添加其他列。