CSV into rows
Hi all,
From the example given in the following link I undersatand that we can convert comma seperated values into rows using a single SQL.
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:73830657104020
But when I try this with multiple rows it gives me duplicate records.
i.e
CREATE TABLE tst1
(problem_no VARCHAR2(10),
cr_tracking_no VARCHAR2(256))
INSERT INTO tst1 VALUES('P1','106,211,243,250')
INSERT INTO tst1 VALUES('P1','1966,22023,38023,53914,56533')
SELECT
FROM
(SELECT
problem_no,
SUBSTR (cr_tracking_no,
INSTR (cr_tracking_no, ',', 1, LEVEL ) + 1,
INSTR (cr_tracking_no, ',', 1, LEVEL+1) - INSTR (cr_tracking_no, ',', 1, LEVEL) -1 )
AS token
FROM (
SELECT problem_no, ','||trim(cr_tracking_no||',') cr_tracking_no
FROM tst1 )
CONNECT BY LEVEL <= LENGTH(cr_tracking_no)-LENGTH(REPLACE(cr_tracking_no,',',''))+1)
WHERE token IS NOT NULL
the desired output is
SQL> SELECT
2 *
3 FROM
4 (SELECT
5 distinct problem_no,
6 SUBSTR (cr_tracking_no,
7 INSTR (cr_tracking_no, ',', 1, LEVEL ) + 1,
8 INSTR (cr_tracking_no, ',', 1, LEVEL+1) - INSTR (cr_tracking_no, ',', 1, LEVEL) -1 )
9 AS token
10 FROM (
11 SELECT problem_no, ','||trim(cr_tracking_no||',') cr_tracking_no
12 FROM tst1 )
13 CONNECT BY LEVEL <= LENGTH(cr_tracking_no)-LENGTH(REPLACE(cr_tracking_no,',',''))+1)
14 WHERE token IS NOT NULL
15 /
PROBLEM_NO TOKEN
P1 106
P1 1966
P1 211
P1 22023
P1 243
P1 250
P1 38023
P1 53914
P1 56533
[pre]
The above query return 46 row (Duplicates) , whereas the expected number of record is 9
Kindly tell me what is wrong and how to get the desired rows without using distinct.
Thanks in advance,
Jaggyam
null
null
You are absolutely right about not wanting to use the distinct function here.
To make it work, you'll have to shortcut the hierarchy loop like this:
SQL> SELECT *
2 FROM ( SELECT problem_no
3 , SUBSTR
4 ( cr_tracking_no
5 , INSTR (cr_tracking_no, ',', 1, LEVEL ) + 1
6 , INSTR (cr_tracking_no, ',', 1, LEVEL+1) - INSTR (cr_tracking_no, ',', 1, LEVEL) -1
7 ) AS token
8 FROM ( SELECT problem_no
9 , ',' || trim(cr_tracking_no) || ',' cr_tracking_no
10 , rownum rn
11 FROM tst1
12 )
13 CONNECT BY LEVEL <= LENGTH(cr_tracking_no)-LENGTH(REPLACE(cr_tracking_no,','))-1
14 AND connect_by_root rn = rn
15 )
16 WHERE token IS NOT NULL
17 /
PROBLEM_NO TOKEN
P1 106
P1 211
P1 243
P1 250
P1 1966
P1 22023
P1 38023
P1 53914
P1 56533
9 rows selected.More techniques for doing this can be found here:
http://rwijk.blogspot.com/2007/11/interval-based-row-generation.html
Regards,
Rob.
Similar Messages
-
How to split columns into rows
Hi All,
Below is my table structure:=
SQL> create table split(id number,value varchar2(200));
Table created.
SQL> insert into split values(1,'X,Y,Z');
1 row created.
SQL> insert into split values(2,'A,B,C');
1 row created.
SQL> commit;
Commit complete.
Expected Output
ID Value
1 X
1 Y
1 Z
2 A
2 B
3 C
I know the feature of converting rows into columns by listagg in Oracle 11g, but is there any feature to convert rows into columns based on a delemiter..."," in my case.
Please help....
Thanks
Arijit>
is there any feature to convert rows into columns based on a delemiter
>
Here is one way
VAR csv VARCHAR2(100)EXEC :csv := 'abc,de,fg,hij,klmn,o,pq,rst,uvw,xyz';
The query:
WITH data AS( SELECT SUBSTR(csv, INSTR(csv,',',1,LEVEL)+1, INSTR(csv,',',1,LEVEL+1) - INSTR(csv,',',1,LEVEL)-1 ) token FROM ( SELECT ','||:csv||',' csv FROM SYS.DUAL ) CONNECT BY LEVEL < LENGTH(:csv)-LENGTH(REPLACE(:csv,',',''))+2 )SELECT token FROM data;See http://projectwownow.blogspot.com/2010/02/oracle-convert-csv-string-into-rows.html -
Loading of flat file (csv) into PSA – no data loaded
Hi BW-gurus,
We have an issue regarding loading a flat file (csv) into PSA using an infopackage u2013 (BI 7.0)
The infopackage has been used for a while. Prior the consultants with SAP_ALL-profile have run the infopackage. Now we want a few super users to run the infopackage.
We have created a role for the super users, including authorization objects:
Data Warehousing objects: S_RS_ADMWB
Activity: 03, 16, 23, 63, 66
Data Warehousing Workbench obj: INFOAREA, INFOOBJECT, INFOPACKAG, MONITOR, SOURCESYS, WORKBENCH
Data Warehousing Workbench u2013 datasource (version > BW 3.x): S_RS_DS
Activity: All
Datasource: All
Subobject for New DataSource: All
Sourcesystem: FILE
Data Warehousing Workbench u2013 infosource (flex update): S_RS_ISOUR
Activity: Display, Maintain, Request
Application Component: All
InfoSource: All
InfoSource Subobject: All values
As mentioned, the infopackage in question, has been used by consultants with SAP_ALL-profile for some time, and been working just fine. When the super users with the new role are executing the infopackage, the records are found, but not loaded into PSA. The load seems to be stuck, but no error message occurs. The file we are trying to load contains only 15 records.
Details monitor:
Overall status: Missing messages or warnings (yellow)
Requests (messages): Everything ok (green)
-> Data request arranged (green)
-> Confirmed with: OK (green)
Extraction (messages): Errors occurred (yellow)
-> Data request received (green)
-> Data selection scheduled (green)
-> 15 Records sent (0 Records received) (yellow)
-> Data selection ended (green)
Transfer (IDocs and TRFC): Missing messages (yellow)
Processing (data packet): Warnings received (yellow)
-> Data package 1 (? Records): Missing messages (yellow)
-> Inbound processing (0 records): Missing messages (yellow)
-> Update PSA (0 Records posted): Missing messages (yellow)
-> Processing end: Missing messages (yellow)
Have we forgotten something? Any assistance will be highly appreciated!
Cheers,
Anne Therese S. JohannessenHi,
Try to use the transaction ST01 to trace the authorization of the upload with the SAP_ALL.
And the enhance your Profile for the super user.
Best regards
Matthias -
Problem in displaying the data of columns into rows in sap script
hi,
i am working on a sap script and i have to display the dat which is displayed in column into rows but it is not displaying it properly.
eg, C
12.1
Si
5.5
it is displaying the data right now like this but i want to display the data like this:-
eg, C Si
12.1 5.5
plzzprovide me guidelines how to solve this problem.hi,
i am using this code to display the data:-
plzz provide me guidelines where i am getting wrong?
TOPparCOMPONENT DESP,,,,,, INS. LOT #, , , , , , MIC,,,,,,,,,, MIC VALUEparENDTOPparFINAL
PROTECT
IF &I_FINAL-PRUEFLOS& NE '000000000000'
&I_FINAL-MAKTX(23)&&i_final-prueflos(12Z)&
&I_FINAL-kurztext(25)&
&I_FINAL-original_input(8)&
ELSE
&I_FINAL-MAKTX(23)&
&I_FINAL-kurztext(25)&
&I_FINAL-original_input(8)&
ENDIF
ENDPROTECT
ITEMHEAD
POSITION WINDOW
SIZE WIDTH +0 . 4 CH HEIGHT +1 LN
BOX FRAME 10 TW
BOX HEIGHT '1.35' LN INTENSITY 20
IF &PAGE& = '1'
BOX XPOS '0' CH YPOS '0' CM WIDTH '0' CM HEIGHT '43' LN FRAME '10' TW
For horizontal line at top
BOX XPOS '0' CH YPOS '0' CM WIDTH '75' CH HEIGHT '0' LN FRAME '10' TW
COLUMN LINES...
END OF COLUMN LINES...
BOX XPOS '0' CH YPOS '43' LN WIDTH '75' CH HEIGHT '0' LN FRAME '10'TW
BOX XPOS '75' CH YPOS '0' LN WIDTH '0' CH HEIGHT '43' LN FRAME '10'TW
ELSE
COLUMN LINES...
END OF COLUMN LINES...
BOX XPOS '0' CH YPOS '0' CM WIDTH '0' CM HEIGHT '47' LN FRAME '10' TW
BOX XPOS '0' CH YPOS '0' CM WIDTH '75' CH HEIGHT '0' LN FRAME '10' TW
BOX XPOS '0' CH YPOS '0' CM WIDTH '45' CM HEIGHT '0' LN FRAME '10' TW
BOX XPOS '20' CH YPOS '0' CM WIDTH '0' CM HEIGHT '47' LN FRAME '10' TW
BOX XPOS '0' CH YPOS '47' LN WIDTH '75' CH HEIGHT '0' LN FRAME '10'TW
BOX XPOS '75' CH YPOS '0' LN WIDTH '0' CH HEIGHT '47' LN FRAME '10'TW
ENDIF
LINEFEED
NEWPAGE
NEW-PAGE
provide me guidelines to solve this problem.
Edited by: ricx .s on Mar 13, 2009 5:58 AM -
How to Load the data from excel file(Extension is .CSV) into the temp.table
Hi
How to Load the data from excel file(Extension is .CSV) into the temporary table of oracle in Forms11g.
My Forms Version is - Forms [64 Bit] Version 11.1.2.0.0 (Production)
Kindly Suggest the Solution.
Regards,
SachinHello Sachin,
You can use the following metalink note:How to Read Data from an EXCEL Spreadsheet into a Form Using Webutil Client_OLE2 (Doc ID 813535.1) and modify it a little bit.
Instead of copy values into forms you can save them in your temporary table.
Kind regards,
Alex
If someone's helpful or correct please mark it accordingly. -
How to display the rows in to columns and columns into rows?
DES:- I know by using pivot and unpivot you can convert rows into columns and columns into rows but i don't know how to write the syntax?
Hi,
Besides the places Martin mentioned above, there are many examples in the forum FAQ:
https://community.oracle.com/message/9362005#9362005
For an example of both PIVOT and UNPIVOT in the same query, see
http://forums.oracle.com/forums/thread.jspa?threadID=920854&tstart=0 -
Is there a simple script or series of SQL statements to import a CSV into either a new table or an existing table rather than using a utility? There must be some way to do it in SQL or PL/SQL.
That's the problem with using tools, like TOAD etc. to help you do stuff, as it doesn't help you to learn the basics.
Once you know how to do stuff from SQL*Plus then, fine, use a tool to do nitty gritty for you. The key to learning such a vaste area of expertise is to have a really good understanding of the basics first. So many people just use tools and struggle to do things because they just don't understand the underlying basics.
External tables are almost as simple as creating a basic table but you specify where the data is coming from and what the layout of that flat file is. After that you can just query it.
;) -
Hi All,
I need help in building view which actually can show columns data as row.
e.g.
row is as follows
Name Age Salary
ABC 25 10000
BBC 28 12000
The above tables data I want to get as
Name ABC BBC
Age 25 28
Salary 10000 12000
Thanks in advance.Even if I don't really understand such requirement, I wrote some times ago such function to play around that :
Re: Converting Columns into rows
Nicolas. -
Is it possible to open a csv into larger columns that display all the data that you need?
Is there a way to get a csv file to open into larger columns without having to increase the column size by hand. Maybe by opening a downloaded csv into a custom template, or to have it open so you can see all the text/numbers in the columns?
RM,
Maybe with a script you could do it in fewer steps, but there is a pretty simple way to get what you want.
If you create a template with a properly sized table for your data it's no bother to paste the standard table that the csv file opens into into a custom table.
Just open the CSV file in Numbers and open a custom template that you have prepared in advance. Select and Copy the table from the CSV file, Select cell A! in the Template, and Edit > Paste and Match Style. It is important when selecting Cell A1 in the custom template's table that you only click once on Cell A1 so that you don't inadvertently drill into the text layer.
That's it.
If you have any difficulty, let me know.
Jerry -
Hi. Does anyone know how to turn columns into rows ie:
select field1, field2, field3, field4, field5 from table
desired result:
field1 field2
field1 field3
field1 field4
field1 field5
Thank you!Something like this ?
select field1
, case n.l
when 1 then field2
when 2 then field3
when 3 then field4
when 4 then field5
end field
from table
, (select level l from dual connect by level <= 4) n -
Is there any way to import a csv file into ical? You can import a csv into address book but not iCal. For example try putting this in your calendar. http://graphics.fansonly.com/schools/wash/graphics/csv/washfb_2009.csv
You can use Applescript. Below is a sample that you can modify. Copy it into Script Editor and try it.
AK
<pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">--Convert CSV file to iCal events
--Prompts for file, then processes
--expects date,start time,end time,event name,xxxx,calendar name
--eg 12/01/2006,20:30,22:00,Water Committee,,TestCal
--change the various text item ns if data order in a file line is different
--blank lines skipped
--if other data present (eg location, notes ...) add a line in the tell calendar Calno loop
--to include it eg set location to text item 5 of ThisLine
set OldDelimiters to AppleScript's text item delimiters
set LF to ASCII character 10
set theFile to choose file with prompt "Select CSV calendar file"
set theLines to read theFile
set AppleScript's text item delimiters to {LF}
set theLines to paragraphs of theLines
set AppleScript's text item delimiters to {","}
repeat with ThisLine in theLines
if (count of ThisLine) > 0 then --ignore blanks
set StartDate to date (text item 1 of ThisLine & " " & text item 2 of ThisLine)
set EndDate to date (text item 1 of ThisLine & " " & text item 3 of ThisLine)
set CalName to word 1 of text item 6 of ThisLine
tell application "iCal"
set CalList to title of every calendar
if CalName is in CalList then
repeat with CalNo from 1 to count of CalList
if CalName is item CalNo of CalList then exit repeat
end repeat
else
set NewOne to make new calendar at end of calendars with properties {title:CalName}
set CalNo to 1 + (count of CalList)
end if
tell calendar CalNo
set newItem to make new event at end of events with properties {start date:StartDate}
set summary of newItem to text item 4 of ThisLine
set end date of newItem to EndDate
end tell --calendar
end tell --iCal
end if
end repeat
set AppleScript's text item delimiters to OldDelimiters</pre> -
Dear all....I need to convert all columns into rows in a table. For example table has following columns:
Emp_Cod........Val1......Val2......Val3
1 a b c
Now I wish that each column should display as a value like:
Emp_Cod........Val1
1 a
1 b
1 c
Now the one way to solve this job is to write a union statement for each column but for this I'll have to write equal number of select statements as there are columns.
What I need that is there anyway to write minimum code for this job, is there any alternate way???SQL> with t as(select 1 emp_code, 'a' val1, 'b' val2, 'c' val3 from dual)
2 select*from t unpivot(v for c in(val1,val2,val3));
EMP_CODE C V
1 VAL1 a
1 VAL2 b
1 VAL3 c
SQL> col COLUMN_VALUE for a20
SQL> with t as(select 1 emp_code, 'a' val1, 'b' val2, 'c' val3 from dual)
2 select*from t,table(sys.odcivarchar2list(val1,val2,val3));
EMP_CODE V V V COLUMN_VALUE
1 a b c a
1 a b c b
1 a b c c -
hi ,
i need to transpose columns into rows ,
i know i can use the UNION ALL but my num of columns will most likely not be fixed so how can i do that ?
pls adviseThis is from one of the forms link,, i reallyy dont know the link, but i guess this is "adrains" code
SQL> WITH ilv AS (
2 SELECT str || ',' AS str
3 , (LENGTH(str) - LENGTH(REPLACE(str, ','))) + 1 AS no_of_elements
4 FROM t
5 )
6 SELECT RTRIM(str, ',') AS original_string
7 , SUBSTR(str, start_pos, (next_pos-start_pos)) AS single_element
8 , element_no
9 FROM (
10 SELECT ilv.str
11 , nt.column_value AS element_no
12 , INSTR(
13 ilv.str,
14 ',',
15 DECODE(nt.column_value, 1, 0, 1),
16 DECODE(nt.column_value, 1, 1, nt.column_value-1)) + 1 AS start_pos
17 , INSTR(
18 ilv.str,
19 ',',
20 1,
21 DECODE(nt.column_value, 1, 1, nt.column_value)) AS next_pos
22 FROM ilv
23 , TABLE(
24 CAST(
25 MULTISET(
26 SELECT ROWNUM FROM dual CONNECT BY ROWNUM < ilv.no_of_elements
27 ) AS number_ntt )) nt
28 );
ORIGINAL_STRING SINGLE_ELEMENT ELEMENT_NO
X,Y,Z X 1
X,Y,Z Y 2
X,Y,Z Z 3
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG XXX 1
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG Y 2
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG ZZ 3
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG AAAAA 4
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG B 5
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG CCC 6
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG D 7
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG E 8
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG F 9
XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG GGG 10
13 rows selected.
Note that the above SQL performs the following steps:
* determines how many elements are in each string (WITH clause);
* for each string, generates a collection of n elements (TABLE expression), where n is the derived number of elements in the string. Note in particular the use of "less than" in the "CONNECT BY ROWNUM < ilv.no_of_elements" on line 26. In all versions other than 10.1.x, this will need to be "CONNECT BY ROWNUM <= ilv.no_of_elements" (i.e. "less than or equal to"). There is an unusual bug with this row-generation technique in 10.1 that generates an additional row from the CONNECT BY;
* uses the generated rows in a Cartesian Product with the original data to generate n rows per string, based on the above definition of n;
* calculates the start and end position of each element in each string (INSTR); and
* cuts each element from each string (SUBSTR). -
Import CSV into Numbers under European Settings?
SUMMARY: Anyone know how to import a CSV into Numbers on the iPhone when under European settings?
DETAIL:
The iPhone version of numbers seems to have difficulties reading any CSV if the user's device is on a setting where the decimal separator is a comma (e.g. 12,00) (such as most of Europe). Interestingly the iPad version does not have this problem.
For example HelloWorld.csv containing
Hello,World
imports as all one line rather than in two columns.
UTF8 of UTF16 doesn't seem to make a difference.
Changing the comma to a tab doesn't make a difference. (Numbers won't import .tsv files either.)
Adding quotes to the fields doesn't help.
Suggestions on creating a file that Numbers can import?On the iPhone, go to Settings, then General, then International, then Region Format. Set that to whatever it is on the iPad and then the CSV file should import the same on iPhone as it does on iPad. Not sure what else it will change, but if you have it changed on your iPad already, maybe it won't be a problem for the iPhone. And you can always change it back after importing the file I guess. Good luck.
-
Convert single column into rows
hi Gurus,
I have one table test colums are id and name.
id number
name varchar2
data is like
id name
1 xy
2 xyy
3 mm
4 pp
Now my requirement is to convert single column id into rows
i,e my output should be of singel rows like :- 1,2,3,4
How to achive this result .
I dont have any idea to do this query.
Please help guys.
Thanks in advance.
VijayWell,
As long as your code doesn't have to run in production, simplest way is:
WM_CONCAT (but it's not documented)
or use XMLAGG, it's simpler than a connect by:
MHO%xe> with t as (
2 select 1 col, 'xy' str from dual union all
3 select 2, 'xyy' from dual union all
4 select 3, 'mm'from dual union all
5 select 4, 'pp' from dual union all
6 select 8, 'pp' from dual union all
7 select 12, 'pp' from dual union all
8 select 40, 'pp' from dual
9 )-- actual query, based on id's generated above:
10 select rtrim(xmlagg(xmlelement(e,col||',')).extract('//text()'),',') col
11 from t;
COL
1,2,3,4,8,12,40
1 rij is geselecteerd.
Maybe you are looking for
-
Error using wldeploy in unix environment
I am trying to use wldeploy ant task to deploy and undeploy applications from my server in a unix environment, and keep getting errors there, and not locally on a XP development workstation. Below are my buildfile and the error that I am getting. Any
-
Solaris 9 Install Fails at Console Window
I am attempting to install Solaris 9 on an Ultra 10. Ran boot cdrom from OK prompt, using the install directory. Got to the selection of language fine, (selected English) CDE started up, and then got a console window. It displays the standard install
-
Missing component for Lomboz plug-in for Eclipse
Hello, I have the following problem that I would really appreciate some help with. Background 1. I have set up Eclipse 3.0.2. 2. I set up the EMF SDK 2.0.3 through the Eclipse.org update site. 3. I downloaded and setup Lomboz 3.0.1 (org.objectweb.lom
-
MacMini Bootcamp has 'wiped' external drive
Having just purchased a MiniMax external drive for my son's Mac Mini and migrated the Mac Mini over to that as the Start up disk, my son has decided to try and create a BootCamp partition by reinitialising the Mac Mini drive as MacOSX/Bootcamp drive.
-
Network Attached Hard Drive with Apple TV?
If i buy a NAS Hard Drive and have a movie in there, is there anyway I can access it through my Apple TV without having to out it on iTunes?