Sql to display rows like columns
Hi,
I am having table with 3 columns i.e col1, col2, col3
and the values are :
col1 col2 col3
1 11 A
1 11 B
1 11 C
2 22 P
2 22 Q
2 22 R
2 22 S
I require the output from the above data as follows :
1 11 A B C
2 22 P Q R S
rows of col3 to be converted into column.
Please suggest to get the about output thru SQL.
Thanks
Ramesh
Ok, but you're not gonna like it.... (ignore the bit above the select statment as that is just setting up the test data)
with t as (select 1 as col1, 11 as col2, 'a' as col3 from dual union
select 1, 11, 'b' from dual union
select 2, 22, 'j' from dual union
select 2, 22, 'k' from dual union
select 2, 22, 'l' from dual union
select 2, 33, 'v' from dual)
select col1, col2, max(col3_1)||max(col3_2)||max(col3_3)||max(col3_4)||max(col3_5)||max(col3_6)||max(col3_7)||max(col3_8)||max(col3_9)||max(col3_10) col3
from (
select col1, col2, decode(row_number() over (partition by col1, col2 order by col1, col2), 1, col3, null) col3_1, null col3_2, null col3_3, null col3_4, null col3_5, null col3_6, null col3_7, null col3_8, null col3_9, null col3_10 from t union
select col1, col2, null col3_1, decode(row_number() over (partition by col1, col2 order by col1, col2), 2, col3, null) col3_2, null col3_3, null col3_4, null col3_5, null col3_6, null col3_7, null col3_8, null col3_9, null col3_10 from t union
select col1, col2, null col3_1, null col3_2, decode(row_number() over (partition by col1, col2 order by col1, col2), 3, col3, null) col3_3, null col3_4, null col3_5, null col3_6, null col3_7, null col3_8, null col3_9, null col3_10 from t union
select col1, col2, null col3_1, null col3_2, null col3_3, decode(row_number() over (partition by col1, col2 order by col1, col2), 4, col3, null) col3_4, null col3_5, null col3_6, null col3_7, null col3_8, null col3_9, null col3_10 from t union
select col1, col2, null col3_1, null col3_2, null col3_3, null col3_4, decode(row_number() over (partition by col1, col2 order by col1, col2), 5, col3, null) col3_5, null col3_6, null col3_7, null col3_8, null col3_9, null col3_10 from t union
select col1, col2, null col3_1, null col3_2, null col3_3, null col3_4, null col3_5, decode(row_number() over (partition by col1, col2 order by col1, col2), 6, col3, null) col3_6, null col3_7, null col3_8, null col3_9, null col3_10 from t union
select col1, col2, null col3_1, null col3_2, null col3_3, null col3_4, null col3_5, null col3_6, decode(row_number() over (partition by col1, col2 order by col1, col2), 7, col3, null) col3_7, null col3_8, null col3_9, null col3_10 from t union
select col1, col2, null col3_1, null col3_2, null col3_3, null col3_4, null col3_5, null col3_6, null col3_7, decode(row_number() over (partition by col1, col2 order by col1, col2), 8, col3, null) col3_8, null col3_9, null col3_10 from t union
select col1, col2, null col3_1, null col3_2, null col3_3, null col3_4, null col3_5, null col3_6, null col3_7, null col3_8, decode(row_number() over (partition by col1, col2 order by col1, col2), 9, col3, null) col3_9, null col3_10 from t union
select col1, col2, null col3_1, null col3_2, null col3_3, null col3_4, null col3_5, null col3_6, null col3_7, null col3_8, null col3_9, decode(row_number() over (partition by col1, col2 order by col1, col2), 10, col3, null) col3_10 from t
) t
group by col1, col2
COL1 COL2 COL3
2 33 v
1 11 ab
2 22 jkl
SQL>
Similar Messages
-
Can anybody help....SQL to display row as column and column as rows
Can anybody help in writing a SQL to display row as column and column as rows?
Thankscheck this link:
Re: Creating Views - from rows to a new column? -
Looking for SQL to Display Rows as Columns in a View
Hi!
I am using Oracle 10g (10.1.0.4.0) 64 bit on Red Hat Enterprise Linux AS release 3.
I have the following tables:
Table A
A_ID number (primary key)
Table B
B_ID number (primary key)
B_NAME varchar2
B_DESC varchar2
Table C
C_ID number (primary key)
B_ID number (foreign key to table B)
A_ID number (foreign key to table A)
ORDERING number
A row in table A can have from 0 (zero) to 3 (three) rows in table C associated with it.
I am trying to make a view that displays A.A_ID and its associated rows from table C as a single row. For example, the following query:
select A.A_ID, C.B_ID, B.B_NAME, B.B_DESC, C.ORDERING
from C, B, A
where C.A_ID = A.A_ID
and C.B_ID = B.B_IDYields the following results:
A_ID B_ID B_NAME B_DESC ORDERING
100 10 A One 1
100 20 B Two 2
100 30 C Three 3I would like to get the following:
A_ID B_NAME_1 B_NAME_2 B_NAME_3
100 A B CThanks (in advance :-),
Avi.SQL> CREATE TABLE dt_test_a(a_id number)
2 /
Table created.
SQL> CREATE TABLE dt_test_b(b_id number, b_name varchar2(1),b_desc varchar2(10))
2 /
Table created.
SQL> CREATE TABLE dt_test_c(c_id number, b_id number, a_id number, ordering number)
2 /
Table created.
SQL>
SQL> insert into dt_test_a values(100)
2 /
1 row created.
SQL> insert into dt_test_b values(10, 'A','One')
2 /
1 row created.
SQL> insert into dt_test_b values(20, 'B','Two')
2 /
1 row created.
SQL> insert into dt_test_b values(30, 'C','Three')
2 /
1 row created.
SQL> insert into dt_test_c values(1, 10, 100, 1)
2 /
1 row created.
SQL> insert into dt_test_c values(2, 20, 100, 2)
2 /
1 row created.
SQL> insert into dt_test_c values(3, 30, 100, 3)
2 /
1 row created.
SQL>
SQL> SELECT
2 a.a_id,
3 DECODE(c.ordering, 1, b.b_name) b_name_1,
4 DECODE(c.ordering, 2, b.b_name) b_name_2,
5 DECODE(c.ordering, 3, b.b_name) b_name_3
6 FROM
7 dt_test_a a,
8 dt_test_b b,
9 dt_test_c c
10 WHERE
11 a.a_id = c.a_id
12 AND
13 b.b_id = c.b_id
14 /
A_ID B B B
100 A
100 B
100 C
SQL>
SQL> SELECT
2 a.a_id,
3 MAX(DECODE(c.ordering, 1, b.b_name)) b_name_1,
4 MAX(DECODE(c.ordering, 2, b.b_name)) b_name_2,
5 MAX(DECODE(c.ordering, 3, b.b_name)) b_name_3
6 FROM
7 dt_test_a a,
8 dt_test_b b,
9 dt_test_c c
10 WHERE
11 a.a_id = c.a_id
12 AND
13 b.b_id = c.b_id
14 GROUP BY
15 a.a_id
16 /
A_ID B B B
100 A B C
SQL>
SQL>
SQL> insert into dt_test_a values(200)
2 /
1 row created.
SQL> insert into dt_test_b values(40, 'A','One')
2 /
1 row created.
SQL> insert into dt_test_b values(50, 'B','Two')
2 /
1 row created.
SQL> insert into dt_test_b values(60, 'C','Three')
2 /
1 row created.
SQL> insert into dt_test_c values(4, 40, 200, 3)
2 /
1 row created.
SQL> insert into dt_test_c values(5, 50, 200, 2)
2 /
1 row created.
SQL> insert into dt_test_c values(6, 60, 200, 1)
2 /
1 row created.
SQL>
SQL> SELECT
2 a.a_id,
3 MAX(DECODE(c.ordering, 1, b.b_name)) b_name_1,
4 MAX(DECODE(c.ordering, 2, b.b_name)) b_name_2,
5 MAX(DECODE(c.ordering, 3, b.b_name)) b_name_3
6 FROM
7 dt_test_a a,
8 dt_test_b b,
9 dt_test_c c
10 WHERE
11 a.a_id = c.a_id
12 AND
13 b.b_id = c.b_id
14 GROUP BY
15 a.a_id
16 /
A_ID B B B
100 A B C
200 C B A -
Need help in displaying Rows to Columns
Hi,
I am facing problem in displaying Rows to Columns
I am using pivot function:
select *
from
(select vendor_name
from tablea)
pivot
(count(vendor_name)
for vendor_name in ('a,b,'c'));
its working fine showing vendor_name and count
but when i want to display the output as:(How to include the Salalry column in the query?)
Name:{a b c}
Sal Total:(400,600,800}
Any help will be needful for meNot sure what you mean:
select *
from (select deptno,sal from emp)
pivot(sum(sal) for deptno in (10,20,30))
10 20 30
8750 10875 9400
SQL> SY. -
How to display Rows as Columns in JSF?
I am using dataTable component to get data for my Menu, i have one column in it which returns me the data, because its automatically adding <tr> and <td> tags to it, its showing data in tabular form.
I want to show output Horizontally instead of Vertically the way its showing.
Is their any way i can display Rows as Columns?
Code:
==============================================
<div class="bodyarea">
<div id="location">
<h:dataTable value="#{menuItem.breadCrumb}" var="bread" >
<f:verbatim><ol></f:verbatim>
<h:column>
<f:verbatim><li></f:verbatim>
<h:outputLink id="crumbID" value="#{bread.menuLink}">
<h:outputText id="crumpName" value="#{bread.menuLabel}"/>
</h:outputLink>
<f:verbatim></li></f:verbatim>
</h:column>
<f:verbatim></ol></f:verbatim>
</h:dataTable>
</div>
</div>
Thank youTable is not the html element for you, in this situation.
I would use a dataList component (distributed with myfaces implementation).
dataList is able to return a list of item, and using css you will be able display in a horizontal grid. (otherwise you can use layout="grid" in dataList).
Anyway...my impression is that who made JSF was not really understanding how a html programmer like to write his code.
Why do JSF gives us only the possibility to write list of items throught tables?
Why do JSF prints error messages throught tables?
Why div is not a standard component?
This is an incredible lack...
I see also other incredible lasks...but they don't concern html ;) . -
IF NEW VARIABLE IN SQL QUERY, DISPLAYS AS LAST COLUMN + rpad not working
Hello everybody and thank you in advance,
1) if I add a new variable to my sql query to select a column which was already in the table, it shows it in the report table as the Last column to the right. That is, if I add "street" to
something like city, postcode, street, store, manager, etc, instead of placing street between postcode and store, it places it as i said as the last column to the right.
2) When values are entered into the cells of the tables, yes, they do expand it to their needed lenght, But, only if it is one word. If it is two, like when i enter the value "very good"
then it takes two lines so as with a carriage return within the cell, thus, making it too high the row. I tried to padd spaces with rpad but it did not work. something like rpad(stock, 20,' ')
I must say that the table is in the same page where there is a Form, so as the table grows in lenth it is actually squeezing the form located right on its left.
3) rpad did not work with the most simple syntax, but less would with what i need because it turns out i am using DECODE in order to do a conversion between value displayed and
value returned in my select list of values, something like : DECODE (TO_CHAR (stock),'1','Deficient','2','Average','3','Good','4','Very Good',null) AS stock,
so, i have tried to put the rpad there in several places but either it gave parsing error or it left the column empty not picking any values.
thank you very much
AlvaroAlvaro
1) That is standard behaviour of apex builder. You can change the display order with the arrows in the report attributes column report.
2) You will have to play with the style attributes of the column to accomplice this. For instance style="white-space:pre;" in the Element Attributes of the column attributes. White-space:normal would thread several space (' ') as 1. So no matter how many you add with rpad they will be shown as 1.
Or set a width either as attibute or in a style class for that column.
Nicolette -
Displaying Row and Column Numbers
I see through other research on Oracle SQL Developer that I should be able to display the row and column numbers on version 1.5.4, but I don't see where to go to turn this on. Suggestions?
Suggestions?Ask in the SQL Developer forum?
-
How to display row to columns without using pivot keyword
hi,
could someone help me how to dispaly rows into columns without using pivot keyword and actuall my scenario is,iam having two tables with names and sample data is shown below
ID PROJECT MID MINAME TASKID TASKNAME
1 PROJ1 1 AA 100 PR1_TASK1
1 PROJ1 3 CC 102 PR1_TASK3
1 PROJ1 4 DD 103 PR1_TASK4
1 PROJ1 5 EE 104 PR1_TASK5
1 PROJ1 6 FF 105 PR1_TASK6
2 PROJ2 5 EE 114 PR2_TASK1
2 PROJ2 6 FF 115 PR2_TASK2
2 PROJ2 7 GG 116 PR2_TASK3
2 PROJ2 8 HH 117 PR2_TASK4
2 PROJ2 9 JJ 118 PR2_TASK5
2 PROJ2 10 KK 119 PR2_TASK6
2 PROJ2 1 AA 120 PR2_TASK7
The output should display project and count of tasks in particular milestone 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 ,
vvrWITH t1 AS
(SELECT 1 ID,
'PROJ1' PROJECT,
1 MID,
'AA' MINAME,
100 TASKID,
'PR1_TASK1' TASKNAME
FROM DUAL
UNION
SELECT 1, 'PROJ1', 3, 'CC', 102, 'PR1_TASK3'
FROM DUAL
UNION
SELECT 1, 'PROJ1', 4, 'DD', 103, 'PR1_TASK4'
FROM DUAL
UNION
SELECT 1, 'PROJ1', 5, 'EE', 104, 'PR1_TASK5'
FROM DUAL
UNION
SELECT 1, 'PROJ1', 6, 'FF', 105, 'PR1_TASK6'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 5, 'EE', 114, 'PR2_TASK1'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 6, 'FF', 115, 'PR2_TASK2'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 7, 'GG', 116, 'PR2_TASK3'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 8, 'HH', 117, 'PR2_TASK4'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 9, 'JJ', 118, 'PR1_TASK5'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 10, 'KK', 119, 'PR1_TASK6'
FROM DUAL
UNION
SELECT 2, 'PROJ2', 1, 'AA', 120, 'PR1_TASK7' FROM DUAL)
SELECT id project,
NVL((SELECT mid
FROM t1
WHERE miname = 'AA'
AND id = t_out.id),
0) AA,
NVL((SELECT mid
FROM t1
WHERE miname = 'BB'
AND id = t_out.id),
0) BB,
NVL((SELECT mid
FROM t1
WHERE miname = 'CC'
AND id = t_out.id),
0) CC,
NVL((SELECT mid
FROM t1
WHERE miname = 'DD'
AND id = t_out.id),
0) DD,
NVL((SELECT mid
FROM t1
WHERE miname = 'EE'
AND id = t_out.id),
0) EE,
NVL((SELECT mid
FROM t1
WHERE miname = 'FF'
AND id = t_out.id),
0) FF,
NVL((SELECT mid
FROM t1
WHERE miname = 'GG'
AND id = t_out.id),
0) GG,
NVL((SELECT mid
FROM t1
WHERE miname = 'HH'
AND id = t_out.id),
0) HH,
NVL((SELECT mid
FROM t1
WHERE miname = 'JJ'
AND id = t_out.id),
0) JJ,
NVL((SELECT mid
FROM t1
WHERE miname = 'KK'
AND id = t_out.id),
0) KK
FROM (SELECT DISTINCT id FROM t1) t_out
PROJECT AA BB CC DD EE FF GG HH JJ KK
1 1 0 3 4 5 6 0 0 0 0
2 1 0 0 0 5 6 7 8 9 10As I understand, you want MID of MINAMEs displayed ? But output is not like yours.. What is exactly your requirements? -
User require a report having the following column data should be populated in column instead of row.
how can i modify or where should i modify to get the required output below. the following query is in XML file
select awd.award_id,
award_number,
bond_name,
p2b.bond_id bond_id,
nvl(b.par_amount,0)+nvl(b.premium,0)+nvl(b.original_issue_discount,0)+nvl(b.bic,0) Total_proceeds,
p2b.earnings_proceeds earning_procs,
nvl(b.original_issue_discount,0)+nvl(b.bic,0) issuance_cost
FROM xxdl.xxdl_cd_bond_setup_new b,
(select bond_id,
effective_date,nvl(earnings_proceeds,0) earnings_proceeds
from (
select s.*,
row_number() over(partition by bond_id order by effective_date desc) rn
from XXDL.xxdl_cd_bond_schedk_p2b s
where rn = 1) p2b,
gms_awards_all awd
WHERE b.bond_id= p2b.bond_id(+)
AND b.award_id = awd.award_id(+)
AND b.award_id = decode(:P_award_num,null,b.award_id,:P_award_num)
Actual output
award_id award_number bond_name bond_id Total_proceeds issuance_cost
345 XI ABC null 100 40
234 XIIA DEF null 86 100
Expected output
345 234
XI XIIA
ABC DEF
null null
100 86
40 100Edited by: 893185 on Nov 9, 2011 4:14 PM4. How do I convert rows to columns?
SQL and PL/SQL FAQ -
Dynamic SQL Pivoting(Converting Row to Columns)
Hi All,
I am using Oracle 9i (Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production)
and also 10g version
I am facing difficulties to find out the logic for
converting the set of values in one of the columns into the column headings for the entire query.
create TABLE my_tab ( deptno VARCHAR2(5), job VARCHAR2(50), sal NUMBER);
insert into my_tab ( deptno,JOB, sal) values ( 10, 'ANALYST', 23000);
insert into my_tab ( deptno,JOB, sal) values ( 10, 'SALESMAN', 1500);
insert into my_tab ( deptno,JOB, sal) values ( 10, 'CLERK', 3550);
insert into my_tab ( deptno,JOB, sal) values ( 20, 'SALESMAN', 700);
insert into my_tab ( deptno,JOB, sal) values ( 20, 'ANALYST', 4200);
insert into my_tab ( deptno,JOB, sal) values ( 30, 'SALESMAN', 5600);
insert into my_tab ( deptno,JOB, sal) values ( 30, 'CLERK', 12000);
insert into my_tab ( deptno,JOB, sal) values ( 30, 'ANALYST', 19000);
COMMIT;
SELECT * FROM my_tab
DEPTNO ______ JOB ________ SAL
10 ______ ANALYST ________ 23000
10 ______ SALESMAN ________ 1500
10 _______ CLERK ________ 3550
20 _______ SALESMAN ________ 700
20 _______ ANALYST ________ 4200
30 _______ SALESMAN ________ 5600
30 _______ CLERK _______ 12000
30 _______ ANALYST _______ 19000
--And I wish to convert it into this structure:
DEPTNO ________ ANALYST ________ SALESMAN _________ CLERK
10 ________ 23000 ________ 1500 _________ 3550
20 ________ 4200 ________ 700 _________ NULL
30 ________ 19000 ________ 5600 _________ 12000
It may be dynamic. i.e Later i inserted more two records into My_tab.
insert into my_tab ( deptno,JOB, sal) values ( 20, 'CLERK', 3400);
insert into my_tab ( deptno,JOB, sal) values ( 30, 'MANAGER', 48000);
So it should be dynamic.
output is like this.
DEPTNO ________ ANALYST ______ SALESMAN ______ CLERK ______ MANAMGER
10 ________ 23000 ______ 1500 ______ 3550 ______ NULL
20 ________ 4200 ______ 700 ______ 3400 ______ NULL
30 ________ 19000 ______ 5600 ______ 12000 ______ 48000
Please help me regarding this.
With warm regards,
PrasantaHi, Prasanta,
Displaying one column from many rows as many columns on one row is called Pivoting . The following thread shows the basics of how to pivot:
Help for a query to add columns
That example uses the aggregate COUNT function; you'll want SUM (or possibly MIN or MAX) instead.
Getting a dynamic number of columns requires Dynamic SQL . As a simpler alternative to pivoting and dynamic SQL, you might consider String Aggregation , where you concatenate a column from many rows into one big string, to be displayed on one row.
See the following thread for more about string aggregation and other options on pivoting into a variable number of columns:
Re: Report count and sum from many rows into many columns -
Hi,
Can anyone help, I have some rows that I am trying to display as columns.
The sample data is below:
create table c(
ID NUMBER,
SCHEDULE_NO NUMBER,
TERM_NO NUMBER,
FREQUENCY VARCHAR2(20),
RENTAL_AMOUNT NUMBER,
RENTAL_OS NUMBER);
INSERT INTO C VALUES(001, 1, 2, 'Monthly', 458, 3);
INSERT INTO C VALUES(001, 1, 3, 'Monthly', 268.33, 7);
INSERT INTO C VALUES(001, 1, 4, 'Monthly', 88.21, 12);
INSERT INTO C VALUES(001, 1, 5, 'Monthly', 757.42, 35);
INSERT INTO C VALUES(002, 1, 7, 'Monthly', 101.22, 3);
INSERT INTO C VALUES(002, 1, 8, 'Yearly', 55, 2);
I would like to display only 1 line per ID, e.g.
001 - Row
SCHEDULE_NO, TERM_NO, FREQUENCY, RENTAL_AMOUNT, RENTAL_OS - Columns
I will need a new column to be displayed for each line split,
e.g. 001, schedule_no1, term_no1, frequency1...
001, schedule_no2, term_no2, frequency2...
001, schedule_no3, term_no3, frequency3...
It would be like a pivot table but split for each change in line.
ThanksThis work ...
select * from (
select id,
SCHEDULE_NO,
TERM_NO,
FREQUENCY,
RENTAL_AMOUNT,
RENTAL_OS
from c)
pivot (max(SCHEDULE_NO) SCHEDULE_NO,
max(TERM_NO) TERM_NO,
max(FREQUENCY) FREQUENCY,
max(RENTAL_AMOUNT) RENTAL_AMOUNT,
max(RENTAL_OS) RENTAL_OS
for (SCHEDULE_NO,TERM_NO,FREQUENCY,RENTAL_AMOUNT,RENTAL_OS)
IN (
(1, 2 , 'Monthly' , 458, 3 ) SCHEDULE_NO1,
(1, 3 , 'Monthly' , 268.33 , 7 ) SCHEDULE_NO2,
(1, 4 , 'Monthly' , 88.21 , 12 ) SCHEDULE_NO3,
(1, 5 , 'Monthly' , 757.42 , 35 ) SCHEDULE_NO4,
(1, 7 , 'Monthly' , 101.22 , 3 ) SCHEDULE_NO5,
(1, 8 , 'Yearly' , 55 , 2 ) SCHEDULE_NO6
) -
Looking for component: flexible rows like columns in a JTable
Hello,
I need a swing component to display several rows which can be moved up and down, the height resized, interchanged with the mouse like the columns in a JTable. But there is no need for columns. I think there is no standard swing component.
Any ideas, resource hints?
Thanks, UlrichOne more piece of advice. It is not very easy to get "pre-written custom components". Most developers do things to meet their own needs and these may not be exactly what you are looking for. The best thing to do is to try to develop this yourself. It will give you loads of experience and in later programmes you may write, based on the experience you obtained from the "pain-staking" development process you'll know how to go round these problems quicker and may be able to help others also.
So just start writing some stuff. You may end up finishing sooner than you think. And remember forum members are always ready to help with problems (accompanied by minimal code examples of the actual problem).
ICE -
How to display rows as columns?
Hi,
I am simulating my problem with the following example.
I have two tables, book_info contains book_id(primary key), book_name and the other table is author_info contains id(primary key), book_id and author. One book may have maximum upto THREE authors.
BOOK_INFO
=========
book_id category book_name
1 a ABC
2 a PQR
3 b XYZ
AUTHOR_INFO
===========
id book_id author
1 1 aaaaaa
2 1 bbbbbb
3 1 cccccc
4 2 pppppp
5 2 qqqqqq
6 3 xxxxxx
I need output like different columns using a sql query WHERE category = 'a'.
book_id book_name author1 author2 author3
1 ABC aaaaaa bbbbbb cccccc
2 PQR pppppp pppppp
We are using Oracle 9i/NT environment. Urgent please.
Thank you.Vijaya,
Looks like I just answered this question, though your table layout is pretty much the same. Here what I said ...
There actually is a couple ways to do this, but the term is called FLATTENING. The method I like is with a function and a view (I'm a huuuuge fan of views). It has some draw backs performance wise if doing this on allot of data, but the results are well worth it. Anyway the code is below. The table is T1, the view it makes is V1. The function is called FN_REC_FLAT. Some sample output is also included.
16:46:11 SQL> SELECT T1.N1,T1.N2,T1.C1 FROM TABLE_NAME_HERE T1 ORDER BY T1.N1,T1.N2;
N1 N2 C1
-1 1 TEST
1 1 THIS
1 2 IS RECORD
1 3 NUMBER 1.
2 1 THIS IS
2 2 RECORD
2 3 NUMBER 2.
3 1 AND THIS
3 2 IS RECORD
3 3 NUMBER 3.
10 rows selected.
CREATE OR REPLACE FUNCTION
FN_REC_FLAT(PI_FLAT_KEY IN NUMBER,PS_FLAT_DATA IN VARCHAR)
RETURN VARCHAR AS
CURSOR CUR_T1 IS SELECT * FROM TABLE_NAME_HERE T1 WHERE T1.N1 = PI_FLAT_KEY ORDER BY T
REC_T1 TABLE_NAME_HERE%ROWTYPE;
LS_FLAT_DATA VARCHAR2(2000);
LS_RETURN VARCHAR2(2000);
BEGIN
LS_FLAT_DATA := NULL;
OPEN CUR_T1;
FETCH CUR_T1 INTO REC_T1;
WHILE CUR_T1%FOUND LOOP
LS_FLAT_DATA := (LS_FLAT_DATA || ' ' || REC_T1.C1);
FETCH CUR_T1 INTO REC_T1;
END LOOP;
LS_RETURN := TRIM(LS_FLAT_DATA);
RETURN LS_RETURN;
END;
Function created.
CREATE OR REPLACE VIEW V1 (N1,C1) AS SELECT DISTINCT T1.N1,FN_REC_FLAT(T1.N1,T1.C1) FROM TABLE_NAME_HERE T1;
View created.
16:46:12 SQL>
16:46:12 SQL> SELECT T1.N1,T1.C1 FROM V1 T1 ORDER BY T1.N1,T1.C1;
N1 C1
-1 TEST
1 THIS IS RECORD NUMBER 1.
2 THIS IS RECORD NUMBER 2.
3 AND THIS IS RECORD NUMBER 3.
16:46:12 SQL>
Note that the function takes in N1 as the root level for the flatten key. N2 is the sort order. In you case this would be EMP_ID and EMP_INFO_SEQ_NUM. The C1 is EMP_INFO. T1 is the table alias and TABLE_NAME_HERE ... well I'll let you figure that part out ;).
Hope this helps,
Tyler D. -
Display rows into columns in table
Hi,
I have a table name Ebiz_Upgrade_Task_Status. Here in table there are 8 rows of data with
pro_id,pobj_id,cemli_id and confirmation.
I am trying to convert all the CONFIRMATION into column wise using the query below
SELECT
PRO_ID,
POBJ_ID,
CEMLI_ID,
max(decode(rownum,1,CONFIRMATION,null)) "CP1",
max(decode(rownum,2,CONFIRMATION,null)) "CP2",
max(decode(rownum,3,CONFIRMATION,null)) "CP3",
max(decode(rownum,4,CONFIRMATION,null)) "CP4"
FROM Ebiz_Upgrade_Task_Status
GROUP BY PRO_ID,POBJ_ID,CEMLI_ID
Am able to see first 2 rows of data as columns, and not able to see the next rows. Please suggest me in modifyin the query
tabel strutuce and data
pro_id obj_id cemli_id confirmation
1 2 3 Yes
1 2 3 No
1 2 3 NA
11 22 33 Yes
11 22 33 NO
11 22 33 NA
Please suggest me in modifying the code
Thanks
SudhirThanks for posting more details.
Using rownum caused your confirmation fields to be null:
SQL> select * from temp1;
PRO_ID POBJ_ID CEMLI_ID CONFIRMATION
111 222 333 yes
111 222 333 no
111 222 333 na
11 22 33 na
11 22 33 no
11 22 33 yes
6 rows selected.
SQL> col cp1 format a10
SQL> col cp2 format a10
SQL> col cp3 format a10
SQL> select pro_id
2 , pobj_id
3 , cemli_id
4 , max(decode(rownum,1,confirmation,null)) "cp1"
5 , max(decode(rownum,2,confirmation,null)) "cp2"
6 , max(decode(rownum,3,confirmation,null)) "cp3"
7 from temp1
8 group by pro_id
9 , pobj_id
10 , cemli_id;
PRO_ID POBJ_ID CEMLI_ID cp1 cp2 cp3
11 22 33
111 222 333 yes no na
SQL> select pro_id
2 , pobj_id
3 , cemli_id
4 , max(decode(confirmation, 'yes',confirmation,null)) "cp1"
5 , max(decode(confirmation, 'no',confirmation,null)) "cp2"
6 , max(decode(confirmation, 'na',confirmation,null)) "cp3"
7 from temp1
8 group by pro_id
9 , pobj_id
10 , cemli_id;
PRO_ID POBJ_ID CEMLI_ID cp1 cp2 cp3
11 22 33 yes no na
111 222 333 yes no na
SQL> -
Display rows as columns in crystal
Hello,
I am using crystal reports version 9 and i have a requirement as below.
main report has a sub report in the page header section and the sub report gets data of the customer names linked to the customer in main report.
my sub report output currently shows as
NAME A
NAME B
but i want it to appear as NAME A, NAME B so that all the customer names appear in a line instead of seaprate rows on the report. is there a way to do this?
Thanks in advance for your help.Suppress the detail section, and use this formula to concatenate all of the values (basic syntax):
WhilePrintingRecords
global nameList as string
if len(nameList) = 0 then
nameList = {db.name}
else
nameList = nameList + ", " + {db.name}
end if
formula = ""
Then in the subreport's footer, print the names:
WhilePrintingRecords
global nameList as string
formula = nameList
HTH,
Carl
Maybe you are looking for
-
How can I make multiple popup links on one page?
I was wondering if anyone had a simple code that will allow me to have multiple popup links on one page? I have no idea what to do. Any help would be greatly appreciated.
-
BATCH SPLIT in Order Delivery and PGI (SD)
Hi Could anybody tell me the following: 1) What is the concept of Batch Split? 2) How it is shown when a Delivery Order is created? 3) How it afftects when Goods Issue is done? 4) How can we calculate the total delivery quantity when there is a Batch
-
Why does true-type font not appear in the Microsoft Word Font menu?
Hi, I am trying to install the "Apple Symbols.ttf" font so that I can use the male and female biological symbols in a Microsoft Word (version X) file. In "Font Book", I found the font under "All Fonts" and dragged it onto the "User" and "Computer" ic
-
Cash flow satement(Direct Method)..
Hello guys, When my client is excuting cash flow statement(S_ALR_87012271 -(Direct Method) , they are getting message like "no records were selected". I Activated Cash Management OT29 (activate CM) and I selected the GL accounts relevant to Cash Flow
-
"Panic" - Working perfect yesterday...
Got to work this morning and computer kernal panicked half dozen times. Able to boot in safe mode and came up with this log. I can really make anything out? Anybody see anything that stands out? Thanks for the help. Model Name: Mac Pro Model Identifi