How to convert rows to columns in oracle
We have data of attendance('ab'--> absent, null--> present) on different dates(d1, d2..)
select id, value from (
select 1 id, null d1, 'ab' d2, 'ab' d3, 'ab' d4, 'ab' d5 from dual
union all
select 2 id, null d1, 'ab' d2, 'ab' d3, 'ab' d4, 'ab' d5 from dual
union all
select 3 id, null d1, 'ab' d2, 'ab' d3, 'ab' d4, 'ab' d5 from dual
union all
select 4 id, null d1, 'ab' d2, 'ab' d3, 'ab' d4, 'ab' d5 from dual
union all
select 5 id, null d1, 'ab' d2, 'ab' d3, 'ab' d4, 'ab' d5 from dual
)
UNPIVOT
(
value for value_type in(d1,d2,d3,d4,d5)
)
Later, you can take the count of it.
select id, count(value) from (
select 1 id, null d1, 'ab' d2, 'ab' d3, 'ab' d4, 'ab' d5 from dual
union all
select 2 id, null d1, 'ab' d2, 'ab' d3, 'ab' d4, 'ab' d5 from dual
union all
select 3 id, null d1, 'ab' d2, 'ab' d3, 'ab' d4, 'ab' d5 from dual
union all
select 4 id, null d1, 'ab' d2, 'ab' d3, 'ab' d4, 'ab' d5 from dual
union all
select 5 id, null d1, 'ab' d2, 'ab' d3, 'ab' d4, 'ab' d5 from dual
)
UNPIVOT
(
value for value_type in(d1,d2,d3,d4,d5)
)
group by id
Comments
Post a Comment