Please help me to the SQL Oracle 9i, convert columns to rows in group by ID.
I can't convert using UNPIVOT in oracle 9i. And I will not use UNION ALL, it's too much code to use later.
From this list:
Table:
Employees
ID | DATA 1 | DATA 2 | DATA 3 | DATA 4 |
----------------------------------------------------------
1 | 0 | 1 | 3 | 2 |
2 | 1 | 0 | 3 | 0 |
Result
ID | SOURCE | DATA |
--------------------------------------
1 | DATA 2 | 1 |
1 | DATA 4 | 2 |
1 | DATA 3 | 3 |
2 | DATA 1 | 1 |
2 | DATA 3 | 3 |
Here's a way of doing a manual unpivot:
This uses a dummy "table" of 4 rows (since you're unpivoting 4 columns) to cross join to the main table. Then it's just a matter of outputting data1 for row 1, data2 for row 2, etc.
I've used the case statement again in the where clause, but you could do the query without the predicate in a subquery and then add the filter in the outer query if you like (e.g.
select id, source, data from (select e.id, case .... ) where data > 0
)