Dynamic rows to columns oracle 10g
Hi,
I'm using oracle 10g. I have a requiriement where I need to show rows to columns. I have read through many posts but I couldn't find what I wanted to solve. Lots of them are using decode, since they know the values in one column.
My table has name, value and date. I need to get values for each name at certain date (each distinct date needs to become column). I don't know the name beforehand.
Name, Value, Date
T1 100 06-27-2011 09:00:00
T2 100 06-27-2011 09:00:00
T1 200 06-27-2011 09:15:00
T2 150 06-27-2011 09:15:00
I would also need to use a date range. Someone can request date between 09:00:00 and 09:05:00 or 09:00:00 and 09:30:00 etc.
I need to print
Name, 06-27-2011 09:00:00, 06-27-2011 09:15:00
T1 100 200
T2 100 150
Appreciate any advice on this.
Edited by: user8801143 on 28-Jun-2011 08:23
With 10g
see http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:766825833740#2989343200346664698
search for post on February 11, 2011 and reply on February 14, 2011
with 11g see the pivot clause
Similar Messages
-
Hello All,
I have a requirement where I have to convert rows to columns and vice versa in 10g.
Pivot isn't supported in 10g.
Actual query looks like this
change_name primary_key_id
ASSET 1501
COLLATERAL 1501
ASSET 1502
COLLATERAL 1510
ASSET 1503
COLLATERAL 1515
Required Output:
change_table_name Asset Collateral
Primary_key_id 1501 1501
Primary_key_id2 1502
Primary_key_id3 1510
Primary_key_id4 1503
Primary_key_id5 1515Thx
Shank.
Edited by: SamFisher on May 16, 2012 7:48 PMHi,
SamFisher wrote:
Hello All,
I have a requirement where I have to convert rows to columns and vice versa in 10g.
Pivot isn't supported in 10g.Pivot can be done in any version of Oracle.
The PIVOT keyword of the SELECT command isn't supported in Oracle 10. That's only one way of pivoting.
Actual query looks like this
change_name primary_key_id
ASSET 1501
COLLATERAL 1501
ASSET 1502
COLLATERAL 1510
ASSET 1503
COLLATERAL 1515
Required Output:
change_table_name Asset Collateral
Primary_key_id 1501 1501
Primary_key_id2 1502
Primary_key_id3 1510
Primary_key_id4 1503
Primary_key_id5 1515
I think you want something like this:
SELECT 'Primary_key_id
|| TO_CHAR ( NULLIF ( ROW_NUMBER () OVER (ORDER BY NVL ( a.primary_key_id
, c.primary_key_id
, 1
) AS change_table_name
, a.primary_key_id AS asset
, c.primary_key_id AS collateral
FROM table_x a
FULL OUTER JOIN table_x c ON a.primary_key_id = c.primary_key_id
-- Next 4 lines added after Ankit, below
AND a.change_name = 'ASSET'
AND c.change_name = 'COLLATERAL'
WHERE a.change_name = 'ASSET'
OR c.change_name = 'COLLATERAL'
;If you'd care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.
This generates a unique change_table_name for each row. I don't see how you get the values you said you wanted associated with the other columns. That is, I have the same question as Justin:
Justin Cave wrote:
... How do you know that 1502 goes with primary_key_id2 and not primary_key_id3 or 4 or 1?Perhaps you don't really care which change_table_name is given to each row, just so long as they are unique, and numbered with consecutive integers (except for 1). If you really do need exactly what you posted, explain how you get it. You probably just need to change the analytic ORDER BY clause.
Edited by: Frank Kulash on May 17, 2012 6:56 AM
Corrected query -
Row chaining and row migration in Oracle 10g R2/11g R2
Hi,
Due to the business rule changes, one of the numeric column in a large table (20 millions rows) will be expanded from number(8) to number(10). Also, the values of this column of each row will be updated from 6 digits to 10 digits. All the indexes that use this column will be dropped and recreated after update. I would like to know if there is any row chaining or row migration issue in Oracle 10g R2 /11g R2.
Thanks for your helpneemin wrote:
Hi,
Due to the business rule changes, one of the numeric column in a large table (20 millions rows) will be expanded from number(8) to number(10). Also, the values of this column of each row will be updated from 6 digits to 10 digits. All the indexes that use this column will be dropped and recreated after update. I would like to know if there is any row chaining or row migration issue in Oracle 10g R2 /11g R2.
Thanks for your helpIt depends.
what you do observe after TESTING against the Development DB? -
Dynamic row counter column in Adobe Forms
Hi Guys,
My table in Adobe Forms is dynamically created with Conditional Breaks. I want to display 1st column as Sr.No. displaying row counter for tables on each page.
Anyone have idea how to achieve the same.
Sr.No
Name
Address
1 (generated in adobe forms dynamically)
xyz
awsaxa sadsad
2 (generated in adobe forms dynamically)
abc
asds sad ads
so on......
Regards,
NaveenHi Guys,
Not able to find any counter variable for table. So what I did is inserted the counter logically before calling Adobe forms driver program.
FYI.
Regards,
Naveen -
Dynamic For loop in Oracle 10g
Hi ,
my requirement is to write dynamic for loop , Some thing similar to below plsql block, But I am getting error , please suggest
declare
lv_text varchar2(200);
begin
lv_text:='select * from some_table ';
for i in (lv_text)
loop
null;
end loop;
end;
**** Here my purpose of lv_text is to use a dynamic query in for loop.You are getting error because lv_Text is not a cursor variable.... So you can try this way..if your statement is known...
begin
For rec in (select * from emp)
loop
dbms_output.put_line(rec.ename);
end loop;
end;
Or try this if your statements can be controlled based on your addition input(cnt)
create or replace procedure p_ref_Cursor
+(p_Empno in number,p_ename out varchar2,p_sal out number,cnt number)+
is
Type ref_cursor is ref cursor;
e_ref_Cursor ref_Cursor;
begin
if cnt =1 then
open e_Ref_Cursor for select ename from emp where empno=p_Empno;
fetch e_ref_cursor into p_ename;
else
open e_Ref_Cursor for select sal from emp where empno=p_Empno;
fetch e_ref_cursor into p_sal;
end if;
close e_ref_cursor;
end;
or Use dbms_sql package for creating dynamic SQL as suggested by others too if your statement is completely dynamic ..:) -
Convert rows into columns nad vice versa in 10g
how to convert rows into columns in 10g??
Qwerty wrote:
see below for rows to column case
SQL> WITH t as
2 (
3 SELECT 'US' test_string FROM DUAL UNION
4 SELECT 'AMERICA' FROM DUAL UNION
5 SELECT'HOLLYWOOD' FROM DUAL UNION
6 SELECT 'WASHINGTON' FROM DUAL
7 )
8 select ltrim (sys_connect_by_path(test_string,','),',') test_string
9 from (
10 SELECT row_number() over(order by test_string) rno, test_string
11 FROM t)
12 WHERE connect_by_isleaf = 1 and rownum=1
13 connect by rno = prior rno+1;
TEST_STRING
AMERICA,HOLLYWOOD,US,WASHINGTONI hope you can do it for column to rows now.That's not really rows to columns. That's rows to a column, which is more commonly called string aggregation.
Rows to columns (or pivot) is more like:
SQL> ed
Wrote file afiedt.buf
1 WITH t as
2 (
3 SELECT 'US' test_string FROM DUAL UNION
4 SELECT 'AMERICA' FROM DUAL UNION
5 SELECT'HOLLYWOOD' FROM DUAL UNION
6 SELECT 'WASHINGTON' FROM DUAL
7 )
8 --
9 select max(decode(rn,1,test_string)) as col_1
10 ,max(decode(rn,2,test_string)) as col_2
11 ,max(decode(rn,3,test_string)) as col_3
12 ,max(decode(rn,4,test_string)) as col_4
13* from (select test_string, row_number() over (order by test_string) as rn from t)
SQL> /
COL_1 COL_2 COL_3 COL_4
AMERICA HOLLYWOOD US WASHINGTON
SQL>And columns to rows (or unpivot) is like:
SQL> ed
Wrote file afiedt.buf
1 WITH t as
2 (
3 SELECT 'US' col_1, 'AMERICA' col_2, 'HOLLYWOOD' col_3, 'WASHINGTON' col_4 FROM DUAL
4 )
5 --
6 select col_1 as col from t union all
7 select col_2 from t union all
8 select col_3 from t union all
9* select col_4 from t
SQL> /
COL
US
AMERICA
HOLLYWOOD
WASHINGTONor...
SQL> ed
Wrote file afiedt.buf
1 WITH t as
2 (
3 SELECT 'US' col_1, 'AMERICA' col_2, 'HOLLYWOOD' col_3, 'WASHINGTON' col_4 FROM DUAL
4 )
5 --
6 select decode(rownum,1,col_1,2,col_2,3,col_3,4,col_4) as col
7* from t, (select * from dual connect by rownum <= 4)
SQL> /
COL
US
AMERICA
HOLLYWOOD
WASHINGTON
SQL> -
Returning 250 rows with 1000 Values in "IN" Clause Oracle 10g On IBM AIX !!
Hi,
Recently we have done the OS migration of Oracle 10g Server from Windows Server to IBM AIX. Everything is fine, But today we came across one crucial bug in the code, i.e In the Select Query, though we're expecting 1000 rows with 1000 values in "IN" Clause , It's returning Only 250 rows. Where as it's returning 1000 rows in Windows Environment with 1000 values in "IN" Clause. I have browsed throgh Google for the resolution but failed to get that.
This is something like,
In Oracle 10g On windows :-
select * from emp
where dept_id in (1,2,3,...................1000);
Assuming there are the dept_id values in Emp table from 1 ... 1000, It's returning 1000 rows.
In Oracle 10g On IBM AIX ,
select * from emp
where dept_id in (1,2,3,...................1000);
Assuming there are the dept_id values in Emp table from 1 ... 1000, It's returning 250 rows. Pls help me, what could be the reason for this. and what needs to be checked to fix this.
Pls suggest !!!
Rajammmh. Did you compared the select count(*) from your_table; in the two cases.
If the result is not good and nobody has deleted rows between migration and your test, you migration need to be replayed.
Which migration did you select, Transportable database or exp/imp...?
Edited by: Dba Z on 16 août 2009 08:56 -
Oracle 10g query rows to column transpose
In oracle 10g I have table called LoginUser
Select * from LoginUser where Day = '01-Dec-2012'
will give me the below result
Day, Name, Country
1-Dec-12 , John, UK
1-Dec12 , Cate, US
1-Dec-12 , Prakash, India
1-Dec-12 ,Juli, Pak
But I want result as below
Day, Name1, Country1, Name2, Country2, Name3, Country3, Name4, Country4
1-Dec-12, John, UK, Cate, US, Prakash, India, Juli, Pak
Please help
Edited by: 975340 on Dec 6, 2012 1:48 AM
Edited by: 975340 on Dec 6, 2012 1:50 AMRefer to below thread
Re: Multiple rows into a single line in 'Single Column Table' -
Transform columns in row for a table in Oracle 10g
Hi,
I am using Oracle 10g.
There is a table (example) EMPLOYEE, with following structure
IDUSER YEAR1 YEAR2 YEAR3
12345 value1 value2 value3
99999 value7 value6 value5
and I need display :
IDUSER YEAR
12345 value1
12345 value2
12345 value3
99999 value7
99999 value6
99999 value5
How can I do this with without unpivot?
ThanksHi,
Welcome to the forum!
One way to unpivot in any version of Orace is to cross-join your table with another table (or resut set, in ths case) that has as many rows as you need toroduce from each row of your original table.
Here's an example:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 3
SELECT e.iduser
, CASE c.n
WHEN 1 THEN year1
WHEN 2 THEN year2
WHEN 3 THEN year3
END AS uear
FROM cntr c
CROSS JOIN employee e
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
See the forum FAQ {message:id=9360002}
Edited by: Frank Kulash on Apr 11, 2013 6:47 AM -
Converting Rows into Column in Oracle 10g
Hi All,
I m using Oracle Version 10.1.0.2.0 - Production
I have requirement to convert rows into column wise as per the following:
My Query is:
WITH t
AS ( SELECT 'A' AS x, 100 AS y FROM DUAL
UNION ALL
SELECT 'B',200 FROM DUAL
SELECT X, Y
FROM t;
X Y
A 100
B 200
My Requirement is
A B
100 200
So any one could help me that how I resolve this.
Regards,
PrasantaDear frank,
Thanks for your support,.
It's working fine for static cases.If the first column is dynamic then how come i will resolve it.
Example:
Create table mytab (ID_C Varchar2(15),Value_N Number);
Records Population into MyTab table is dynamic.
Insert into mytab values('HO',5000);
Insert Into mytab values('PG1',2400);
Insert Into mytab values('PG2',3000);
Insert Into mytab values('PG3',800);
Commit;
SQL> Select * From MyTab;
IDC_ ValueN_
HO 5000
PG1 2400
PG2 3000
PG3 800
Then My expected result will be as follows
HO PG1 PG2 PG3
5000 2400 3000 800
Thanks and Regards,
Prasanta -
Hi,
I need the output from columns to rows in oracle 10g.
this is my original output using some select query,
select a.created_by,b.modified_by from created_tp a,modified_tp b where a.col_id=b.col_id.
created_by modified_by
Siva Raja
but i need this same output by rows like
user--anything fine
Siva
Raja
Thanks
SivaHi,
select case l when 1 then a.created_by
else b.modified end
from created_tp a,modified_tp b, (select level l from dual connect by level <= 2)
where a.col_id=b.col_id. -
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. -
Hi
I have folowing scenario. I am getting the result with some query like below :
STATE DAY AVG
NY 02/02 5
NY 02/03 10
NY 02/04 20
NY 02/05 15
IL 02/02 23
IL 02/03 34
IL 02/04 29
IL 02/05 9
FL 02/02 15
FL 02/03 8
FL 02/04 9
FL 02/05 10
and so on..Now I want to convert it from rows to column, but still want to keep STATE column group by :
so the result would be :
STATE 02/02 02/03 02/04 02/05
NY 5 10 20 15
IL 23 34 29 9
FL 15 8 9 10
and so on...I tried using pivot function, but it didnt work. is it even possible? or I will have to do it in code (.net c#) side.
Thanks1 WITH t AS (
2 SELECT 'NY' state,'02/02' mydt,5 myavg FROM dual
3 UNION all
4 SELECT 'NY','02/03',10 FROM dual
5 UNION all
6 SELECT 'NY','02/04',20 FROM dual
7 UNION all
8 SELECT 'NY','02/05',15 FROM dual
9 UNION all
10 SELECT 'IL','02/02',23 FROM dual
11 UNION all
12 SELECT 'IL','02/03',34 FROM dual
13 UNION all
14 SELECT 'IL','02/04',29 FROM dual
15 UNION all
16 SELECT 'IL','02/05',9 FROM dual
17 UNION all
18 SELECT 'FL','02/02',15 FROM dual
19 UNION all
20 SELECT 'FL','02/03',8 FROM dual
21 UNION all
22 SELECT 'FL','02/04',9 FROM dual
23 UNION all
24 SELECT 'FL','02/05',10 FROM dual
25 )
26 SELECT * FROM t
27 PIVOT
28 (
29 SUM(myavg)
30 FOR mydt IN('02/02','02/03','02/04','02/05')
31 )
32* ORDER BY state DESC
SQL> /
ST '02/02' '02/03' '02/04' '02/05'
NY 5 10 20 15
IL 23 34 29 9
FL 15 8 9 10Am I missing some thing here ? -
Converting Columns to rows in Oracle 10g
Hi
i need hint to convert rows to columns.i had given the data strucure and expected output
Source Table
BU_ID Prod_id total_clients Tot_men Totwomen
101 AAA 85 50 35
101 BBB 40 20 20Expected Output
BU_ID Prod_id Clint_info Values
101 AAA total_clients 85
101 AAA Tot_men 50
101 AAA totwomen 35
101 BBB total_clients 40
101 BBB tot_men 20
101 BBB totwomen 20Thanks
Edited by: Sami on Aug 1, 2012 8:25 PMHi,
Cross-join your table with a Counter Table , a table (or result set, as in the example below) that counts from 1 up to the number of columns you want to unpivot, like this:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 3 -- Number of columns to be unpivoted
SELECT s.bu_id
, s.prod_id
, CASE c.n
WHEN 1 THEN 'total_clients'
WHEN 2 THEN 'total_men'
WHEN 3 THEN 'total_women'
END AS client_info
, CASE c.n
WHEN 1 THEN total_clients
WHEN 2 THEN total_men
WHEN 3 THEN total_women
END AS values
FROM source_table s
CROSS JOIN cntr c
ORDER BY s.bu_id
, s.prod_id
, c.n
;If you'd care to post CREATE TABLE and INSERT statements for your sample data, then I could test this.
The values column that you create can only have 1 data type. If the original, unpivoted columns have different data types, you may need to convert some of them in the CASE expression that defines values. -
Oracle 10g - Defining the column name in Non English
Hi Experts,
I have an exisitng application which is developed on Windows using ASP Technology and uses Oracle 10g 10.1.0.2.0.
The application is supported with an instance of Data Base within which multiple tablespaces are created for different clients. The application is developed in such a way that some of the tables arecreated dynamically and the columns are named using the data entered through the UI.
This application needs to be globalized now. The problem is, the column name entered through the UI can be in any language based on the client's settings and those values in turn will be used for naming the columns in the tables.
1) Can I have the column names to be named using non english characters in Oracle 10g DB? If so,
1.1) what should I do to configure the exisiting Oracle instance to support it?
1.2) To what level is that configuration possible, is it per DB instance level (or) can it be done at Tablespace level. I would like to configure each tablespace to host tables with columns defined with different languages, say for example, tablespace 1 will have tables with Japaenese column names and tablespace 2 will have tables with German column names?
2) What should I do to make my entire DB to support unicode data i.e., to accept any language strings. Currently all strings are declared as VarChar2, should I change all VarChar2 to NVarChar2 (or) is there a way to retain the VarChar2 as is and make some database wide setting?
Please note that I do not have an option of retaining the column in English as per the Business Requirement.
Envionment:
OS - Windows 2003 32 bit
Oracle 10g 10.1.0.2.0
UI forms in ASP
TIA,
Prem1. Yes, you can.
SQL> create table ÜÝÞ( ßàá number(10));
Table created.
SQL> insert into ÜÝÞ values (10);
1 row created.1.1 and 1.2 and 2. You can choose UTF as your default character set. It allows the user of non-English characters in VARCHAR columns in your whole database. It is not per tablespace.
SQL> create table ÜÝÞ( ßàá varchar2(100));
Table created.
SQL> insert into ÜÝÞ values ('âãäçìé');
1 row created.
Maybe you are looking for
-
J2EE Logon Servlet Prompting For Username Email
I have a situation in my landscape that recently, after patching the system to NW700 SPS23, some users would enter their logon and password correctly and then get confronted with an additional security screen requesting their firstname, lastname, ema
-
Hello, We have the following issues when publishing TLB orders from APO to R/3. Would really appreciate if anyone has insights regarding these issues: 1) When the TLB order (78* number) was published from APO from the interactive TLB screen, it goes
-
Cannot activate my iphone after updating
after updating ios5.01, my Iphone 3GS needs to be activated. While i was activating it, it keeps come back saying" your iphone can not be activated because the activation server is temporarily unavaliable" I've been trying all night, and the problem
-
Hello, I have been experimenting with snapshot isolation with Berkeley DB, but I find that it frequently triggers a segmentation fault when write transactions are in progress. The following test program reliably demonstrates the problem in Linux usi
-
I have the Mac OSX and I accidentally erased iPhoto, how can I reinstall it?
I have the Mac OSX and I accidentally erased iPhoto, how can I reinstall it?