我有这张出勤表:
days emp1 emp2 emp3 emp4
1 P P A P
2 P P P P
3 P A P L
4 A P P A
我希望在Oracle中转置它。我想要一个像这样的输出:
1 2 3 4
emp1 P P P A
emp2 P P A P
emp3 A P P P
emp4 P P L A
答案 0 :(得分:0)
我认为我设法用一个枢轴操作员做你想做的事情(这件事看起来太复杂了,无法轻松实现):
with w as
(
select * from
(
select t.days, t.emp1, t.emp2, t.emp3, t.emp4
from my_table t
)
pivot (max(emp1) emp1, max(emp2) emp2, max(emp3) emp3, max(emp4) emp4 for (days) in (1 as a1, 2 as a2, 3 as a3, 4 as a4))
)
select w.a1_emp1 d1, w.a2_emp1 d2, w.a1_emp1 d3, w.a1_emp1 d4 from w
union all
select w.a1_emp2 d1, w.a2_emp2 d2, w.a3_emp2 d3, w.a4_emp2 d4 from w
union all
select w.a1_emp3 d1, w.a2_emp3 d2, w.a3_emp3 d3, w.a4_emp3 d4 from w
union all
select w.a1_emp4 d1, w.a2_emp4 d2, w.a3_emp4 d3, w.a4_emp4 d4 from w
;
你得到:
D1 D2 D3 D4
P P P P
P P A P
A P P P
P P L A