Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL Query: Switching Rows to Columns dynamically.
"Anil G" <anil.gothal_at_gmail.com> a écrit dans le message de news: 1161038711.676290.252870_at_i3g2000cwc.googlegroups.com...
| Guys:
|
| I am looking to develop SQL query which will switch the rows to
| Columns. I was trying with DECODE statements but that leads to
| hardcoding of types. Any one has any clues to develop dynamic logic to
| convert rows to columns. Following is detailed information about
| problem.
|
| Thanks in advance,
| Anil G
|
| ----------------------------------------- problem statement
| ---------------------------------------------------------------
| type_tbl
| + name
|
| data_tbl
| + org
| + type_name
| + data_value (FK to type_tbl)
|
| -----
| Data layouts: type_tbl, user can keep adding types from the
| application.
|
| type_tbl
| --------
| T-1
| T-2
| T-3
| T-4
| ..
| ..
|
| data_tbl
| -------
| org type_name data_value
| ---- --------- -----------
| O-1 T-1 100
| O-1 T-2 30
| O-1 T-3 45
| O-2 T-1 100
| O-2 T-4 30
| O-3 T-1 45
|
|
| Desired Output:
| ---------------------------------------------------
|
| org T-1 T-2 T-3 T-4 .....
| --- --- --- --- --- --
| O-1 100 30 45
| O-2 100 0 0 25
| O-3 45
|
| --------------------------------------------------------- Ends
| -------------------------------------------------------------
|
I posted a solution for such a problem on Ask Tom (it was about students and classes but the principle is the same): http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:52266643928180#52284733269998
Regards
Michel Cadot
Received on Tue Oct 17 2006 - 11:04:25 CDT
![]() |
![]() |