Home » SQL & PL/SQL » SQL & PL/SQL » Split 1 row into multiple based on date (Oracle 11G)
Split 1 row into multiple based on date [message #628546] Wed, 26 November 2014 03:42 Go to next message
sinpeak
Messages: 59
Registered: January 2011
Location: india
Member
I have a table with the following structure :

Table name : RT
Columns :
RT_ID NUMBER(9) NOT NULL
START_DATE DATE NOT NULL
END_DATE DATE NOT NULL
PARAM_1 VARCHAR2(20) 
PARAM_2 VARCHAR2(20)
PARAM_3 VARCHAR2(20)
PARAM_4 VARCHAR2(20)


I need to split each row into multiple rows based on the START_DATE and END_DATE range.
Example if one row in RT is :

RT_ID: 82
START_DATE:  01-JAN-2014
END_DATE: 04-JAN-2014
PARAM_1: A
PARAM_2: B
PARAM_3: C
PARAM_4: D


Then it should appear as :

82 1-Jan-14 A B C D
82 2-Jan-14 A B C D
82 3-Jan-14 A B C D
82 4-Jan-14 A B C D


Please advise/suggest how can this be achieved.

Thanks.



Re: Split 1 row into multiple based on date [message #628548 is a reply to message #628546] Wed, 26 November 2014 03:57 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
What is the result for
RT_ID: 82
START_DATE:  01-JAN-2014
END_DATE: 07-JAN-2014
PARAM_1: A
PARAM_2: B
PARAM_3: C
PARAM_4: D


What is the result for
RT_ID: 82
START_DATE:  01-JAN-2014
END_DATE: 02-JAN-2014
PARAM_1: A
PARAM_2: B
PARAM_3: C
PARAM_4: D


Obviously, different combinations are possible, I've mentioned only a couple of them. In other words, we need a better explanation.
Re: Split 1 row into multiple based on date [message #628549 is a reply to message #628548] Wed, 26 November 2014 04:17 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Use a row generator query to generate all possible dates and then join that to your table.
Re: Split 1 row into multiple based on date [message #628552 is a reply to message #628546] Wed, 26 November 2014 04:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Re: Split 1 row into multiple based on date [message #628559 is a reply to message #628546] Wed, 26 November 2014 05:19 Go to previous messageGo to next message
Andrey_R
Messages: 441
Registered: January 2012
Location: Israel
Senior Member
How about this:

SQL*Plus: Release 11.2.0.1.0 Production on Wed Nov 26 11:18:12 2014

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Release 11.2.0.1.0 - 64bit Production

SQL>
SQL>  col param_1 for a5;
SQL>  col param_2 for a5;
SQL>  col param_3 for a5;
SQL>  col param_4 for a5;
SQL>  col param_5 for a5;
SQL>
SQL>
SQL> create table rt (
  2  RT_ID NUMBER(9) NOT NULL,
  3  START_DATE DATE NOT NULL,
  4  END_DATE DATE NOT NULL,
  5  PARAM_1 VARCHAR2(20) ,
  6  PARAM_2 VARCHAR2(20),
  7  PARAM_3 VARCHAR2(20),
  8  PARAM_4 VARCHAR2(20));

Table created.

SQL> --
SQL> insert into rt values (82,'01-JAN-2014','04-JAN-2014','A','B','C','

1 row created.

SQL> commit;

Commit complete.

SQL> --
SQL>
SQL> select * from rt;

     RT_ID START_DAT END_DATE  PARAM PARAM PARAM PARAM
---------- --------- --------- ----- ----- ----- -----
        82 01-JAN-14 04-JAN-14 A     B     C     D

SQL>
SQL> --
SQL> create table rt_res (
  2  RT_ID NUMBER(9) NOT NULL,
  3  RES_DATE DATE NOT NULL,
  4  PARAM_1 VARCHAR2(20) ,
  5  PARAM_2 VARCHAR2(20),
  6  PARAM_3 VARCHAR2(20),
  7  PARAM_4 VARCHAR2(20));

Table created.

SQL>
SQL> --
SQL>
SQL> declare
  2    cnt_days number(6);
  3  begin
  4    for x in (select * from rt) loop
  5      select x.end_date - x.start_date + 1 into cnt_days from dual;
  6      for i in 1 .. cnt_days loop
  7        insert into rt_res
  8        values
  9          (x.rt_id,
 10           x.start_date + i - 1,
 11           x.param_1,
 12           x.param_2,
 13           x.param_3,
 14           x.param_4);
 15      end loop;
 16    end loop;
 17  end;
 18  /

PL/SQL procedure successfully completed.

SQL> --
SQL> commit;

Commit complete.

SQL> --
SQL> select * from rt_res;

     RT_ID RES_DATE  PARAM PARAM PARAM PARAM
---------- --------- ----- ----- ----- -----
        82 01-JAN-14 A     B     C     D
        82 02-JAN-14 A     B     C     D
        82 03-JAN-14 A     B     C     D
        82 04-JAN-14 A     B     C     D


Regards,
Andrey R.

[Updated on: Wed, 26 November 2014 05:19]

Report message to a moderator

Re: Split 1 row into multiple based on date [message #628587 is a reply to message #628559] Wed, 26 November 2014 06:24 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
How about working out how to do it in a single query? I've pointed out what's needed.
Re: Split 1 row into multiple based on date [message #628795 is a reply to message #628546] Fri, 28 November 2014 08:38 Go to previous messageGo to next message
Rajesh Reddy
Messages: 6
Registered: November 2014
Location: Hyderabad
Junior Member
Dear sinpeak,
go thrugh the below query


Select rt_id,start_Date+level-1,param_1,param_2,param_3,param_4
from tablename
connect by level<=(select (trunc(end_date)-trunc(start_DAte))+1 from tablename);


Thanks

Re: Split 1 row into multiple based on date [message #628805 is a reply to message #628795] Fri, 28 November 2014 09:15 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Hello Rajesh,

Welcome to the forum. Good to see you want tk help OP, however, it would be nice if you follow posting guidelines. You need to use code tags, and it would be good if you add an explanation about your suggestion.
Re: Split 1 row into multiple based on date [message #628814 is a reply to message #628795] Fri, 28 November 2014 09:54 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Rajesh Reddy wrote on Fri, 28 November 2014 15:38
Dear sinpeak,
go thrugh the below query

Select rt_id,start_Date+level-1,param_1,param_2,param_3,param_4
from tablename
connect by level<=(select (trunc(end_date)-trunc(start_DAte))+1 from tablename);

Thanks


This query will not work as soon as the table has more than one row.


Re: Split 1 row into multiple based on date [message #628817 is a reply to message #628814] Fri, 28 November 2014 10:46 Go to previous messageGo to next message
Rajesh Reddy
Messages: 6
Registered: November 2014
Location: Hyderabad
Junior Member
Hi Michel,
based on his requirement i posted it.
Re: Split 1 row into multiple based on date [message #628818 is a reply to message #628805] Fri, 28 November 2014 10:48 Go to previous messageGo to next message
Rajesh Reddy
Messages: 6
Registered: November 2014
Location: Hyderabad
Junior Member
Thanks Lalit,
I adhere to it.
Re: Split 1 row into multiple based on date [message #628820 is a reply to message #628817] Fri, 28 November 2014 10:52 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Requirement is "I need to split each row" which implies there are several rows in general case, not ALWAYS one row.

Previous Topic: using VIEW in procedures
Next Topic: FORALL and Associative Arrays
Goto Forum:
  


Current Time: Fri Apr 26 10:25:34 CDT 2024