Solved

Query to split one row to multiple rows.

Posted on 2014-09-11
3
339 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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

911 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