SQL to convert the lines into columns
Can someone please help with this? It sounds easy, but I can't do it on my ownI need to create the output table something like this using the input table
Entry table
BOOKID ORDNAM ORDID
100 1 XXX
100 2 YYY
101 AAA 3
101 4 BBB
For each ID value, there will be 2 ORDID leave a sequence
Output table
BOOKID ORDNAM ORDID ORDNAM ORDID
YYY XXX 100 1 2
101 BBB 3 4 AAA
Hello
It's called swing , and there are a few good links in the FAQ forum on this subject:
SQL and PL/SQL FAQ
Exactly how do depends on your version of Oracle and exactly to your needs.
If you use Oracle 11, you can use SELECT... Function PIVOT.
If you have only two lines (by ID) of the original table you need to view side by side, then a self-join can be easier, regardless of your version.
For example:
SELECT l.bookid
, l.ordnam
, l.ordid
, r.ordnam
, r.ordid
FROM input_table l
LEFT OUTER JOIN input_table r ON l.bookid = r.bookid
AND l.ordid < r.ordid
;
I hope that answers your question.
If this isn't the case, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all tables, or post a question involving tables commonly available, such as those in the scott schema. Whatever it is, after the desired results from these data.
Explain, using specific examples, how you get these results from these data.
Always tell what version of Oracle you are using.
You will find the answers better faster if you always provide this information whenever you post a question.
Published by: Frank Kulash, July 22, 2011 11:21
Tags: Database
Similar Questions
-
How to convert the lines into columns dynamically
HII
I have a 'report_line' with the following data table
location_code line_code value
LOC1 L1 12
loc2 L1 13
LOC1 L2 15
loc2 L3 11
LOC3 L1 9
Now I want the o/p by converting the line_codes in clumns as below
L1 L2 L3
LOC1 12 15
13 11 loc2
LOC3 9
How can I do this dynamically. Line codes are not constatnt, they can change for each scenario.
Thank you
SandeepI want just a sql for this query and no. columns ' maximum of are 6.
In this case you can do.
--sample data with t as ( select 'loc1' location_code, 'L1' line_code, 12 value from dual union all select 'loc2', 'L1', 13 from dual union all select 'loc1', 'L2', 15 from dual union all select 'loc2', 'L3', 11 from dual union all select 'loc3', 'L1', 9 from dual ) --end of sample data select location_code, max(decode(line_code, 'L1', value, null)) L1, max(decode(line_code, 'L2', value, null)) L2, max(decode(line_code, 'L3', value, null)) L3, max(decode(line_code, 'L4', value, null)) L4, max(decode(line_code, 'L5', value, null)) L5, max(decode(line_code, 'L6', value, null)) L6 from t group by location_code
Here I used the WITH clause as an example of table. You can replace the "t" table name in the select statement with your actual table and remove the WITH clause.
-
Convert the lines into columns... very urgent
Hello
I have a small doubt... I create it follows and insert commands.
CREATE THE TABLE SCOTT. ABC
(
ROOM_NAME VARCHAR2 (20 BYTE),
MET_MONTH VARCHAR2 (2 BYTE),
NUMBER OF TOTAL_SEATS
);
TOGETHER TO DEFINE
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("Synergy Room 3', '06' 18");
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("NP2-18', ' 08,' 35 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("Synergy room 2', '08' 50 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("Synergy room 2', '05' 20 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("NP5-26 ', ' 05,' 10 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("NP2-18 ', ' 11', 35 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("NP3-49 ', ' 07,' 10 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("Synergy Room1", '08', 45);
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("Wallace Stevens', '09', 95 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
('Synergy Room1', '04', 73);
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("Wallace Stevens ', 10', 205 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("Wallace Stevens', '04', 100 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("Synergy Room 3', '04' 25 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("NP3-49 ', ' 04', 8 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("Synergy Room 3', '07' 10 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("NP5-26', ' 07,' 13 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("NP2-18', ' 05,' 28 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("NP3-49 ', ' 09', 15 ');
Insert into ABC
(ROOM_NAME, MET_MONTH, TOTAL_SEATS)
Values
("NP5-26 ', ' 10', 15 ');
COMMIT;
So I need to get the result like this:
RoomName Jan Feb Mar Apr May June July Aug 7 Oct Nov Dec
Synergy room 1 0 0 0 73 0 0 0 45 0 0 0 0
Synergy room 2 0 0 0 0 20 0 0 50 0 0 0 0
Synergy Room 3 0 0 0 25 0 18 10 0 0 0 0 0
Wallace Stevens 0 0 0 100 0 0 0 0 95 205 0 0
NP5-26 0 0 0 0 10 0 13 0 0 15 0 0
NP2-18 0 0 0 0 28 0 0 35 0 0 35 0
NP3-49 0 0 0 0 0 0 10 0 15 0 0 0
Thank you
RajivIf you do not want null values then try like this
NVL (sum (decode (met_month, ' 01', total_seats)), 0) such as jan,.
S
-
Convert the lines into a single column
create table suresh
(
Identification number,
ch char (1)
)
;
Insert in suresh values(1,'i');
Insert in suresh values(1,'a');
Insert in suresh values(1,'m');
Insert in suresh values(1,'b');
Insert in suresh values(1,'o');
Insert in suresh values(1,'y');
Select * from suresh
ID ch
1 I
1 a
1 m
1 b
1 o
1 y
I'm looking for output something like this
ID ch
1 iamboy
..select id,listagg(ch) within group(order by ch) as ch from suresh group by id
-
Divide the line into several columns
Hi all
I have a question where I want to divide the line into multiple columns based on the delimiter ' |'.
Staging of Table structure: People_STG, I have people in it.
Create table People_STG(col1 varchar2(4000));
Insert into People_STG(Emp_id|) User name | FirstName. LastName. JobTitle | hire_date | Location_id)
SELECT REGEXP_SUBSTR ( COL1, '[^,|]+', 1, 1) EMP_ID, REGEXP_SUBSTR ( COL1, '[^|]+', 1, 2) USERNAME, REGEXP_SUBSTR ( COL1, '[^,|]+', 1, 3) FIRSTNAME, REGEXP_SUBSTR ( COL1, '[^,|]+', 1, 4) LASTNAME, REGEXP_SUBSTR ( COL1, '[^,|]+', 1, 5) JOBTITLE, REGEXP_SUBSTR ( COL1, '[^,|]+', 1, 6) HIRE_DATE, REGEXP_SUBSTR ( COL1, '[^,|]+', 1, 7) LOCATION_ID FROM PEOPLE_STG WHERE length(regexp_replace(COL1, '[^|]', '')) = 14;
But I am facing a problem here, as in some lines, function is null, but when I run the above query, it is not recognizing the empty element and inserting Hire_date values of function and location_id in Hire_date where function is null.
for example: 1 | akash51 | Akaksh | C | 22/11/14 | 15
Here the function is null, so when I run top to divide query it will insert 11/22/14 in the function column and 15 in Hire_Date.
Please need help on this one.
Oracle Version: 11.2 g
Thanks in advance,
Akash.
There are different techniques to cope with this. Is a simple...
SQL > ed
A written file afiedt.buf1 with t as (select 1 | akash51 |) Akaksh | C | 22/11/14 | 15' as col1 of union double all the
2 Select 2 | akash52 | Akaksh | C | Jobs jobs | 23/11/14 | 15' of the double
3 )
4 --
5. end of test data
6 --
7 select trim (REGEXP_SUBSTR (COL1, ' [^, |] +', 1, 1)) EMP_ID,.
8 toppings (REGEXP_SUBSTR (COL1, ' [^, |] +', 1, 2)) USERNAME,.
9 garnish (REGEXP_SUBSTR (COL1, ' [^, |] +' 1, 3)) FIRSTNAME,
10 pads (REGEXP_SUBSTR (COL1, ' [^, |] +' 1, 4)) LASTNAME,.
11 garnish (REGEXP_SUBSTR (COL1, ' [^, |] +' 1, 5)) JOBTITLE,.
12 pads (REGEXP_SUBSTR (COL1, ' [^, |] +', 1, 6)) HIRE_DATE,.
13 garnish (REGEXP_SUBSTR (COL1, ' [^, |] +' 1, 7)) location_id
14 * from (select replace (col1, ' |)) ',' | as col1 of t)
SQL > /.EMP_ID USERNAME FIRSTNAME LASTNAME, JOBTITLE HIRE_DATE LOCATION_I
---------- ---------- ---------- ---------- ---------- ---------- ----------
1 akash51 Akaksh C 22/11/14 15
2 akash52 Akaksh C Job 23/11/14 15 -
Who can give me a hand to settle the question below? I use the db 11G for two or three days. This works. But now it displays information below, it blocks my server needs to be in place. Can anyone help to take a look? And explain to me what is meaning with below question?
Thank you
java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00201: identifier ' INVALIDHTOMEH. KEEP ' should be declared
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:138)
11/08/05 02:18:17-oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:316)
11/08/05 02:18:17-oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:282)
11/08/05 02:18:17-oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:639)
11/08/05 02:18:17-oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:184)
11/08/05 02:18:17-oracle.jdbc.driver.T4CCallableStatement.execute_for_rows(T4CCallableStatement.java:873)
11/08/05 02:18:17-oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1161)
11/08/05 02:18:17-oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3001)
11/08/05 02:18:17-oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3093)
11/08/05 02:18:17-oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4286)
11/08/05 02:18:17-com.agile.pc.cmserver.util.OracleIDGenerator.pinSequence(OracleIDGenerator.java:245)
11/08/05-02:18:17-com.agile.pc.cmserver.util.OracleIDGenerator. < init > (OracleIDGenerator.java:62)
11/08/05 02:18:17-com.agile.pc.cmserver.util.CMUtilReflection.getOracleIDGenerator(CMUtilReflection.java:79)
11/08/05 02:18:17-com.agile.util.dao.IDGenerator.getNewInstance(IDGenerator.java:78)
11/08/05 02:18:17-com.agile.util.dao.IDGenerator.getInstance(IDGenerator.java:66)
11/08/05 02:18:17-com.agile.util.dao.IDGenerator.getNextID(IDGenerator.java:91)
11/08/05-02:18:17-com.agile.util.AppServerId. < clinit > (AppServerId.java:14)
11/08/05 02:18:17-com.agile.startup.PCMStartupImpl.start(PCMStartupImpl.java:115)
11/08/05 02:18:17-com.agile.ui.pcm.InitServlet.init(InitServlet.java:63)
11/08/05 02:18:17-com.evermind.server.http.HttpApplication.loadServlet(HttpApplication.java:2379)
11/08/05 02:18:17-com.evermind.server.http.HttpApplication.findServlet(HttpApplication.java:4830)
11/08/05 02:18:17-com.evermind.server.http.HttpApplication.findServlet(HttpApplication.java:4754)
11/08/05 02:18:17-com.evermind.server.http.HttpApplication.initPreloadServlets(HttpApplication.java:4942)
11/08/05 02:18:17-com.evermind.server.http.HttpApplication.initDynamic(HttpApplication.java:1144)
11/08/05-02:18:17-com.evermind.server.http.HttpApplication. < init > (HttpApplication.java:741)
11/08/05 02:18:17-com.evermind.server.ApplicationStateRunning.getHttpApplication(ApplicationStateRunning.java:431)
11/08/05 02:18:17-com.evermind.server.Application.getHttpApplication(Application.java:586)
11/08/05 02:18:17-com.evermind.server.http.HttpSite$ HttpApplicationRunTimeReference.createHttpApplicationFromReference (HttpSite.java:1987)
11/08/05 02:18:17-com.evermind.server.http.HttpSite$ HttpApplicationRunTimeReference. < init > (HttpSite.java:1906)
11/08/05 02:18:17-com.evermind.server.http.HttpSite.initApplications(HttpSite.java:643)
11/08/05 02:18:17-com.evermind.server.http.HttpSite.setConfig(HttpSite.java:290)
11/08/05 02:18:17-com.evermind.server.http.HttpServer.setSites(HttpServer.java:270)
11/08/05 02:18:17-com.evermind.server.http.HttpServer.setConfig(HttpServer.java:177)
11/08/05 02:18:17-com.evermind.server.ApplicationServer.initializeHttp(ApplicationServer.java:2493)
11/08/05 02:18:17-com.evermind.server.ApplicationServer.setConfig(ApplicationServer.java:1042)
11/08/05 02:18:17-com.evermind.server.ApplicationServerLauncher.run(ApplicationServerLauncher.java:131)
Published by: user774630 on November 4, 2008 18:57# com.agile.pc.cmserver.util.OracleIDGenerator.pinSequence(OracleIDGenerator.java:245)
This method tries to pin a sequence in the Shared Pool so that it is not recharged. Reloading can cause sequence numbers to be ignored, if I remember correctly.
Pinning is done using packed the INVALIDHTOMEH procedure. KEEP. The error is reported because either the package INVALIDHTOMEH is not created in the database (the $ORACLE_HOME/rdbms/admin/dbmspool.sql script creates) or the user who connects to the database have not run privilege for the package.Note that the package has access to the shared swimming pool. Execute privilege to grant only trusted users.
-Sergiusz
-
SQL Loader - ignore the lines with "rejected - all null columns."
Hello
Please see the attached log file. Also joined the table creation script, data file and the bad and throw the files after execution.
Sqlldr customer in the version of Windows-
SQL * Loader: release 11.2.0.1.0 - Production
The CTL file has two clauses INTO TABLE due to the nature of the data. The data presented are a subset of data in the real world file. We are only interested in the lines with the word "Index" in the first column.
The problem we need to do face is, according to paragraph INTO TABLE appears first in the corresponding CTL lines file to the WHEN CLAUSE it would insert and the rest get discarded.
1. statement of Create table : create table dummy_load (varchar2 (30) name, number, date of effdate);
2. data file to simulate this issue contains the lines below 10. Save this as name.dat. The intention is to load all of the rows in a CTL file. The actual file would have additional lines before and after these lines that can be discarded.
H15T1Y Index | 2. 19/01/2016 |
H15T2Y Index | 2. 19/01/2016 |
H15T3Y Index | 2. 19/01/2016 |
H15T5Y Index | 2. 19/01/2016 |
H15T7Y Index | 2. 19/01/2016 |
H15T10Y Index | 2. 19/01/2016 |
CPDR9AAC Index | 2. 15/01/2016 |
MOODCAVG Index | 2. 15/01/2016 |
H15TXXX Index | 2. 15/01/2016 |
H15TXXX Index | 2. 15/01/2016 |
3. the CTL file - name.ctl
DOWNLOAD THE DATA
ADD
IN THE TABLE dummy_load
WHEN (09:13) = "Index".
TRAILING NULLCOLS
(
COMPLETED name BY ' | ',.
rate TERMINATED BY ' | '.
COMPLETED effdate BY ' | '. ' TO_DATE (: effdate, "MM/DD/YYYY").
)
IN THE TABLE dummy_load
WHEN (08:12) = "Index".
TRAILING NULLCOLS
(
COMPLETED name BY ' | ',.
rate TERMINATED BY ' | '.
COMPLETED effdate BY ' | '. ' TO_DATE (: effdate, "MM/DD/YYYY").
)
invoke SQL loader in a file-> beats
C:\Oracle\product\11.2.0\client\bin\sqlldr USERID = myid/[email protected] CONTROL=C:\temp\t\name.ctl BAD=C:\temp\t\name_bad.dat LOG=C:\temp\t\name_log.dat DISCARD=C:\temp\t\name_disc.dat DATA=C:\temp\t\name.dat
Once this is run, the following text appears in the log file (excerpt):
Table DUMMY_LOAD, charged when 09:13 = 0X496e646578 ('Index' character)
Insert the option in effect for this table: APPEND
TRAILING NULLCOLS option in effect
Column Position Len term Encl. Datatype name
------------------------------ ---------- ----- ---- ---- ---------------------
NAME FIRST * | CHARACTER
RATE NEXT * | CHARACTER
EFFDATE NEXT * | CHARACTER
SQL string for the column: ' TO_DATE (: effdate, "MM/DD/YYYY").
Table DUMMY_LOAD, charged when 08:12 = 0X496e646578 ('Index' character)
Insert the option in effect for this table: APPEND
TRAILING NULLCOLS option in effect
Column Position Len term Encl. Datatype name
------------------------------ ---------- ----- ---- ---- ---------------------
NAME NEXT * | CHARACTER
RATE NEXT * | CHARACTER
EFFDATE NEXT * | CHARACTER
SQL string for the column: ' TO_DATE (: effdate, "MM/DD/YYYY").
Record 1: Ignored - all null columns.
Sheet 2: Cast - all null columns.
Record 3: Ignored - all null columns.
Record 4: Ignored - all null columns.
Sheet 5: Cast - all null columns.
Sheet 7: Discarded - failed all WHEN clauses.
Sheet 8: Discarded - failed all WHEN clauses.
File 9: Discarded - failed all WHEN clauses.
Case 10: Discarded - failed all WHEN clauses.
Table DUMMY_LOAD:
1 row loaded successfully.
0 rows not loaded due to data errors.
9 lines not loading because all WHEN clauses were failed.
0 rows not populated because all fields are null.
Table DUMMY_LOAD:
0 rows successfully loaded.
0 rows not loaded due to data errors.
5 rows not loading because all WHEN clauses were failed.
5 rows not populated because all fields are null.
The bad file is empty. The discard file has the following
H15T1Y Index | 2. 19/01/2016 |
H15T2Y Index | 2. 19/01/2016 |
H15T3Y Index | 2. 19/01/2016 |
H15T5Y Index | 2. 19/01/2016 |
H15T7Y Index | 2. 19/01/2016 |
CPDR9AAC Index | 2. 15/01/2016 |
MOODCAVG Index | 2. 15/01/2016 |
H15TXXX Index | 2. 15/01/2016 |
H15TXXX Index | 2. 15/01/2016 |
Based on the understanding of the instructions in the CTL file, ideally the first 6 rows will have been inserted into the table. Instead the table comes from the line 6' th.
NAME RATE EFFDATE H15T10Y Index 2 January 19, 2016 If the INTO TABLE clauses were put in the CTL file, then the first 5 rows are inserted and the rest are in the discard file. The line 6' th would have a ""rejected - all columns null. "in the log file. "
Could someone please take a look and advise? My apologies that the files cannot be attached.
Unless you tell it otherwise, SQL * Loader assumes that each later in the table and what clause after the first back in the position where the previous left off. If you want to start at the beginning of the line every time, then you need to reset the position using position (1) with the first column, as shown below. Position on the first using is optional.
DOWNLOAD THE DATA
ADD
IN THE TABLE dummy_load
WHEN (09:13) = "Index".
TRAILING NULLCOLS
(
name POSITION (1) TERMINATED BY ' | '.
rate TERMINATED BY ' | '.
COMPLETED effdate BY ' | '. ' TO_DATE (: effdate, "MM/DD/YYYY").
)
IN THE TABLE dummy_load
WHEN (08:12) = "Index".
TRAILING NULLCOLS
(
name POSITION (1) TERMINATED BY ' | '.
rate TERMINATED BY ' | '.
COMPLETED effdate BY ' | '. ' TO_DATE (: effdate, "MM/DD/YYYY").
)
-
SQL Loader with the line terminator
Dear friends,
I use Oracle 10 g R2.
Need to load some records to a text file to Oracle.
It's my control file:
and received the error:LOAD DATA INFILE 'D:\load\dat\FinNote.dat' BADFILE 'D:\load\bad\FinNote.bad' DISCARDFILE 'D:\load\dat\discard\FinNote.dsc' INSERT INTO TABLE FinNote FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS ( IDNo, RegDate date 'YYYY-MM-DD HH24:MI:SS', Col1, Remark, UserID, Status) {code} My text file has semicolon as a row delimiter and | as column delimiter. Now, how can I add the semicolon as row delimiter. Please guide me. I followed the method given in this site: http://www.exforsys.com/tutorials/oracle-10g/oracle-10g-sql-loader-input-data-and-datafiles.html *INFILE datafile_name ["str terminator_string"]* {code} LOAD DATA INFILE 'D:\load\dat\CRS_FinNote.dat' ";" BADFILE 'D:\load\bad\CRS_FinNote.bad' DISCARDFILE 'D:\load\dat\discard\CRS_FinNote.dsc' INSERT INTO TABLE CRS_FinNote FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS ( StudentNo, IDate date 'YYYY-MM-DD HH24:MI:SS', Topic, Remark, UserID, Status)
SQL * Loader-500: could not open the file (D:\load\dat\CRS_FinNote.dat)
SQL * Loader-555: unrecognized property treatment option
SQL * Loader-509: System error: the operation completed successfully.
SQL * Loader-2026: the charge was dropped because SQL Loader cannot continue.
SQL * Loader-513: cannot close the file (D:\load\dat\CRS_FinNote.dat)
SQL * Loader-559: Error closing file
SQL * Loader-509: System error: the storage control block address is invalid.
Thanks in advance.
Nith
Published by: user645399 on June 16, 2010 10:35And, if your CRS_FinNote.dat look like below (line/record delimited; then)
1245|2000-05-16|Oracle|The Best|AAA|Online; 294|2000-04-03|Dotnet|Best|BBB|Pending; 300|2010-03-28|C++|The Best|CCC|Offline;
Scott@ORCL > select utl_raw.cast_to_raw (';') under "hexadecimal;" double;
in hexadecimal
--------------------------------------------------------------------------------
3BThen I change user645399.ctl with below:
LOAD DATA INFILE 'd:\csv\CRS_FinNote.dat' "str X'3B'" BADFILE 'd:\csv\FinNote.bad' DISCARDFILE 'd:\csv\FinNote.dsc' INSERT INTO TABLE FinNote FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS ( IDNo, RegDate date 'YYYY-MM-DD HH24:MI:SS', Col1, Remark, UserID, Status)
The idea received a link from http://www.orafaq.com/forum/t/140062/0/.
C:\Documents and Settings\Girish Sharma>sqlldr scott/tiger@orcl control=d:\csv\user645399.ctl log=user645399.log Enter user-name: scott/tiger Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production With the Partitioning, OLAP and Data Mining options SCOTT@orcl> select * from finnote; IDNO REGDATE COL1 REMARK USERID STATUS ---------- -------------------- ---------- ---------- ---------- ---------- 1245 16-may-2000 00:00:00 Oracle The Best AAA Online 294 03-apr-2000 00:00:00 Dotnet Best BBB Pending 300 28-mar-2010 00:00:00 C++ The Best CCC Offline SCOTT@orcl>
HTH
Girish Sharma -
I have a table which has used and services
I have question
Select d.department_name, count (*)
e employees, departments d
where
e.department_id = d.department_id
D.department_name group;
This will return charges and the name of the Department.
Can any body guide me how to display these rows into columns.
using pivot or some other way.
Thank you
LavanIt is explained, including examples in the SQL and PL/SQL FAQ: {message identifier: = 9360005}
If you need more help then post a unit test and your version of the database , as explained here: {message identifier: = 9360002} -
How to view the line of columns without using the pivot keyword
Hello
could someone help me how to display lines in columns without using the keyword pivot and actuall is my scenario, iam having two tables with names and examples of data is shown below
MIDDLE MINAME TASKID TASKNAME IDENTIFICATION PROJECT
1 PROJ1 1 AA 100 PR1_TASK1
1 PROJ1 3 CC PR1_TASK3 102
1 PROJ1 DD 4 103 PR1_TASK4
1 PROJ1 EE 5 104 PR1_TASK5
1 PROJ1 6 105 FF PR1_TASK6
2 PROJ2 EE 5 114 PR2_TASK1
2 PROJ2 6 115 FF PR2_TASK2
2 PROJ2 GG 7 116 PR2_TASK3
2 PROJ2 HH 8 117 PR2_TASK4
2 PROJ2 9 118 PR2_TASK5 JJ
2 PROJ2 KK 10 119 PR2_TASK6
2 PROJ2 1 AA PR2_TASK7 120
The output should show the project and County of tasks at a given stage as shown below
project AA BB CC DD EE FF GG HH JJ KK
1 2 0 1 5 3 2 0 2 1 0
2 1 2 0 2 1 0 2 4 3 1
Thanks in advance,
VVRCREATE TABLE pivot_test ( ID NUMBER, PROJECT VARCHAR2(10), MID NUMBER, MINAME VARCHAR2(5), TASKID NUMBER, TASKNAME VARCHAR2(10) ); INSERT INTO pivot_test VALUES (1, 'PROJ1', 1, 'AA', 100, 'PR1_TASK1'); INSERT INTO pivot_test VALUES (1, 'PROJ1', 3, 'CC', 102, 'PR1_TASK3'); INSERT INTO pivot_test VALUES (1, 'PROJ1', 4, 'DD', 103, 'PR1_TASK4'); INSERT INTO pivot_test VALUES (1, 'PROJ1', 5, 'EE', 104, 'PR1_TASK5'); INSERT INTO pivot_test VALUES (1, 'PROJ1', 6, 'FF', 105, 'PR1_TASK6'); INSERT INTO pivot_test VALUES (2, 'PROJ2', 5, 'EE', 114, 'PR2_TASK1'); INSERT INTO pivot_test VALUES (2, 'PROJ2', 6, 'FF', 115, 'PR2_TASK2'); INSERT INTO pivot_test VALUES (2, 'PROJ2', 7, 'GG', 116, 'PR2_TASK3'); INSERT INTO pivot_test VALUES (2, 'PROJ2', 8, 'HH', 117, 'PR2_TASK4'); INSERT INTO pivot_test VALUES (2, 'PROJ2', 9, 'JJ', 118, 'PR2_TASK5'); INSERT INTO pivot_test VALUES (2, 'PROJ2', 10, 'KK', 119, 'PR2_TASK6'); INSERT INTO pivot_test VALUES (2, 'PROJ2', 1, 'AA', 120, 'PR2_TASK7'); SELECT ID as PROJECT, SUM(DECODE(miname, 'AA', 1, 0)) AS AA, SUM(DECODE(miname, 'BB', 1, 0)) AS BB, SUM(DECODE(miname, 'CC', 1, 0)) AS CC, SUM(DECODE(miname, 'DD', 1, 0)) AS DD, SUM(DECODE(miname, 'EE', 1, 0)) AS EE, SUM(DECODE(miname, 'FF', 1, 0)) AS FF, SUM(DECODE(miname, 'GG', 1, 0)) AS GG, SUM(DECODE(miname, 'HH', 1, 0)) AS HH, SUM(DECODE(miname, 'JJ', 1, 0)) AS JJ, SUM(DECODE(miname, 'KK', 1, 0)) AS KK FROM pivot_test GROUP BY ID; PROJECT AA BB CC DD EE FF GG HH JJ KK ------- -- -- -- -- -- -- -- -- -- -- 1 1 0 1 1 1 1 0 0 0 0 2 1 0 0 0 1 1 1 1 1 1
-
I used several worksheets. More allows me to specify the rows/columns and then delete them. This is to remove all the lines/columns beyond those set up and formatted for my spreadsheet. I tried many ways to remove all additional lines/columns, but without success. I even took the time to scroll down/on to highlight all the unnecessary lines and columns, but they do not remove it.
Any suggestions? I used spreadsheets most allow me to specify the number of rows/columns for my worksheetYou can ask your question in the office | Excel Forum because it is not really a problem of Windows XP performance and maintenance.
Office | Excel
http://answers.Microsoft.com/en-us/Office/Forum/Excel
Also, providing specific information may help to get a faster response or better, such as the version of Office or Excel? version of Windows XP? Home, Pro, 32-bit or 64-bit.
I hope this helps.
-
Need help for the conversion of the lines in columns
Hi all
I have a table with 2 columns.
colId value
1 aaa
2 bbb
3 ccc
1 ddd
Eee 2
3 fff
I want to store the data in the table above in another table that has 3 columns.
col1 col2 col3
AAA bbb ccc
DDD eee fff
I am pivot query. But I don't get it properly. Help, please.
I have Oracle Database 11 g Enterprise Edition Release 11.1.0.6.0 - 64 bit Production
Thanks in advance,
Girish G
Published by: Girish G July 28, 2011 01:28Girish G wrote:
Hey Tubby,Let me explain the real-world scenario.
I'm the external source CLOB data in oracle stored procedure.
The data are coming in the form below.
col1 # | #col2 # | #col3 ~ | ~ col1 # | #col2 # | #col3 ~ | ~ col1 # | #col2 # | #col3
# Here. #-> is the column delimiter.
and ~ | ~-> is the line delimiter.I want to store these data in a table that has 3 columns.
My approach was to extract the data for each column and store it in a temporary table in separate lines. Then move the data from the temporary table to the destination table.
Are there other alternatives for my requirement? Please suggest.
Thank you
Girish GMuch better when you show us the context like that.
It's late and I have sleepiness in my bones, so it's not likely optimal.
select regexp_substr(split, '[^@]+', 1, 1) as col1 , regexp_substr(split, '[^@]+', 1, 2) as col2 , regexp_substr(split, '[^@]+', 1, 3) as col3 from ( select replace(regexp_substr(source_str, '[^@]+', 1, level), '#|#', '@') as split from ( select replace('val1#|#val2#|#val3~|~val4#|#val5#|#val6~|~val7#|#val8#|#val9', '~|~', '@') as source_str from dual ) connect by level <= length(source_str) - length (replace(source_str, '@') ) + 1 );
I do not have an instance running (tested on XE) 11 so I can't use "magical" things like regexp_count and fun stuff. This should give you a basic idea of how to analyze data well.
I decode your delimiters in something "more manageable" just because it's easier than worrying about the escaping of special characters and all that fun stuff I'm too asleep to try.
Since you are dealing with a CLOB (you actually over 4,000 characters of data?) you have to give it up and look for a function in the pipeline as a suitable alternative.
-
How to make the lines dynimac columns in Oracle PL/SQL?
I have this request:
The result of this query is the following:select cso.branch, decode(NVL(cso.YES,0)+ NVL(cso.NO,0)+ NVL(cso.NA,0),0,'N','Y') ||' - '|| NVL(cso.officer,0) AS CSO_YN from ( SELECT A.BRAN_CODE_PK as branch, a.emp_code_pk as officer, COUNT(CASE WHEN D.STATUS = 1 THEN D.STATUS ELSE NULL END) YES, COUNT(CASE WHEN D.STATUS = 2 THEN D.STATUS ELSE NULL END) NO, COUNT(CASE WHEN D.STATUS = 3 THEN D.STATUS ELSE NULL END) NA FROM sales_team_accounts A, users U LEFT OUTER JOIN daily_check_lists D ON (D.USER_ID_PK = U.EMP_OFFICER_CODE and to_date(D.CHECK_LIST_DATE, 'DD-MON-YYYY') = '08-JUL-2010' ) -- date should be the parameter date) WHERE 1=1 AND A.EMP_OFFICER_CODE = U.EMP_OFFICER_CODE AND A.sale_type_fk = 101 -- CSO AND A.SUPV_EMP_CODE_FK != 0 and a.BRAN_CODE_PK = 1001 group by A.BRAN_CODE_PK, a.emp_code_pk ) cso;
The scenario is, there could be maximum 7 officers in a branch,BRANCH CSO_YN -------------------------------------- 1001 Y - 321 1001 N - 335 1001 Y - 90050 1001 N - 303
the total number of columns must therefore 7, and employee Y or N must be shaped in these 7 columns.
So for each branch, it will always be 1 row to have 7 columns:
So I want that it resembles the following:
Need to do using only the PL/SQL queryBRANCH CSO1 CSO2 CSO3 CSO4 CSO5 CSO6 CSO7 ----------------------------------------------------------------------------------------------------------- 1001 Y - 321 N - 335 Y - 90050 N - 303
Published by: imation3m on October 22, 2010 12:21http://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:766825833740
http://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:15151874723724
http://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:7644594042547 -
convert the data in the rows into columns
Hello
my table structure in this
The same employee might have to take two loans.i different need to display every ready as a separate... column detail likeEmp_id loancode interest principal 1 003 12 1000 1 004 12 2000 2 007 16 2500
I need to break all the different emp_id and soc_code (from another table)Empi_id soc_code loancode003 interestforloancode003 principal_for_loancode003 loancode004 i int_004 principal_for_loancode004
pls help
Kind regardsThe WITH clause is called subquery factoring (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#i2077142)
It is used to create tables of nickname, roughly the same as using inline views in the query.
for example, it's the same thing...
SQL> ed Wrote file afiedt.buf 1 select decode(tp,1,'Op/Bal','Cl/Bal') as type 2 ,max(case when period = 'JAN' and rn=1 then decode(tp,1,opn_bal,clo_bal) end) as Jan1 3 ,max(case when period = 'JAN' and rn=2 then decode(tp,1,opn_bal,clo_bal) end) as Jan2 4 ,max(case when period = 'JAN' and rn=3 then decode(tp,1,opn_bal,clo_bal) end) as Jan3 5 ,max(case when period = 'FEB' and rn=1 then decode(tp,1,opn_bal,clo_bal) end) as Feb1 6 ,max(case when period = 'FEB' and rn=2 then decode(tp,1,opn_bal,clo_bal) end) as Feb2 7 ,max(case when period = 'FEB' and rn=3 then decode(tp,1,opn_bal,clo_bal) end) as Feb3 8 from (select period, opn_bal, clo_bal, row_number() over (partition by period order by opn_bal) rn from dta), 9 (select rownum as tp from dual connect by rownum <= 2) 10* group by tp SQL> / TYPE JAN1 JAN2 JAN3 FEB1 FEB2 FEB3 ------ ---------- ---------- ---------- ---------- ---------- ---------- Op/Bal 2000 2200 2300 5000 Cl/Bal 3000 3300 3400 4500 SQL>
Simply, it makes it more tidy code and allows the same sets of data to be referenced multiple times in a query without duplicating the request online.
It is also useful to create sets of dummy test data to use (you will see people on the forum to use for that) so that we don't have to create tables in our database just to test with.
-
Divide the lines into different columns
HRCM_RECRUITMENT_NO amount facilities
R000035 MONEY, FOOD, BASIC 0,0,0
I did the result like this
Now, I want to like this
HRCM_RECRUITMENT_NO facilities Facilities_1 Facilities_2 amount Amount_1 Amount_2
R000035. CASH FOOD BASIC 0 0 0
Please help me! Tell me what to do?Hello
This will not work unless you have constant facilities for all HRCM_RECRUITMENT_NO.SQL> with h as 2 (select 'R000035' HRCM_RECRUITMENT_NO ,'CASH,FOOD,BASIC' Facilities ,'0,0,0' Amount from dual) 3 select HRCM_RECRUITMENT_NO,regexp_substr(Facilities,'[^,]+',1,1) f1, 4 regexp_substr(Facilities,'[^,]+',1,2) f2, 5 regexp_substr(Facilities,'[^,]+',1,3) f3, 6 regexp_substr(Amount,'[^,]+',1,1) A1, 7 regexp_substr(Amount,'[^,]+',1,2) A2, 8 regexp_substr(Amount,'[^,]+',1,3) A3 9 from h; HRCM_RE F1 F2 F3 A A A ------- ---- ---- ----- - - - R000035 CASH FOOD BASIC 0 0 0
Twinkle
Maybe you are looking for
-
Firefox will not change it's startup routine.
Whenever I run Firefox, it is as if it were the first time. Always wonder if I want to be by default (even if I changed the Options). Doesn't let me change the homepage. Do not take on new themes. I can update the bookmarks, but everything else is st
-
Installation/upgrade error!
Current version: 6.16.0.105 Window 7 Ultimate 64 Bit I get errors during the last Upgrade. Also tried installing standallone but the error messages are attached Any help? Thank you
-
help get out rossetta stone to locate my microphone
I recently installed rossetta stone version 3 in my computer when I was first done install I used the usb headset that came with it when I started, I had to choose a microphone and a set to the top you have to do that everytime you go into it worked
-
Question: The DNS in ISE 1.2 domain name may be different from the AD domain which ISE is attached to? Situation: I have an internal area of the AD "mydomain.local". ISE is currently Setup with mydomain.local as he dns domain is the domain name is i
-
access 32 bit files EXCEL question ODI
HelloI use 64-bit ODI 11.1.1.7.0 - on Windows 7-64 Bit machine. My version of MSOffice is 2007-32 bit.When I created the database server physical using ODI to access the EXCEL file, it causes the following error.java.sql.SQLException: [Microsoft] [OD