Converting multiple column in single column
Hi All,
I need to create a table with single column by using select statement with multiple columns
For Ex- i have 1 row with 10 columns (may be more than 10) like
'A','B','C','D','E','F','G','H',I','J'
i written sql like
select 'A','B','C','D','E','F','G','H','I','J' from dual
result is - 'A','B','C','D','E','F','G','H','I','J' with 10 columns
Now i need output lik this using SQL
Text
'A'
'B'
'C'
'D'
'E'
'F'
'G'
'H'
'I'
'J'
Please help me to sort out this problem. Your help would highly appropriated.
Regards
Raju
Edited by: 888667 on Apr 8, 2013 10:54 PM
Edited by: 888667 on Apr 8, 2013 10:56 PM
Welcome to the forum!!! You can convert column to row like this
SQL>with my_table
2 as
3 (
4 select 'A' col1,'B' col2,'C' col3,'D' col4,'E' col5,'F' col6,'G' col7,'H' col8,'I' col9,'J' col10
5 from dual
6 )
7 select decode(no , 1, col1, 2, col2, 3, col3, 4, col4, 5, col5, 6, col6, 7, col7, 8, col8, 9, col9, 10, col10) col
8 from my_table
9 cross join (select level no from dual connect by level <= 10)
10 /
C
A
B
C
D
E
F
G
H
I
J
10 rows selected.
SQL> To ask question in a better way please read {message:id=9360002} from FAQ
Similar Messages
-
Need multiple lines in single column of header using alv oops
Hi
I am using alv oops in a report.
I need multiple lines in single column of header.
In header of my report i am using 9 columns.
In the second column i need to split the line
Notification description/activity/work/activity long text/
as
Notification description/
activity/
work/
activity long text/
Please guide me to achieve this functionality.It is not possible to break the column description in the ALV disply.
Actually, if you have a longer description in one of the seltext_* field of the fiels catalog, it will automatically display it as a tooltip, if you put the mouse over the heading. In ALV OO, you can explicitly set the tooltip text.
Also you can get more help on the field pressing F1: If you use DDIC fields, it it automatic, otherwise you can do it as you like. -
How to Concat multiple columns to single column at the report level
Hi All,
I have a requirement in such a way that I have to concat multiple colulmns to single column at the report level.
First of all the column I am using in my report is "X" with datatype as Integer. Now if the length of this column "X" is <8 then I have add zero's (0) before the number.
And then I have to concat with a column "Y" and then cocnat with a column "Z" and finally the datatype should be casted to CHAR.
Can anyone please let me know the logic how I can add to my column so that it satisfies all the above.
Thanks All>Also, Another thing is if X < length of 8 Ihave to append with zero's..
>Ex: if the actual value of X is 123456 i wanted to show it as 000123456.
Try to be consistence what you said, you are looking for 8 length and above ex is 9 in length.
You need to learn many things one of those is CASE, at least do copy and also read
Forums Etiquette/Reward Points -
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 -
How to insert from one table to another (multiple columns to single column)
I want to insert data from table1 into table2 and the data looks in 3 columns as below
Table1
RepNm ObjNm DbNm
123 abc def
456 def xyz
789 123 456
and the data in table2 should be display as
Table 2
ObjNm
123
456
789
abc
def
xyz
the dupes should get eliminated and only the distinct values should be inserted into a single column in table2 from multiple columns in table1How do you want to handle nulls? If there is a null in any value of a column, do you want to insert it?
Then,
WITH T
AS (SELECT LEVEL colnum
FROM DUAL
CONNECT BY LEVEL <= 3)
SELECT DECODE (colnum, 1, RepNm, 2, ObjNm, 3, DbNm) as ObjNm
FROM table1, T;if you dont want to insert nulls then,
WITH T
AS (SELECT LEVEL colnum
FROM DUAL
CONNECT BY LEVEL <= 3)
SELECT DECODE (colnum, 1, RepNm, 2, ObjNm, 3, DbNm) AS ObjNm
FROM table1, T
WHERE DECODE (colnum, 1, RepNm, 2, ObjNm, 3, DbNm) = DECODE (colnum, 1, RepNm, 2, ObjNm, 3, DbNm)G. -
Merge multiple columns into single column?
Hi,
I need to execute queries dynamically and return the values. I need to fetch the values of the queries in single column only by concatenating the multiple columns. I cannot use PL/SQL in my scenario.
is there any way to execute a query and the result will be fetched in single column (values of multiple columns needs to be concatenated)?
Thanks,
Raja.hi,
do you mean this??
1* select EMPNO||' '||ENAME||' '||JOB||' '||MGR||' '||HIREDATE||' '||SAL||' '||COMM||' '||DEPTNO||
SQL> /
MULTIPLE_COL
100 JDF DIR 05-SEP-09 200 1000 10
7497 MILLER CLERK 7782 23-JAN-82 25000 195 35
7566 JONES MANAGER 7839 02-APR-81 3175 446.25 20
7654 RAR SALESMAN 7698 28-SEP-81 1450 1587.5 30
7698 BLAKE MANAGER 7839 01-MAY-81 3050 427.5 30
7782 CLARK MANAGER 7839 09-JUN-81 2650 367.5 10
7788 SCOTT ANALYST 7566 09-DEC-82 3200 450 20
7839 KING PRESIDENT 17-NOV-81 5200 8250 10
7844 TURNER SALESMAN 7698 08-SEP-81 1700 225 30
7876 ADAMS CLERK 7788 12-JAN-83 1300 165 20
7900 JAMES CLERK 7698 03-DEC-81 1150 85.5 30 [email protected]
MULTIPLE_COL
7902 FORD ANALYST 7566 03-DEC-81 3200 450 20
8000 KINGBABA PRESIDENT 17-NOV-81 5200 8250 10
8001 TURNER RAV SALESMAN 8000 08-SEP-81 1700 450 30
1001 KITTU DOR 05-SEP-09 1200 100 40
15 rows selected.Or
Mean this??
SQL> ed
Wrote file afiedt.buf
1 With T As
2 ( Select Level col1 From dual Connect By Level<=10
3 )
4 Select Max(SYS_CONNECT_BY_PATH(COL1||',',' ')) Multi_col
5 From
6 ( Select COL1, Lag(COL1) Over (Order By COL1) As Lag
7 From T )
8 Start With Lag Is Null
9 Connect By
10* Prior col1 = LAG
SQL> /
MULTI_COL
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,Edited by: user291283 on Sep 8, 2009 10:10 PM -
Convert different rows into single column
DB : 11.1.0.7
OS : Solaris Sparc 5.10
I have one query which is joining few tables and giving me output like below.
personnum orgnm
======= =======
6 Keyholder
9 Sales
3 Mgmt
I would like to convert that into single column like below.
col1
========
6,Keyholder,9,Sales,3,Mgmt
I have tried with pivot and decode, but not getting resule which I am exepcting. Any suggesstions ?yashwanth437 wrote:
listagg() function could work.LISTAGG is not available in 11.1. It was introduced in 11.2.
Anyway, 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. -
Indexes with multiple columns v/s multiple indexes with single column
Hi Everyone,
I have a table with below structure
Create Table Test_Table
Col1 int,
Col2 int,
Col3 int,
Col4 int,
Col5 int,
Col6 int,
Col7 int,
Col8 bigint
Col9 money
Col10 money
Now this table is being used in various reports. Since reports need various data, the filter clause for the report’s have various combination of table columns
Example, Report 1 can have query like
Select * from Test_Table where col1 = @val1
Report 2:
Select * from Test_Table where col1 = @val1 and col2 >=@val2
Report3:
Select * from Test_Table where col1 = @val1 and col2 >=@val2 and col3 =@val3 and col4 in(@val4)
Report4:
Select * from Test_Table where col1 = @val1 and col2 >=@val2 and col4 = @col4
And many more combinations.
Currently the table has many combinations of indexes
Index1: col1, col2
Index2: Col2
Index3: col1, col2, col4
Index4: col3 include (col9)
Index5: col1, col2, col3, col4, col9
Now I am into a performance issue where I have to create one more index with a slight variation from the existing combination of columns.
Can you please let me know instead of having these various combination of indexes can I create multiple indexes on individual columns.
Considering the above 4 examples which I have given for 4 reports, if I create 4 indexes separately on 4 individual columns col1, col2, col3, col4 will it be effective on all4 reports?
Please suggest me the best way to create indexes on this table
Thanks in advance,
RakshaDo you really need to bring all columns ? Do not use SELECT *.
Just a guess try index on col1, col2, col3, col4
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Collect Multiple Responses Into Single Column
I need a form that can collect multiple names and phone numbers and save the data in two columns. One column for names and one column for phone numbers.
How can I do this?My form looks like this:
Name:
Phone:
Name:
Phone:
Name:
Phone:
So responses come in looking like this:
Name: Phone: Name: Phone: Name: Phone:
John xxx-xxx-xxxx Jane xxx-xxx-xxxx Paul xxx-xxx-xxxx
I need them to look like this:
Name: Phone:
John xxx-xxx-xxxx
Jane xxx-xxx-xxxx
Paul xxx-xxx-xxxx -
How to add multiple columns to single column header
hi,
i wans to add two columns under a single column header in jtable how can i do this.By following some of these clever samples:
http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples1.html
Lots of different ideas on manipulating headers and columns there and on the proceeding pages. -
Merging values of CKFs in two columns into single column
Hi ,
I want to merge values present in two different columns.The values present in rows of one column are not present in rows of other column, I have to merge the values into a single column.so that in the single column i can get all the values.how to do it in BEx.
regards ,
man.Hi,
Dear murali , the solution you gave has helped me a bit as i am able to get the values for both the formula KF's in the same column,But another problem has arisen.
For both the formula KF's I had to choose 'New Selection' (and not 'New formula')after R-cllick on the structure, as I had to use a Characteristic as restriction for Each of the KF's, and also to use the Characteristic I had to associate it with some KF so that the Characteristic wil be used as a KF in the formula.
Now what is happening is that I am getting the values of that associated KF also in the empty rows.(i.e. the rows where the value of both the KF1 and KF2 is not present)
What has to be done to remove them and keep the empty rows empty and not show the value of the KF taken with the Characteristic for restriction. Also its value is summing up with KF1 and KF2 in the coulmn which is having both KF1 and KF2 together.
Regards
man -
Unique row based on two columns and single column
Dear Members,
I have a table which contains duplicate rows, for which a query should be able to fetch the unique row from the table. Here the unique is not based on one column, but it should be on two columns and also check for uniqueness on one column.
create table addr ( firstname varchar2(10), lastname varchar2(10), area varchar2(3));
insert into addr values('bob', 'james', '1');
insert into addr values('bob', 'james', '1');
insert into addr values('harry', 'bert', '1');
insert into addr values('jimmy', 'bert', '1');
insert into addr values('sam', 'mac', '1');
insert into addr values('sam', 'knight', '1');
insert into addr values('tom', 'sand', '1');
insert into addr values('cat', 'mud', '1');
The output of query should contain 3 rows.
bob - james
harry - bert or jimmy - bert [ either one of them, but not both ]
sam - mac or sam - knight [ either one of them, but not both ]
tom - sand
cat - mud
SELECT firstname, lastname as total from addr WHERE area = '1' GROUP by firstname,lastname; This does not take of single column duplication..
Any suggestions..SQL> with t_data
as
select 'bob' as firstname, 'james' as lastname, '1' as area from dual union all
select 'bob', 'james', '1' from dual union all
select 'harry', 'bert', '1' from dual union all
select 'jimmy', 'bert', '1' from dual union all
select 'sam', 'mac', '1' from dual union all
select 'sam', 'knight', '1' from dual union all
select 'tom', 'sand', '1' from dual union all
select 'cat', 'mud', '1' from dual
SELECT
firstname,
lastname,
area
FROM
SELECT
t.*,
row_number() over(partition BY firstname order by 1) rn,
row_number() over(partition BY lastname order by 1) rn1
FROM
t_data t
WHERE
rn = 1
AND rn1 =1 ;
FIRSTNAME LASTNAME AREA
bob james 1
cat mud 1
jimmy bert 1
sam knight 1
tom sand 1
SQL> -
Trying to convert multiple rows into multipe columns within a single row
I am trying to convert data from multiple rows into multiple columns. Let me see if I can paint the picture for you.
Here is a sample of the table i am trying to read from:
Company Name Account
1 Sam 123
1 Sam 234
1 Joe 345
1 Sue 789
1 Sue 987
1 Sue 573
I am trying to put this into a View that would have the data represented as such:
Company Name Acct1 Acct2 Acct3 Acct4
1 Sam 123 234 <null> <null>
1 Joe 345 <null> <null> <null>
1 Sue 789 987 573 <null>
Many thanks in advance for your help!test@XE> --
test@XE> with t as (
2 select 1 as company, 'Sam' as name, 123 as account from dual union all
3 select 1, 'Sam', 234 from dual union all
4 select 1, 'Joe', 345 from dual union all
5 select 1, 'Sue', 789 from dual union all
6 select 1, 'Sue', 987 from dual union all
7 select 1, 'Sue', 573 from dual)
8 --
9 select company,
10 name,
11 max(case when rn = 1 then account else null end) as acct1,
12 max(case when rn = 2 then account else null end) as acct2,
13 max(case when rn = 3 then account else null end) as acct3,
14 max(case when rn = 4 then account else null end) as acct4
15 from (select company,
16 name,
17 account,
18 row_number() over (partition by company, name order by 1) as rn
19 from t)
20 group by company, name;
COMPANY NAM ACCT1 ACCT2 ACCT3 ACCT4
1 Joe 345
1 Sam 234 123
1 Sue 573 789 987
3 rows selected.
test@XE>
test@XE>isotope -
Transform Multiple column into single column;
Hi all,
I have a table with multiple columns
Col_1
Col_2
Col1_3
Col_4
Col_5
Col1_6
Col_7
Col_8
Col1_9
Val_1
val_2
Val_3
Val_4
val_5
Val_6
Val_7
val_8
Val_9
Oper1
Oper2
Oper9
Currently the table look like this
ID Name | f_actve | Col_1| Col_2|….Col1_9| Val_1| val_2| …Val_9| Oper1…..| Oper9
Now I need to insert the data into new table The structure of the new table look like this
Table : T1 (header)
ID | Name | F_active
Table : T2 (detail )
Dtl ID | Columnname | Oper
Now all the column_name Col_1through Col_9 should be inserted into column_name column.
Same thing with the Opers
Table T3(detail )
DTL_ID | Column_Values
Now all the Column_Values Col_1through Col_9 should be inserted into column_name column
Please help
We have 10G /11g.
Thanks
RebeccaWhat you are trying to do is called unpivot in 11g.
See this example.
SQL> WITH T
2 AS (SELECT 1 dtl_id,
3 1 col1,
4 2 col2,
5 3 col3,
6 4 col4
7 FROM DUAL)
8 SELECT * FROM T;
DTL_ID COL1 COL2 COL3 COL4
1 1 2 3 4
SQL> WITH T
2 AS (SELECT 1 dtl_id,
3 1 col1,
4 2 col2,
5 3 col3,
6 4 col4
7 FROM DUAL)
8 SELECT dtl_id, "COL_VALUES"
9 FROM T UNPIVOT (col_values FOR abc IN (col1, col2, col3, col4));
DTL_ID COL_VALUES
1 1
1 2
1 3
1 4
SQL> -
Convert rows into a single column
create table suresh
id number,
ch char(1)
insert into suresh values(1,'i');
insert into suresh values(1,'a');
insert into suresh values(1,'m');
insert into suresh values(1,'b');
insert into suresh values(1,'o');
insert into suresh values(1,'y');
select * from suresh
id ch
1 i
1 a
1 m
1 b
1 o
1 y
I am looking for output somethign like this
id ch
1 iamboy
..Not for that data, no. You have a set of characters with no information about 'order', so they could appear in any order.
If you had some numbering against them, you could pivot the data and concatenate them, but in truth even the concatenation symbol "||" is a built in function, as well as the functions needed to pivot it...
SQL> ed
Wrote file afiedt.buf
1 with t as (select 1 as id, 'i' as ch from dual union all
2 select 2, 'a' from dual union all
3 select 3, 'm' from dual union all
4 select 4, 'b' from dual union all
5 select 5, 'o' from dual union all
6 select 6, 'y' from dual)
7 --
8 select max(decode(id,1,ch))||max(decode(id,2,ch))||max(decode(id,3,ch))||max(decode(id,4,ch))||max(decode(id,5,ch))||max(decode(id,6,ch))
9* from t
SQL> /
MAX(DE
iamboy
SQL>Whatever you do, there's going to be some built in functions somewhere.
Unless.... you do something horrendous like...
SQL> ed
Wrote file afiedt.buf
1 with t as (select 1 as id, 'i' as ch from dual union all
2 select 2, 'a' from dual union all
3 select 3, 'm' from dual union all
4 select 4, 'b' from dual union all
5 select 5, 'o' from dual union all
6 select 6, 'y' from dual)
7 --
8 select (select ch from t where id = 1)||
9 (select ch from t where id = 2)||
10 (select ch from t where id = 3)||
11 (select ch from t where id = 4)||
12 (select ch from t where id = 5)||
13 (select ch from t where id = 6)
14* from dual
SQL> /
(SELEC
iamboy
Maybe you are looking for
-
Windows 7 64 bit: impossible to install if you have a 1.1 MP ?
Hi all after 4 days of research, i have found the reason why either parallels 4 and bootcamp won;t install my 64 bit version of windows 7 and vista: basically the mac pro 1.1 has a 64 bit processor, but an EFI that is still at 32 bit, with the result
-
Hi all, I have 2 radio button in the selection screen. When I click the 1st button, Parameter1 should be displayed and when i clicked the 2nd button, Parameter2 should be displayed. kindly provide any sample code if possible. thanks in advance point
-
Premiere CC 2014 exporting H264 takes longer than Premiere CC ..?
Hi, I did some tests with exporting a Apple prores HQ clip of 1 minute with the standard H264 Youtube 1080p setting. And the results are a bit worrying. Direct export in Adobe Premiere Pro CC = 1 minute 28 seconds Direct export in Adobe Premiere Pro
-
Installation on ORACLE 11g R2 on Windows
Hi , In the installation phase of 11g r2 i got installed successfully 11g R2 on windows environment. but during installation i ignored some pop menu that stating like em.war not found. etc.. I just given sqlplus in cmd prompt. struck with error. Erro
-
Experts all, We are seeking a User-exit to modify the number of decimals using into the F110 when calculate the automatic payments. Thanks in advance for your support. OscarF.