Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 238 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 437 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
How a row data is converted into a column data

703280
Member Posts: 2
How the data in any row of a table is converted into a column data. (the query to do that)
e.g.
id name city
a11 Ram Kolhapur
b22 Manu Satara
the result should be
a11 b22
ram Manu
kolhapur Satara
Is it possible , if yes how ? boz this has been asked in an interview?
Thanks
Prithviraj
e.g.
id name city
a11 Ram Kolhapur
b22 Manu Satara
the result should be
a11 b22
ram Manu
kolhapur Satara
Is it possible , if yes how ? boz this has been asked in an interview?
Thanks
Prithviraj
Tagged:
Answers
-
user10643728 wrote:This is called a Pivot.
How the data in any row of a table is converted into a column data. (the query to do that)
e.g.
id name city
a11 Ram Kolhapur
b22 Manu Satara
the result should be
a11 b22
ram Manu
kolhapur Satara
Is it possible , if yes how ? boz this has been asked in an interview?
Thanks
Prithviraj
In 11g there is specifically a PIVOT keyword for use in SQL. In 10g or below there are various techniques using decode or case statements etc. to achieve the same.
However, when pivoting data it is necessary to know how many columns (or the maximum expected columns) will be produced by the query. You can't easily have it completely dynamic to pivot any number of rows to any number of columns.
Ideally, it should the the front end (client) reporting tool that does this sort of work as Report writers are generally very good at pivoting data and formatting it to fit on report pages, and they often have the ability to deal with a dynamic number of resultant columns, unlike SQL. -
If someone ask stuff like this, i will ask them why they want me to do such crazy thing. Pivot is a presentation layer work. Why do they ask DB guy to do it
But any way here you go.with t as ( select 'a11' id, 'Ram' name, 'Kolhapur' city from dual union all select 'b22', 'Manu', 'Satara' from dual ) select max(decode(rno,1,decode(rno1,1,id,2,name,3,city))), max(decode(rno,2,decode(rno1,1,id,2,name,3,city))) from (select rownum rno, id, name, city from t) cross join (select level rno1 from dual connect by level <= 3) group by rno1
-
I like Pivot and UnPivot B-)
create table pivUnPiv(id,name,city) as select 'a11','Ram' ,'Kolhapur' from dual union all select 'b22','Manu','Satara' from dual; select * from (select Row_Number() over(order by id) as rn, id,name,city from pivUnPiv) unpivot(Vals for Base in(ID,NAME,CITY)) Pivot(max(Vals) for rn in(1 as Col1, 2 as Col2)) order by decode(Base,'ID',1,'NAME',2,3); BASE COL1 COL2 ---- -------- ------ ID a11 b22 NAME Ram Manu CITY Kolhapur Satara
evaluated order of Pivot and UnPivot in select statement
995121 -
I like model clause ,too :8}
select Col1,Col2 from pivUnPiv model dimension by(Row_Number() over(order by id) as soeji) measures(id,name,city, cast(null as varchar2(10)) as COL1, cast(null as varchar2(10)) as COL2) rules( Col1[1] = ID[1], Col2[1] = ID[2], Col1[2] = name[1], Col2[2] = name[2], Col1[3] = city[1], Col2[3] = city[2]) order by soeji;
-
I like sys.odciNumberList ;-)
select case b.Column_Value when 1 then max(decode(a.rn,1,a.ID)) when 2 then max(decode(a.rn,1,a.name)) when 3 then max(decode(a.rn,1,a.city)) end as COL1, case b.Column_Value when 1 then max(decode(a.rn,2,a.ID)) when 2 then max(decode(a.rn,2,a.name)) when 3 then max(decode(a.rn,2,a.city)) end as COL2 from (select Row_Number() over(order by id) as rn, id,name,city from pivUnPiv) a,Table(sys.odciNumberList(1,2,3)) b group by b.Column_Value order by b.Column_Value;
-
I like women, not sure what your problem is.
This discussion has been closed.