Discussions
Categories
- 197.1K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.7K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 555 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.3K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 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
- 466 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
unable to create Query ? (Interesting Scenerio)

569986
Member Posts: 227
Hi all, i am unable to create a query for below expected output with the following data. Any help will be appreciated?
Expected output
ID1 NAME1 ID2 NAME2
1 test1 2 test2
3 test3 4 test4
5 test5 6 test6
Thanks in Advance
Expected output
ID1 NAME1 ID2 NAME2
1 test1 2 test2
3 test3 4 test4
5 test5 6 test6
Thanks in Advance
Tagged:
Best Answer
-
Select o.id id1, o.name name1, e.id id2, e.name name2 from emp o, emp e where o.id+1=e.id(+) and mod(o.id,2)=1;
Odd id on column1 and following even id on column2.
Max
Answers
-
with tab as ( select 1 col1, 'test1' col2 from dual union all select 2 col1, 'test2' col2 from dual union all select 3 col1, 'test3' col2 from dual union all select 4 col1, 'test4' col2 from dual union all select 5 col1, 'test5' col2 from dual union all select 6 col1, 'test6' col2 from dual ) SELECT * FROM (SELECT col1 , col2 , lead(col1) over (order by col1) col3, lead(col2) over (order by col1) col4 FROM tab ) WHERE mod(col1,2)=1
Ravi Kumar -
Select o.id id1, o.name name1, e.id id2, e.name name2 from emp o, emp e where o.id+1=e.id(+) and mod(o.id,2)=1;
Odd id on column1 and following even id on column2.
Max -
Hi,
Nice one!!!
Cheers!!!
Bhushan -
with tab as ( select 1 col1, 'test1' col2 from dual union all select 2 col1, 'test2' col2 from dual union all select 3 col1, 'test3' col2 from dual union all select 4 col1, 'test4' col2 from dual union all select 5 col1, 'test5' col2 from dual union all select 6 col1, 'test6' col2 from dual ) SELECT MAX(DECODE(mod(col1,2),1,col1)) col1, MAX(DECODE(mod(col1,2),1,col2)) col2 , MAX(DECODE(mod(col1,2),0,col1)) col3 , MAX(DECODE(mod(col1,2),0,col2)) col4 FROM tab GROUP BY floor((col1-1)/2)
It can be done in a single read on table without a join also like above... ;-)
Ravi Kumar -
I came up with the same solution of max(decode) but thought analytical approach was good also something new to learn if one does not know
Cheers!!!
Bhushan -
I used Pivot B-)
However I think group by and max(decode is more simple.
Because Pivot need calc to columns.with tab as( select 1 col1, 'test1' col2 from dual union all select 2 col1, 'test2' col2 from dual union all select 3 col1, 'test3' col2 from dual union all select 4 col1, 'test4' col2 from dual union all select 5 col1, 'test5' col2 from dual union all select 6 col1, 'test6' col2 from dual) select * from (select floor((col1-1)/2) as GID,mod(col1,2) as md, col1,col2 from tab) Pivot(max(col1) as ID,max(Col2) as name for md in(1,0)) order by GID; GID 1_ID 1_NAM 0_ID 0_NAM --- ---- ----- ---- ----- 0 1 test1 2 test2 1 3 test3 4 test4 2 5 test5 6 test6
http://download.oracle.com/docs/cd/B28359_01/server.111/b28313/analysis.htm#BCFHHHHF
If you need to pivot on an expression,
then you should alias the expression in a view before the PIVOT operation.
This discussion has been closed.