1 row into multiple rows
I have a table time_entries
create table time_entries
(employee_id integer,
start_date date,
end_date date,
CONSTRAINT pk_time_entries PRIMARY KEY (employee_id, start_date))Suppose I have one row in this table (dates in yyyy-mm-dd format):
employee_id, start_date, end_date
1, 2012-08-27, 2012-09-02
As you can see the difference between the dates is 7 days. I want to write a query so that each day has its own row, i.e. I want to write a query which translates the above 1 row into 7 rows. This is the result set I want:
employee_id, start_date, end_date
1, 2012-08-27, 2012-08-27
1, 2012-08-28, 2012-08-28
1, 2012-08-29, 2012-08-29
1, 2012-08-30, 2012-08-30
1, 2012-08-31, 2012-08-31
1, 2012-09-01, 2012-09-01
1, 2012-09-02, 2012-09-02
Is it possible to do it in one select statement?
Edited by: srhcan on Aug 17, 2012 4:26 PM
Edited by: srhcan on Aug 17, 2012 4:26 PM
WITH t
AS (SELECT 1 id,
TO_DATE ('2012-08-27', 'yyyy-mm-dd') start_date,
TO_DATE ('2012-09-02', 'yyyy-mm-dd') end_date
FROM DUAL
UNION ALL
SELECT 2 id,
TO_DATE ('2012-10-27', 'yyyy-mm-dd') start_date,
TO_DATE ('2012-11-02', 'yyyy-mm-dd') end_date
FROM DUAL)
SELECT id, start_date, end_date
FROM t
MODEL
PARTITION BY (id)
DIMENSION BY (0 d)
MEASURES (start_date, end_date, end_date stop)
RULES
ITERATE (10000) UNTIL start_date[ITERATION_NUMBER] = stop[0]
(start_date [ITERATION_NUMBER] = start_date[0] + ITERATION_NUMBER,
end_date [ITERATION_NUMBER] = start_date[0] + ITERATION_NUMBER)
ID START_DATE END_DATE
1 8/27/2012 8/27/2012
1 8/28/2012 8/28/2012
1 8/29/2012 8/29/2012
1 8/30/2012 8/30/2012
1 8/31/2012 8/31/2012
1 9/1/2012 9/1/2012
1 9/2/2012 9/2/2012
2 10/27/2012 10/27/2012
2 10/28/2012 10/28/2012
2 10/29/2012 10/29/2012
2 10/30/2012 10/30/2012
2 10/31/2012 10/31/2012
2 11/1/2012 11/1/2012
2 11/2/2012 11/2/2012
Similar Messages
-
I need to divide selected row into multiple rows when i navigate ADF 11g
Hi
I'm using jdeveloper 11.1.1.2.0 with ADF 11g.
I need to divide selected row into multiple rows when i navigate to other page . Scenario - in first page i'm displaying some records with columns like empno , empstatus , empworkdepts ,curdepts
Here empworkdepts gives the numeric number like no of departments work shifts 3 or 4 or 5. when i select any particular employee and fire next button to navigate next page.I have to divide the selected employee with same information into multiple times based on the empworkdepts value.
empno empstatus empworkdepts curdept
001 eds 2 TS
002 hr 1 FO
003 eds 4 TS
*004 eds 3 TS*
now i selected employee 004 , when i navigate to next page.
Empno EmpStatus EmpWorkDepts CurDept
004 eds 3 TS
004 eds 3 TS
004 eds 3 TS
i did with java code in bean .but not stable .
any help............
thanks advance.............
Edited by: user9010551 on May 5, 2010 10:48 PM
Edited by: user9010551 on May 10, 2010 11:31 PMuser9086775 wrote:
Hi Experts,
I have a requirment where i need to fetch parts of a single row into multiple rows from a singlt Query, i am sure it is possible using Pivots but just cant figure out an approach. Any help on this is highly appriciapted.
Requirment:
This is a sample set record in a table
Product Sub Product Name Age
New Car Nissan Tom 49
New Car Nissan Jack 36
Old Car Audi Sam 24
Old Car Jaguar Pint 26
Old Car Audi Smith 41
I need to be able to fetch the above data in the below fashion
Product Sub Product Name Age
New Car
Nissan
Tom 49
Jack 36
Old Car
Audi
Sam 24
Smith 41
Jaguar Pint 26Please help with ideas as to how can i achive the above without using PLSQL.
Thanks in advance!You should be doing this in the client on not in the DB. Use the reporting tool that you use to do this.
For example if you are in SQL Plus you can use the BREAK command. -
Split single row into multiple rows containing time periods
Hi,
I have a table with rows like this:
id, intime, outtime
1, 2010-01-01 00:10, 2010-01-3 20:00
I would like to split this row into multiple rows, 1 for each 24hr period in the record.
i.e. The above should translate into:
id, starttime, endtime, period
1, 2010-01-01 00:10, 2010-01-02 00:10, 1
1, 2010-01-02 00:10, 2010-01-03 00:10, 2
1, 2010-01-03 00:10, 2010-01-03 20:00, 3
The first starttime should be the intime and the last endtime should be the outtime.
Is there a way to do this without hard-coding the 24hr periods?
Thanks,
Dan Scott
http://danieljamesscott.orgThanks for all the feedback, Dan.
It appears that the respective solutions provided will give you: a) different resultsets and b) different performance.
Regarding your 'truly desired resultset' you haven't answered all questions from my previous post (there are differences in the provided examples), but anyway:
I found that using CEIL or ROUND makes quite a difference, using my 'simple 3 record testset' (30 records vs. 66 records got initially returned, that's less than half of the original). That's quite a difference. However, I must call it a day (since it's almost midnight) for now, so there's room for more optimizement and I haven't thoroughly tested.
But this might hopefully make a difference performancewise when compared to my previous 'dreaded example':
SQL> drop table t;
Table dropped.
SQL> create table t as
2 select 1 id, to_date('2010-01-01 00:10', 'yyyy-mm-dd hh24:mi') intime, to_date('2010-01-03 20:00', 'yyyy-mm-dd hh24:mi') outtime from dual union all
3 select 2 id, to_date('2010-02-01 00:10', 'yyyy-mm-dd hh24:mi') intime, to_date('2010-02-05 20:00', 'yyyy-mm-dd hh24:mi') outtime from dual union all
4 select 3 id, to_date('2010-03-01 00:10', 'yyyy-mm-dd hh24:mi') intime, to_date('2010-03-03 00:10', 'yyyy-mm-dd hh24:mi') outtime from dual;
Table created.
SQL> select id
2 , max(intime)+level-1 starttime
3 , case
4 when level = to_char(max(t.outtime), 'dd')
5 then max(t.outtime)
6 else max(t.intime)+level
7 end outtime
8 , level period
9 from t
10 connect by level <= round(outtime-intime)
11 group by id, level
12 order by 1,2;
ID STARTTIME OUTTIME PERIOD
1 01-01-2010 00:10:00 02-01-2010 00:10:00 1
1 02-01-2010 00:10:00 03-01-2010 00:10:00 2
1 03-01-2010 00:10:00 03-01-2010 20:00:00 3
2 01-02-2010 00:10:00 02-02-2010 00:10:00 1
2 02-02-2010 00:10:00 03-02-2010 00:10:00 2
2 03-02-2010 00:10:00 04-02-2010 00:10:00 3
2 04-02-2010 00:10:00 05-02-2010 00:10:00 4
2 05-02-2010 00:10:00 05-02-2010 20:00:00 5
3 01-03-2010 00:10:00 02-03-2010 00:10:00 1
3 02-03-2010 00:10:00 03-03-2010 00:10:00 2
10 rows selected.
SQL> By the way: I'm assuming you're on 10g, is that correct?
Can you give us some information regarding the indexes present on your table? -
Urgent: How to break 1 Row into Multiple Rows
Hi Experts,
I have a requirment where i need to fetch parts of a single row into multiple rows from a singlt Query, i am sure it is possible using Pivots but just cant figure out an approach. Any help on this is highly appriciapted.
Requirment:
This is a sample set record in a table
Product Sub Product Name Age
New Car Nissan Tom 49
New Car Nissan Jack 36
Old Car Audi Sam 24
Old Car Jaguar Pint 26
Old Car Audi Smith 41
I need to be able to fetch the above data in the below fashion
Product Sub Product Name Age
New Car
Nissan
Tom 49
Jack 36
Old Car
Audi
Sam 24
Smith 41
Jaguar Pint 26
Please help with ideas as to how can i achive the above without using PLSQL.
Thanks in advance!user9086775 wrote:
Hi Experts,
I have a requirment where i need to fetch parts of a single row into multiple rows from a singlt Query, i am sure it is possible using Pivots but just cant figure out an approach. Any help on this is highly appriciapted.
Requirment:
This is a sample set record in a table
Product Sub Product Name Age
New Car Nissan Tom 49
New Car Nissan Jack 36
Old Car Audi Sam 24
Old Car Jaguar Pint 26
Old Car Audi Smith 41
I need to be able to fetch the above data in the below fashion
Product Sub Product Name Age
New Car
Nissan
Tom 49
Jack 36
Old Car
Audi
Sam 24
Smith 41
Jaguar Pint 26Please help with ideas as to how can i achive the above without using PLSQL.
Thanks in advance!You should be doing this in the client on not in the DB. Use the reporting tool that you use to do this.
For example if you are in SQL Plus you can use the BREAK command. -
Convert one record row into multiple rows
Hi,
I have small requirement.I have selected one data base record into an internal table.Now internal table has 1 record i.e 1 row(ex: 10 columns). Now i will convert this single row record into multiple records i.e 10 rows( 10 columns wil; be converted into 10 rows). How i will convert this. Please give me any idea on this.
Regards
GeethaHi Geetha,
Search SCN using keyword, " convert Rows into columns" or vice versa,,,
U will get more answers & solutions,
Look at Some of the Previous threads....
Re: How to create a structure of itab as rows as colums and columns rows dy
Re: Transpose rows and columns
CONVERT ROWS INTO COLUMNS IN INTERNAL TABLE
Convert Internal table Rows into columns of another internal table
how to convert columns of an internal table to rows of another internal tab.
Convert Columns into Rows (internal tables) - Urgent Help Pleasse..
converting columns to rows
Thanks & regards,
Dileep .C -
Convert 1 row into multiple rows
Guys - I have a table with 6 columns and it looks like this:
1 2 3 4 5 6
1 XX Y A Z ABC
2 XX P AB CN CAB
Now I want my output to be:
1 2 3 4 5 6
1 XX Y AAA AAB ABC
2 XX Y AAC AAD ABC
3 XX Y AAE AAF ABC......
26 XX Y AAY AAZ ABC
27 XX P ABA ABB CAB
28 XX P ABC ABD CAB
9999 XX P CNY CNZ CAB
I am trying to do this in SQL , but without much luck. I can always do it with pl-sql but I want to keep that as my last option.
Please, let me know if I need to explain this further...
Thanks..1. Its not that I cannot do with pl/sql, but would like to try this with pure sql than pl / sql, thats what I meant
2. for length( col4 and col5 ), its actually length(col4) =3 and length(col5) = 3, sorry if you miss understood it
3. You are making up your data for all the possible combinations you might have i the future, thats why you are adding 'A' till 'Z' to each and every row...
so for example if a row has
col1 col4 col5 col6
1 ZZX ZZZ john
Now I need to make this row as:
col1 col4 col5 col6
1 ZZX ZZY john
2 ZZZ ZZZ john
So in the future if john is replaced by jack...then all I have to do it update these 2 rows col6 to 'jack' instead of 'john'.
Now the col1 which is the primary key for my new table actually goes into another table as ID field (in my fact as a dimension key)....so I dont have to update both the tables rather I can live with just updating 1 table...
Let me know if you need further explanation... -
Spliting rows into multiple rows based on time interval
Hi experts need your help for framing query for the below scenarios
Scenario 1
Query should check for priority record(25), if the start_date and end_date of that priority record is the max in that group, records will not have any split.
output will be the same.
DC Store St Date End date Priority
955 3 1/1/2010 12/31/9999 25
966 3 4/5/2011 10/10/2011 50
977 3 10/12/2011 12/12/2012 100
output
DC store St Date End date Priority Rank
955 3 1/1/2010 12/31/9999 25 1
966 3 4/5/2011 10/10/2011 50 2
977 3 10/12/2011 12/12/2012 100 3
Scenario 2
If priority record is not covering the max range, then split the records as shown below,
1. during the time period 1/1/2011 & 4/30/2011 there were no other DC for that store so rank would be 1
2. the next range would be 5/1/2011 to 6/29/2011 we have 2 records in service so the record with low priortiy would be ranked 1 and second priority would be ranked 2
3. similarly, for 6/30/2011 to 10/1/2011 we have 3 records in service and it will be ranked accordingly on the priority.
DC Store St Date End date Priority
966 3 6/30/2011 10/1/2011 25
955 3 5/1/2011 11/30/2011 50
977 3 1/1/2011 12/31/2011 100
output
DC store St Date End date Priority Rank
977 3 1/1/2011 4/30/2011 100 1
955 3 5/1/2011 6/29/2011 50 1
977 3 5/1/2011 6/29/2011 100 2
966 3 6/30/2011 10/1/2011 25 1
955 3 6/30/2011 10/1/2011 50 2
977 3 6/30/2011 10/1/2011 100 3
955 3 10/2/2011 11/30/2011 50 1
977 3 10/2/2011 11/30/2011 100 2
977 3 12/1/2011 12/31/2011 100 1
Scenario 3
This works similar to scenario 2
DC Store St Date End date Priority
966 3 2/1/2011 12/31/2011 25
955 3 1/1/2011 12/31/2012 50
977 3 5/1/2011 06/31/2011 100
output
DC store St Date End date Priority Rank
955 3 1/1/2011 1/31/2011 50 1
966 3 2/1/2011 12/31/2011 25 1
955 3 2/1/2011 12/31/2011 50 2
977 3 5/1/2011 6/30/2011 100 3
955 3 1/1/2012 12/31/2012 50 1
Note: the number of rows coming in input will be dynamic and there are possibilities for the date ranges to overlap.Hi,
Sorry, your requirement are unclear.
What role do dc, store and priority play in this problem? Prioriry obviously has something to do with how rows are split. Why is priority=25 special? Does the number 25 have some speciakl meaning, or does it just happen to be the lowest value of priority? Can 2 (or more) rows have the same priority? if so, can their dates overlap? Include examples in your sample data and results.
I think you want something like this:
WITH all_dates AS
SELECT st_date AS a_date
, priority
FROM table_x
UNION ALL
SELECT end_date + 1 AS a_date
, priority
FROM table_x
, got_break_date AS
SELECT a_date
, LEAD (a_date) OVER (ORDER BY a_date) AS next_date
, MIN (priority) AS min_priority
FROM all_dates
GROUP BY a_date
, paired_data AS
SELECT x.*
, b.*
, GREATEST ( x.st_date
, b.a_date
) AS s_date
, LEAST ( x.end_date
, b.next_date
) AS e_date
, RANK () OVER ( PARTITION BY GREATEST (x.st_date, b.a_date)
ORDER BY x.priority
) AS rnk
FROM table_x x
JOIN got_break_date b ON b.a_date >= x.st_date
AND b.a_date < x.end_date
AND b.min_priority <= x.priority
SELECT dc
, store
, s_date AS st_date
, e_date - CASE
WHEN rnk = 1
AND end_date = e_date
THEN 0
ELSE 1
END AS end_date
, priority
, rnk
FROM paired_data
ORDER BY s_date
, priority
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the results you want from that data, formatted, between \ tags. See the froum FAQ {message:id=9360002}
Point out where the results of the query above are wrong, and explain, using specific examples, how you get those results from the given data in those places.
Always say what version of Oracle you're using. -
Multiplex single row into multiple rows
Hi guys,
could you please help me with preparing a select statement.
Table sales:
product name transaction
x laptop 3
y mobile 2
z charger 4
consider we have the table structure as above.
i need a select query, that gives the result as below
product name transaction
x laptop 1
x laptop 1
x laptop 1
y mobile 1
y mobile 1
z charger 1
z charger 1
z charger 1
z charger 1
here the ransactions are divided into several records based on the transaction.
Thanks
KartheeHi Karthee,
Welcome to the forum !!!
Try this...
with xx as(
select 'x' p,'laptop' n,3 t from dual union all
select 'y' p,'mobile' n,2 t from dual union all
select 'z' p,'charger' n,4 t from dual
select t1.p ,t1.n, 1 from (
select p,n,row_number() over(partition by p,n order by p) rn from xx
connect by level<=t) t1, xx
where rn<= xx.t and xx.p = t1.p;gives
o/p =
x laptop 1
x laptop 1
x laptop 1
y mobile 1
y mobile 1
z charger 1
z charger 1
z charger 1
z charger 1Ranit B.
Edited by: ranit B on Nov 24, 2012 9:30 PM
-- added o/p -
Splitting comma seperated column data into multiple rows
Hi Gurus,
Please help me for solving below scenario. I have multiple value in single column with comma seperated values and my requirement is load that data into multiple rows.
Below is the example:
Source Data:
Product Size Stock
ABC X,XL,XXL,M,L,S 1,2,3,4,5,6
Target Data:
Product Size Stock
ABC X 1
ABC XL 2
ABC XXL 3
ABC M 4
ABC L 5
ABC S 6
Which transformation we need to use for getting this output?
Thanks in advance !Hello,
Do you need to do this tranformation through OWB mapping only? And can you please tell what type of source you are using? Is it a flat file or a table?
Thanks -
Easy Question: How to split concatenated string into multiple rows?
Hi folks,
this might be an easy question.
How can I split a concatenated string into multiple rows using SQL query?
INPUT:
select 'AAA,BBB,CC,DDDD' as data from dualDelimiter = ','
Expected output:
data
AAA
BBB
CCC
DDDDI'm looking for something kind of "an opposite for 'sys_connect_by_path'" function.
Thanks,
TomasHere is the SUBSTR/INSTR version of the solution:
SQL> WITH test_data AS
2 (
3 SELECT ',' || 'AAA,BBB,CC,DDDD' || ',' AS DATA FROM DUAL
4 )
5 SELECT SUBSTR
6 (
7 DATA
8 , INSTR
9 (
10 DATA
11 , ','
12 , 1
13 , LEVEL
14 ) + 1
15 , INSTR
16 (
17 DATA
18 , ','
19 , 1
20 , LEVEL + 1
21 ) -
22 INSTR
23 (
24 DATA
25 , ','
26 , 1
27 , LEVEL
28 ) - 1
29 ) AS NEW_STRING
30 FROM test_data
31 CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(DATA,'[^,]','')) - 1
32 /
NEW_STRING
AAA
BBB
CC
DDDD -
Convert multiple rows into single rows for the respective index name
Dear Experts,
I want to convert multiple rows into single rows for the respective index name,
Here is my query.
SELECT user_tables.table_name, user_indexes.index_name, user_ind_columns.column_name
FROM user_tables
JOIN user_indexes on user_indexes.table_name = user_tables.table_name
join USER_IND_COLUMNS on USER_INDEXES.INDEX_NAME = USER_IND_COLUMNS.INDEX_NAME
where user_indexes.index_name not like '%PK%' AND user_ind_columns.column_name NOT LIKE '%SYS%'
ORDER BY user_tables.table_name,user_indexes.index_name;
Result of previous query
TABLE_NAME
INDEX_NAME
COLUMN_NAME
T1
IDX_ACCNTYPCFG1
ENABLE_SERVICE
T1
IDX_ACCTTYPCFG1
ACC_CODE
T1
IDX_ACCTTYPCFG1
ACCTYPE
T2
IDX_ACCTTYPCFGAPP1
ACCTYPE
T3
IDX_ACTLG1
MOBILE_NO
T3
IDX_ACTLG1
ID
Desired output required is
TABLE_NAME
INDEX_NAME
COLUMN_NAME
T1
IDX_ACCNTYPCFG1
ENABLE_SERVICE,ACC_CODE,ACCTYPE
T2
IDX_ACCTTYPCFGAPP1
ACCTYPE
T3
IDX_ACTLG1
ACCTYPE,MOBILE_NO
please help.Maybe
with
user_tables as
(select 'T1' table_name,'IDX_ACCNTYPCFG1' index_name,'ENABLE_SERVICE' column_name from dual union all
select 'T1','IDX_ACCTTYPCFG1','ACC_CODE' from dual union all
select 'T1','IDX_ACCTTYPCFG1','ACCTYPE' from dual union all
select 'T2','IDX_ACCTTYPCFGAPP1','ACCTYPE' from dual union all
select 'T3','IDX_ACTLG1','MOBILE_NO' from dual union all
select 'T3','IDX_ACTLG1','ID' from dual
select table_name,
case index_name when 'IDX_ACCNTYPCFG1' then 'IDX_ACCTTYPCFG1' else index_name end index_name,
listagg(case column_name when 'ID' then 'ACCTYPE' else column_name end,',') within group (order by null) column_name
from user_tables
group by table_name,case index_name when 'IDX_ACCNTYPCFG1' then 'IDX_ACCTTYPCFG1' else index_name end
TABLE_NAME
INDEX_NAME
COLUMN_NAME
T1
IDX_ACCTTYPCFG1
ACCTYPE,ACC_CODE,ENABLE_SERVICE
T2
IDX_ACCTTYPCFGAPP1
ACCTYPE
T3
IDX_ACTLG1
ACCTYPE,MOBILE_NO
Regards
Etbin -
Merge multiple rows into single row (but multiple columns)
How to merge multiple rows into single row (but multiple columns) efficiently.
For example
IDVal IDDesc IdNum Id_Information_Type Attribute_1 Attribute_2 Attribute_3 Attribute_4 Attribute_5
23 asdc 1 Location USA NM ABQ Four Seasons 87106
23 asdc 1 Stats 2300 91.7 8.2 85432
23 asdc 1 Audit 1996 June 17 1200
65 affc 2 Location USA TX AUS Hilton 92305
65 affc 2 Stats 5510 42.7 46 9999
65 affc 2 Audit 1996 July 172 1100
where different attributes mean different thing for each Information_type.
For example for Information_Type=Location
Attribute_1 means Country
Attribute_2 means State and so on.
For example for Information_Type=Stats
Attribute_1 means Population
Attribute_2 means American Ethnicity percentage and so on.
I want to create a view that shows like below:
IDVal IDDesc IDNum Country State City Hotel ZipCode Population American% Other% Area Audit Year AuditMonth Audit Type AuditTime
23 asdc 1 USA NM ABQ FourSeasons 87106 2300 91.7 46 85432 1996 June 17 1200
65 affc 2 USA TX AUS Hilton 92305 5510 42.7 46 9999 1996 July 172 1100
ThanksHi,
That's called Pivoting . The forum FAQ has a section on this subject: {message:id=9360005}
I hope this answers your question.
If not, post your best attempt, along with a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data. (You did post the results you wanted, but they're very hard to read because they're not formatted. Use \ tags, as described in the forum FAQ, below.)
Explain, using specific examples, how you get the results you want from the data given.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0). This is always important, but especially so with pivots.
See the forum FAQ {message:id=9360002} -
Split one row into multiple columns
Hi,
Data in one CLOB column in a table storing with delimiter, ##~~##. Ex. ##~~##abc##~~##defgh##~~##ijklm##~~##nopqr (data starts with delimiter). Please help me to split the data into multiple rows like below and it should be in the same order.
abc
defgh
ijklm
nopqr
I am using Oracle 11g.
Thanks.Thanks Hoek for your response. Before posting my question in the forum, I tried similar query. It is working with one character as delimiter.
with test as (select 'ABC,DEF,GHI,JKL,MNO' str from dual )
select regexp_substr (str, '[^,]+', 1, rownum) split
from test
connect by level <= length (regexp_replace (str, '[^,]+')) + 1;
Above query is giving correct result by fetching 5 rows. I have modified the query like below...
with test as (select 'ABC,,,DEF,,,GHI,,,JKL,,,MNO' str from dual )
select regexp_substr (str, '[^,,,]+', 1, rownum) split
from test
connect by level <= length (regexp_replace (str, '[^,,,]+')) + 1;
Above query resulting 13 rows and last 8 rows are nulls. Number of null rows are increasing, if I increase number of characters in delimiter. Could you please tell me how to avoid those null rows.
Thanks. -
Cats ESS view - into multiple rows when we release times or refresh them
Dear ALL,
We are using the record working times and when we enter the data for the week for 5 days each with 8 hours a day in one row in portal. Once we refresh or review that page it is resulting into multiple rows, like for 5 days five rows each row containing an entry for each day. how can we avoid this, we want all entries maintained into one row only.
Help is appreciated.
Thanks and Regs,
RajMade some changes in Data entry profiles
-
Please - immediate help needed parsing csv values into multiple rows
Hello, we have a very immediate need to be able to parse out a field of comma separated values into individual rows. The following is an example written in SQL Server syntax which does not work in Oracle.
The tricky part is that each ROUTES can be a different length, and each CSV can have a different number of routes in it.
Here is an example of the table ("Quotes") of CSV values I want to normalize:
TPNUMBER ROUTES
1001 1, 56W, 18
1002 2, 16, 186, 28
Here is an example of what I need it to look like:
TPNUMBER ROUTES
1001 1
1001 56W
1001 18
1002 2
1002 16
1002 186
1002 28
Here is the "Tally" table for the query below:
ID
1
2
3
4
5
6
7
And finally, here is the query which parses CSV values into multiple rows but which does not work in Oralce:
SELECT TPNUMBER,
NullIf(SubString(',' + ROUTES + ',' , ID , CharIndex(',' , ',' + ROUTES + ',' , ID) - ID) , '') AS ONEROUTE
FROM Tally, Quotes
WHERE ID <= Len(',' + ROUTES + ',') AND SubString(',' + Phrase + ',' , ID - 1, 1) = ','
AND CharIndex(',' , ',' + ROUTES + ',' , ID) - ID > 0
It may be necessary to use a cursor to loop through the CSV table and process each row (a loop within another loop...) but this is beyond my comprehesion of PL/SQL.
Many thanks in advance for your advice/help.
apkNot sure what you are trying to do with the last step, but this should work for the first part. I assume you would use sqlldr but I just did inserts instead. You might need more than 5 "routes" in the csv. You could put some reasonable max on that number of columns:
SQL>create table t_csv
2 (TPNUMBER varchar2(20),
3 ROUTE_1 VARCHAR2(5),
4 ROUTE_2 VARCHAR2(5),
5 ROUTE_3 VARCHAR2(5),
6 ROUTE_4 VARCHAR2(5),
7 ROUTE_5 VARCHAR2(5),
8 ROUTE_6 VARCHAR2(5) );
Table created.
SQL>INSERT INTO t_csv (TPNUMBER,ROUTE_1,ROUTE_2) values( '1001 1', '56W', '18' );
1 row created.
SQL>INSERT INTO t_csv (TPNUMBER,ROUTE_1,ROUTE_2,ROUTE_3) values( '1002 2', '16', '186', '28');
1 row created.
SQL>create table t_quotes(
2 tpnumber NUMBER,
3 routes VARCHAR2(5));
Table created.
SQL>DECLARE
2 L_tpnumber NUMBER;
3 L_route VARCHAR2(5);
4 begin
5 for rec in (select * from t_csv) loop
6 L_tpnumber := SUBSTR(rec.tpnumber,1,INSTR(rec.tpnumber,' ')-1);
7 L_route := SUBSTR(rec.tpnumber,INSTR(rec.tpnumber,' ')+1);
8 insert into t_quotes values( L_tpnumber, l_route );
9 if rec.route_1 is not null then
10 insert into t_quotes values( L_tpnumber, rec.route_1 );
11 end if;
12 if rec.route_2 is not null then
13 insert into t_quotes values( L_tpnumber, rec.route_2 );
14 end if;
15 if rec.route_3 is not null then
16 insert into t_quotes values( L_tpnumber, rec.route_3 );
17 end if;
18 if rec.route_4 is not null then
19 insert into t_quotes values( L_tpnumber, rec.route_4 );
20 end if;
21 if rec.route_5 is not null then
22 insert into t_quotes values( L_tpnumber, rec.route_5 );
23 end if;
24 end loop;
25 end;
26 /
PL/SQL procedure successfully completed.
SQL> select tpnumber, routes from t_quotes;
TPNUMBER ROUTE
1001 1
1001 56W
1001 18
1002 2
1002 16
1002 186
1002 28
7 rows selected.
Maybe you are looking for
-
No free trial as promised.
I can understand you wan't money for your software, but promising a free trial and then demanding pay after customers installs is pretty lousy for a company this size. I was offered a free trial. After install, you demand money. False advertising, og
-
Error when trying to extend range using an Extreme connecting to a Express
Ok, My cable modem is connected just fine to my AE. I can run iTunes (which is nice!) off of it, connect to the internet off of my MBP and all. I just bought a AEBS to be able to EXTEND the range of my connection for all of my small townhome. However
-
Generate PDF form without hidden fields
when I generate a PDF form from the responses it shows the hidden fields that would not have been shown. Is there a way to stop that from happening?
-
Hi Is this preparedstatement valid INSERT INTO TABLE2 (COL1,COL2,COL3,COL4) SELECT C1,C2,?,? FROM TABLE1 WHERE condn When I do con.prepareStatement() on this sql it is giving me error saying "Use of parameter marker not valid" Is this wrong??
-
Is this tough Landscape possible?
hai friends Actually i have gone through Marketplace even though i couldnt understand Is this Landscape Possible :(Service Procurement using Extended Classic Scenario) Systems connected CCM,SUS,EBP,Strategic Sourcing Supplier directory replicated fro