Solved

Query to split one row to multiple rows.

Posted on 2014-09-11
3
338 Views
Last Modified: 2014-09-17
I have records like below.

col1      col2
11-Sep-14       11-Oct-14
15-Sep-14 18-Sep-14

In the first row I am having col1 & col2 in different months, so first record should be split like below

col1                      col2
11-sep-14          30-sep-2014
1-oct-14             11-oct-2014


In the second row, I just have values from same month,
so it should as it is. so altogether it should be 3 rows.
0
Comment
Question by:sakthikumar
  • 2
3 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 40316909
SELECT GREATEST(col1, ADD_MONTHS(TRUNC(col1, 'mm'), COLUMN_VALUE)) col1,
       LEAST(col2, LAST_DAY(ADD_MONTHS(TRUNC(col1, 'mm'), COLUMN_VALUE))) col2
  FROM yourtable,
       TABLE(
               SELECT COLLECT(LEVEL - 1)
                 FROM DUAL
           CONNECT BY ADD_MONTHS(TRUNC(col1, 'mm'), LEVEL - 1) <= TRUNC(col2, 'mm')
       )
0
 

Author Comment

by:sakthikumar
ID: 40322424
Hi SDSTUBER,

This query works exactly for me, just like to know if I achieve this through outer joins...

just like to have your expert opinion on this.
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 500 total points
ID: 40322916
There are probably some special cases where you could make an outer join work, but as a general technique for this kind of row expansion, no outer joins aren't really a viable option.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now