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
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.
-
SQL to convert the lines into columns
Can someone please help with this? It sounds easy, but I can't do it on my own
I 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 AAAHello
It's called swing , and there are a few good links in the FAQ forum on this subject:
SQL and PL/SQL FAQExactly 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
-
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 different lines in a single column
DB: 11.1.0.7
Operating system: Solaris Sparc 5.10
I have a query that is joining a few tables and give me output like below.
personnum orgnm
======= =======
The 6 key holder
9 sales
3 Mgmt
I would like to only convert a single as column below.
col1
========
6, keeper of the key, 9, sales, 3, Mgmt
I tried with pivot and decode, but not get out that I'm exepcting. Any suggesstions?yashwanth437 wrote:
listagg() function might work.LISTAGG is not available in 11.1. It was introduced in 11.2.
In any case, XML solution:
with sample_table as ( select 6 personnum,'Keyholder' orgnm from dual union all select 9,'Sales' from dual union all select 3,'Mgmt' from dual ) select rtrim(xmlagg(xmlelement(e,personnum || ',' || orgnm,',').extract('//text()')),',') col1 from sample_table / COL1 --------------------------- 6,Keyholder,9,Sales,3,Mgmt SQL>
SY.
-
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 -
Convert lines to a single column
Hi all
Need help, I have a table where I want the output to a single column
ex: Select in t1. *
the query result_
rownum col_1
1 8217
2 6037
3-5368
4 5543
5 5232
I want the result to be: * 8217,6037,5368,5543,5232 *.
Thank you for your help in advance.
I search the web but couldn't find a solution that is easily understandable.WM_CONCAT is not documented, so not everyone would want to use it in production code.
However, SYS_CONNECT_BY_PATH might work:SQL> create table t as 2 select 1 rn, 8217 count_1 from dual union 3 select 2, 6037 from dual union 4 select 3, 5368 from dual union 5 select 4, 5543 from dual union 6 select 5, 5232 from dual; Table created. SQL> select * from t; RN COUNT_1 --------- ---------- 1 8217 2 6037 3 5368 4 5543 5 5232 5 rows selected. SQL> SQL> select rownum 2 , ltrim(sys_connect_by_path(count_1, ','), ',') count_1 3 from t 4 where connect_by_isleaf=1 5 start with t.rn=1 6 connect by t.rn = prior t.rn+1; ROWNUM ------- COUNT_1 ------------------------------------------------------------------------------------- 1 8217,6037,5368,5543,5232 1 row selected.
Or LISTAGG on 11.2:
SQL> select listagg(count_1, ',') within group (order by rn) agged from t; AGGED ------------------------- 8217,6037,5368,5543,5232 1 row selected.
I really hope that you do not really use ROWNUM as column name? I used instead RN...
-
TIFF of Import Options - flatten the layers into a single Image
Hi all
Running a script to open the files and convert them into another type. When it comes to a TIFF file type, called 'TIFF Import Options' dialog box opens. This window has a column of Options containing two pieces of music radio to "Convert layers to objects" and "flatten into a single Image. How to open the file with the whole "flatten the layers into a single image?
Thank you
OpenOptionsPhotoshop.preserveLayers = true;
-
Convert multiple lines in a single value separated by commas
Is is possible to convert multiple lines in a single value by concatenating the value of each line in OBIEE. I think that IO had seem a blog related to this by I'm not able to find him. Essentially, here's what I would do:
Number of customer location
101
101 NY
101 PA
102 TX
102 CA
This is to convert
101. HE, NY, PA
102 TX, CA
Can you get it someone please let me know if this is possible and how to operate it.
Thanks in advance for the help.You can do this by creating a DB function,
Visit this link
Re: Display of the horizontal valuesThank you
Vino -
Paint or Windows Photo Gallery
I try to combine a picture of my sister with a picture of a poem beside it, how to combine the two into a single photo?
If you have the poem recorded in an image format,
the following freeware can create a side by side
Panorama.(FWIW... it's always a good idea to create a system)
Restore point before installing software or updates)Download IrfanView
http://MajorGeeks.com/IrfanView_d4253.html
(filename: iview433_setup.exe)
(uncheck if you don't want Google Chrome)Download plug-ins too...
IrfanView plugins
http://MajorGeeks.com/IrfanView_PlugIns_d4908.html
(filename: irfanview_plugins_433_setup.exe)When the program is installed... read more...
Open IrfanView and go... Image / create the Image of the Panorama...
(this will open the screen to "Create a panorama image")On the screen to "Create a panorama image"... left click on the add images"" button.
(Displays the 'Open' screen. Now, drill down to the
the folder that contains your saved Photos of veterinarians.Now... click left (highlighted), the two images, you want to join.
(you can select more than one if you hold down your Ctrl key)
The two images highlight... left click on the button 'open '.
(Or... you can add the images one at a time... which is always easier)Now, go back to the screen "Create the panorama image.
and the file names of the selected pictures need to be in the
Field "Input Images.Now with the names of two files in 'Images of entry' field...
You can left click on the button 'create Image '.(the positions left and right of these images can be swapped in)
selection of a file name and using the "mount image" / "Move."
Images down"buttons...)Now you should see a display of the combined image.
Reach... File / save as...Choose a backup location / enter a file name / choose a format...
On the left, click on the button "Save..." -
Numbering of the lines based on two columns
Hello world
I´d would like to know if there is a way to achieve the numbering shown in the second table below indicating only Oracle native functions, like ROW_COUNT() on the partition, etc.
I m using Oracle 10 g.
The logic used is:
From 1, increment one each time that the ORIGIN is identical to the FIRST ORIGIN of the line (ID) group.
In order to compare the ORIGIN of each line with the FIRST ORIGIN of the group, I used the function LAG to create a column that will be the FIRST ORIGIN of the value in the group.ID ORIGIN DESTINATION ORDER ------------------------------------ 1 A B 1 1 B A 2 1 A B 3 1 B C 4 1 C A 5 ID ORIGIN DESTINATION ORDER NUMBERING -------------------------------------------------- 1 A B 1 1 1 B A 2 1 1 A B 3 2 1 B C 4 2 1 C A 5 2
However, I was not able to number the lines as shown above (column NUMBERING).
Any help will be much appreciated.
--------------------------------------------------------------------------------------------------------------
Test query:WITH T AS ( SELECT 1 ID, 'A' ORIGIN, 'B' DESTINATION, 1 ORDERING FROM DUAL UNION ALL SELECT 1 ID, 'B' ORIGIN, 'A' DESTINATION, 2 ORDERING FROM DUAL UNION ALL SELECT 1 ID, 'A' ORIGIN, 'B' DESTINATION, 3 ORDERING FROM DUAL UNION ALL SELECT 1 ID, 'B' ORIGIN, 'C' DESTINATION, 4 ORDERING FROM DUAL UNION ALL SELECT 1 ID, 'C' ORIGIN, 'A' DESTINATION, 5 ORDERING FROM DUAL ) SELECT T.ID , T.ORIGIN , T.DESTINATION , T.ORDERING , LAG (T.ORIGIN, T.ORDERING -1, 0) OVER (PARTITION BY T.ID ORDER BY T.ID , T.ORDERING) FIRST_ORIGIN_OF_GROUP FROM T ORDER BY T.ID , T.ORDERING
Hello
Here's one way:
WITH got_first_origin AS ( SELECT id, origin, destination, ordering , FIRST_VALUE (origin) OVER ( PARTITION BY id ORDER BY ordering ) AS first_origin FROM t ) SELECT id, origin, destination, ordering , COUNT ( CASE WHEN origin = first_origin THEN 1 END ) OVER ( PARTITION BY id ORDER BY ordering ) AS numbering FROM got_first_origin ;
This assumes that the combination of id and order is unique. Within an id, you place your order does not have to be consecutive integers, or something like that.
Analytical functions cannot be nested (the argument of the function of COUNTY anlytic can not call the analytical FIRST_VALUE function); The subquery is necessary.
You could do something with a LAG, as you have tried, rather than FIRST_VALUE, but you would still need a subquery, for the same reason. -
Merge multiple columns into a single column?
Hello
I need to perform queries dynamically and the return values. I need to retrieve the values of the queries in a single column only by concatenating multiple columns. I can't use the PL/SQL in my scenario.
is it possible to run a query and the result will be extracted in a single column (multiple column values must be concatenated)?
Thank you
Raja.SELECT rtrim(extract(xmltype(REPLACE(column_value,''',',')),'/ROW/*/text()'),',') col FROM TABLE (SELECT xmlsequence(dbms_xmlgen.getxmltype('select * from employees').extract('ROWSET/ROW')) FROM dual );
Check this box... Copy it directly to this
Ravi Kumar
-
Divide the values of a single column into multiple values of columns
I want that the values of the "col_val" column to divide into several values in the column
Table1:
Col_val Col1, Col2
Code1 POINT 45
L2 AB 45
L1 POINT 45 OF
Code2 CAB 61
ABC 51 LABORATORY
SSS LAB 45
QQQ BED 123
BBV COT 100
FFF COT 444
Output as expected:
OUT1 out2 out3-out4, out5
Code1 AB SSS
L1
Code2
ABC
QQQ
BBV
FFF
Each line must contain the values corresponding to the Col2 values and each column must have the values in Col1, as illustrated above.
SQL> with t 2 as 3 ( 4 select 'Code1' col_val, 'ITEM' col1, 45 col2 5 from dual 6 union all 7 select 'L2' col_val, 'AB' col1, 45 col2 8 from dual 9 union all 10 select 'L1' col_val, 'ITEM' col1, 45 col2 11 from dual 12 union all 13 select 'Code2' col_val, 'CAB' col1, 61 col2 14 from dual 15 union all 16 select 'ABC' col_val, 'LAB' col1, 51 col2 17 from dual 18 union all 19 select 'SSS' col_val, 'LAB' col1, 45 col2 20 from dual 21 union all 22 select 'QQQ' col_val, 'COT' col1, 123 col2 23 from dual 24 union all 25 select 'BBV' col_val, 'COT' col1, 100 col2 26 from dual 27 union all 28 select 'FFF' col_val, 'COT' col1, 444 col2 29 from dual 30 ) 31 select max(col1) 32 , max(col2) 33 , max(col3) 34 , max(col4) 35 , max(col5) 36 from ( 37 select decode(col1, 'ITEM', col_val) col1 38 , decode(col1, 'AB' , col_val) col2 39 , decode(col1, 'LAB' , col_val) col3 40 , decode(col1, 'CAB' , col_val) col4 41 , decode(col1, 'COT' , col_val) col5 42 , t.col2 col_2 43 , row_number() over(partition by col2, col1 order by 1) rno 44 from t 45 ) 46 group 47 by col_2, rno 48 order 49 by col_2; MAX(C MAX(C MAX(C MAX(C MAX(C ----- ----- ----- ----- ----- Code1 L2 SSS L1 ABC Code2 BBV QQQ FFF 7 rows selected. SQL>
-
SELECT SQL to merge the text of different lines into a single line
Hello
I need to create a Select statement to merge multiple lines in a single text. I have a table with the following data:
I need to create a query on the table to show a result like this:SQL> DESC doc_comments Name Null? Type ----------------------------------------------------- -------- ------------------------------------ DOC NUMBER COMMENTS VARCHAR2(30) SQL> SELECT * FROM doc_comments 2 / DOC COMMENTS ---------- ---------- 1 Note A 1 Note B 2 Note C 2 Note D 2 Note E 3 Note F 6 rows selected.
A possible solution would be to create a function to merge the text of the various lines and then use it in the query, but I'm not allowed to create any function in the database, I can use a simple SELECT statement.DOC COMMENTS ------------ -------------------------- 1 Note A, Note B 2 Note C, Note D, Note E 3 Note F
Any ideas on how I can do?Assuming that comments contain no vertical bar (|) character:
with doc_comments as ( select 1 doc,'Note A' comments from dual union all select 1,'Note B' from dual union all select 2,'Note C' from dual union all select 2,'Note D' from dual union all select 2,'Note E' from dual union all select 3,'Note F' from dual ) select doc, replace(ltrim(sys_connect_by_path(comments,'|'),'|'),'|',', ') comments from ( select doc, comments, row_number() over(partition by doc order by comments) rn from doc_comments ) where connect_by_isleaf = 1 start with rn = 1 connect by doc = prior doc and rn = prior rn + 1 order by doc / DOC COMMENTS ---------- ---------------------------------------- 1 Note A, Note B 2 Note C, Note D, Note E 3 Note F SQL>
And if the comments may contain the vertical bar (|), then replace the hose in the code above with a character that is not used in the comments.
SY.
-
Hello
I need to split into single column in the format below:
SELECT '6500,1100,3200,1233,9000' FROM DUAL;
Result:
6500
1100
3200
1233
9000
For 11g and above:
Select regexp_substr (6500, 1100, 3200, 1233, 9000', ' [^,] +', 1, level) suite
of the double
connect by level<= regexp_count('6500,1100,3200,1233,9000',="" ',')="" +="">=>
For 10g:
Select regexp_substr (6500, 1100, 3200, 1233, 9000', ' [^,] +', 1, level) suite
of the double
connect by level<= length="" (regexp_replace="" ('6500,1100,3200,1233,9000',="" '[^,]+'))="" +="">=>
or (if there is no empty entries):
Select regexp_substr (6500, 1100, 3200, 1233, 9000', ' [^,] +', 1, level) suite
of the double
connect regexp_substr (6500, 1100, 3200, 1233, 9000', ' [^,] +', 1, level) is not null
RESULT 6500 1100 3200 1233 9000 5 selected lines.
Maybe you are looking for
-
Thunderbird has worked perfectly on 9/3. This morning all of a sudden I can't send e-mail. The message is: an error occurred while sending mail. The mail server responded: authentication is needed before sending it [R0107005]. Please make sure that y
-
Can I Exchange my 6 for a more than 6?
I love my iPhone 6 but I got used to the size and would enjoy a much better, I was wondering if I could just trade in my 6 for most, it is undamaged, functioning normally
-
Need drivers and manual for Satellite Pro 4360
Hellopower led flashes after passage and the laptop does not start.Who knows where to find manual for Satellite Pro 4360? and where you can find drivers for windows 2000? TX, michtec
-
I laptot compaq 15-s109ng... and I have problem with my administrator I do not know the administrator password... I tried to open the password to open the deactivation of the system (86748487)
-
My Skype can't update - 7.17 Skype cannot be deleted.
I tried to update my Skype for the past two weeks, but now I get this error saying "unable to remove the old version of Skype 7.17." Contact your technical support group. "Then after I click ok it brings something say install Skype ' has failed. code