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
Similar Messages
-
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. -
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? -
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 ;) . -
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?
-
Localization of CrossTab row and column label Crystal Report RPT file
Dear all,
Does anyone know how to support multiple locale texts (English and French) in CrossTab Row and Column Header labels and grand total labels? Is there a way to parameterize text objects CrossTab in one single Crystal Report RPT file to use some sort of locale resource file containing text string for different languages based on the users of RPT at runtime? The idea is to use one single RPT file to generate report for different locale text for CrossTab Row and Column abels and grand total labels instead of creating RPT files per locale?Hi,
Does the database have a column that identifies the language? If it does, then you could create two separate cross-tabs and place them in different sections and conditionally suppress the section depending on the language from the database.
That's the only thing that can be done in my opinion.
-Abhilash -
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 -
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 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 -
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> -
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. -
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
RameshOk, 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> -
Displaying rows as columns and coumns as rows?
Can we do this?
Thanks.Can we do this?Yes.
Google it and you will find HOW to do it. -
Pivoting rows into columns in Oracle 10g
Hi,
I want to pivot rows into column in some optimal way.
I don't want to go with the DECODE option as the number of columns can be more than 200.
i have also tried the transpose logic which is making the pl/sql block too huge.
can i directly query the database for the desired output instead of storing the data into some arrays and displaying rows as columns?Hi,
Here's a dynamic way to do this is Oracle 10, using theSQL*Plus @ command to handle the dynamic parts.
First, let's see how we would do this using a static query:
WITH col_cntr AS
SELECT column_name
FROM all_tab_columns
WHERE owner = 'FKULASH'
AND table_name = 'TEST_EMP'
AND column_name NOT IN ('EMP_ID', 'TYPE_VAL')
, unpivoted_data AS
SELECT e.type_val
, c.column_name
, CASE c.column_name
WHEN 'X_AMT' THEN x_amt -- ***** Dynamic section 1 *****
WHEN 'Y_AMT' THEN y_amt -- ***** Dynamic section 1 *****
WHEN 'Z_AMT' THEN z_amt -- ***** Dynamic section 1 *****
END AS v
FROM test_emp e
CROSS JOIN col_cntr c
SELECT column_name AS type_val
, SUM (CASE WHEN type_val = 'Q1' THEN v ELSE 0 END) AS q1 -- ***** Dynamic section 2 *****
, SUM (CASE WHEN type_val = 'Q2' THEN v ELSE 0 END) AS q2 -- ***** Dynamic section 2 *****
, SUM (CASE WHEN type_val = 'Q3' THEN v ELSE 0 END) AS q3 -- ***** Dynamic section 2 *****
, SUM (CASE WHEN type_val = 'Q4' THEN v ELSE 0 END) AS q4 -- ***** Dynamic section 2 *****
FROM unpivoted_data
GROUP BY column_name
ORDER BY column_name
;Column names are hard-coded in two places:
(1) in the sub-query unpivoted_data, we had to know that there were 3 columns to be unpivoted, and that they were called x_amt, y_amt and z_amt. You want to derive all of that from all_tab_columns.
(2) in the main query, we had to know that there would be 4 pivoted columns in the rsult set, and that they would be called q1, q2, q3 and q4. You want to derive all that from the data actually in test_emp.
Instead of hard-coding those 2 dynamic sections, have Preliminary Queries write them for you, a split second before you run the main query, by running this script:
-- Before writing sub-scripts, turn off features designed for human readers
SET FEEDBACK OFF
SET PAGESIZE 0
PROMPT ***** Preliminary Query 1 *****
SPOOL c:\temp\sub_script_1.sql
SELECT ' WHEN '''
|| column_name
|| ''' THEN '
|| LOWER (column_name) AS txt
FROM all_tab_columns
WHERE owner = 'FKULASH'
AND table_name = 'TEST_EMP'
AND column_name NOT IN ('EMP_ID', 'TYPE_VAL')
ORDER BY column_name
SPOOL OFF
PROMPT ***** Preliminary Query 2 *****
SPOOL c:\temp\sub_script_2.sql
SELECT DISTINCT ', SUM (CASE WHEN type_val = '''
|| type_val
|| ''' THEN v ELSE 0 END) AS '
|| LOWER (type_val) AS txt
FROM test_emp
ORDER BY txt
SPOOL OFF
-- After writing sub-scripts, turn on features designed for human readers
SET FEEDBACK 5
SET PAGESIZE 50
-- Main Query:
WITH col_cntr AS
SELECT column_name
FROM all_tab_columns
WHERE owner = 'FKULASH'
AND table_name = 'TEST_EMP'
AND column_name NOT IN ('EMP_ID', 'TYPE_VAL')
, unpivoted_data AS
SELECT e.type_val
, c.column_name
, CASE c.column_name
@c:\temp\sub_script_1
END AS v
FROM test_emp e
CROSS JOIN col_cntr c
SELECT column_name AS type_val
@c:\temp\sub_script_2
FROM unpivoted_data
GROUP BY column_name
ORDER BY column_name
;As you can see, the main query looks exactly like the static query, except that the two dynamic sections have been replaced by sub-scripts. These 2 sub-scripts are written by 2 prelimiary queries, right before the main query.
As others have said, the fact that you're asking this question hints at a poor table design. Perhaps the table should be permanently stored in a form pretty much like unpivoted_data, above. When you need to display it with columns x_amt, y_amt, ..., then pivot it, using GROUP BY type_col. When you need to display it with columns q1, q2, ..., then pivot it using GROUP BY column_name.
Maybe you are looking for
-
T61 no longer recognizes DVD super-multi drive
My T61 for some reason has stopped recognizing my DVD super-multi drive (GSA-U10N). I do not use the optical drive often, so I am not sure when during the past couple of months this occured. I have tried finding it using Lenovo System Toolbox and D
-
How do I rename one wrongly identified photo in Faces? I seem to only be able to merge two stacks of photos to the correct name, but that is not accurate. I only have one photo in a batch under a certain name, that is wrongly identified.
-
BAPI_PO_CHANGE failing
I am using BAPI_PO_CHANGE to add line item to the purchase order. This PO is taking reference from the SO of same company code. when i run this BAPI it gives me the following message: PO Changes Failed due to following reasons Changi
-
Print jobs keep starting and then error in print que
I have a HP Photosmart 100D series printer. The print jobs keep freezing up in the que. The jobs start and then after a page or two the error message appears when checking the status. I uninstalled the printer and reinstalled I have download new driv
-
Inconsistence at PO History - Entry Sheet deleted
Hello everyone! I have an issue at my system with an entry sheet that has been deleted. This Entry sheet was deleted and the material document with movement type = 102 was well created. But the entry sheet is not showed now with total = 0 but with it