Convert columns to row equivalent to stragg function in oracle sql
Hi,
Sorry i forgot my Oracle version :
SQL> select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for 64-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - ProductionI searched in google but i didn't found the solution.
I looking for a function in discoverer equivalent to stragg sql function.
Note : stragg function convert columns to rows.
Thanks
SELECT deptno, stragg ('-' || ename)
FROM emp_test
GROUP BY deptno;
DEPTNO STRAGG_STR
10 -CLARK-KING-MILLER
20 -SMITH-FORD-ADAMS-SCOTT-JONES
30 -ALLEN-BLAKE-MARTIN-TURNER-JAMES-WARD
3 rows selected.Edited by: Salim Chelabi on 2010-01-29 08:32
Hi again,
*1- I created my function in my schema.*
CREATE OR REPLACE TYPE t_string_agg AS OBJECT
g_string VARCHAR2(32767),
STATIC FUNCTION ODCIAggregateInitialize(sctx IN OUT t_string_agg)
RETURN NUMBER,
MEMBER FUNCTION ODCIAggregateIterate(self IN OUT t_string_agg,
value IN VARCHAR2 )
RETURN NUMBER,
MEMBER FUNCTION ODCIAggregateTerminate(self IN t_string_agg,
returnValue OUT VARCHAR2,
flags IN NUMBER)
RETURN NUMBER,
MEMBER FUNCTION ODCIAggregateMerge(self IN OUT t_string_agg,
ctx2 IN t_string_agg)
RETURN NUMBER
SHOW ERRORS
CREATE OR REPLACE TYPE BODY t_string_agg IS
STATIC FUNCTION ODCIAggregateInitialize(sctx IN OUT t_string_agg)
RETURN NUMBER IS
BEGIN
sctx := t_string_agg(NULL);
RETURN ODCIConst.Success;
END;
MEMBER FUNCTION ODCIAggregateIterate(self IN OUT t_string_agg,
value IN VARCHAR2 )
RETURN NUMBER IS
BEGIN
SELF.g_string := self.g_string || ',' || value;
RETURN ODCIConst.Success;
END;
MEMBER FUNCTION ODCIAggregateTerminate(self IN t_string_agg,
returnValue OUT VARCHAR2,
flags IN NUMBER)
RETURN NUMBER IS
BEGIN
returnValue := RTRIM(LTRIM(SELF.g_string, ','), ',');
RETURN ODCIConst.Success;
END;
MEMBER FUNCTION ODCIAggregateMerge(self IN OUT t_string_agg,
ctx2 IN t_string_agg)
RETURN NUMBER IS
BEGIN
SELF.g_string := SELF.g_string || ',' || ctx2.g_string;
RETURN ODCIConst.Success;
END;
END;
SHOW ERRORS
CREATE OR REPLACE FUNCTION string_agg (p_input VARCHAR2)
RETURN VARCHAR2
PARALLEL_ENABLE AGGREGATE USING t_string_agg;
SHOW ERRORS
*2- I ran my query in my schema with sqlplus.*
SELECT deptno,ename,sal, string_agg(ename)over(partition by deptno) AS employees
FROM emp_test
order by deptno;
DEPTNO ENAME SAL EMPLOYEES
10 CLARK 2450 CLARK,KING,MILLER
10 KING 5000 CLARK,KING,MILLER
10 MILLER 1300 CLARK,KING,MILLER
20 JONES 2975 JONES,FORD,ADAMS,SMITH,SCOTT
20 FORD 3000 JONES,FORD,ADAMS,SMITH,SCOTT
20 ADAMS 1100 JONES,FORD,ADAMS,SMITH,SCOTT
20 SMITH 800 JONES,FORD,ADAMS,SMITH,SCOTT
20 SCOTT 3000 JONES,FORD,ADAMS,SMITH,SCOTT
30 WARD 1250 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN
30 TURNER 1500 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN
30 ALLEN 1600 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN
30 JAMES 950 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN
30 BLAKE 2850 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN
30 MARTIN 1250 WARD,TURNER,ALLEN,JAMES,BLAKE,MARTIN
14 rows selected.
*3- I import this function in discoverer administration*
4- My problem :When i use the function string_agg(ename)over(partition by deptno) in discover deskto i got the error you can't use over in this place.
Any ideas.
Thank in advance.
Regards Salim.
Similar Messages
-
Hi All,
I need help in building view which actually can show columns data as row.
e.g.
row is as follows
Name Age Salary
ABC 25 10000
BBC 28 12000
The above tables data I want to get as
Name ABC BBC
Age 25 28
Salary 10000 12000
Thanks in advance.Even if I don't really understand such requirement, I wrote some times ago such function to play around that :
Re: Converting Columns into rows
Nicolas. -
Equivalent of to_date function in Ms SQL and using it in Evaluate function
Hi,
I am trying to find out a function in MS SQL which is equivalent to to_date function in oracle. Please find below the advanced filter i am trying to use in OBIEE.
Evaluate('to_date(%1,%2)' as date ,SUBSTRING(TIMES.CALENDAR_MONTH_NAME FROM 1 FOR 3)||'-'||cast(TIMES.CALENDAR_YEAR as char(4)),'MON-YYYY')>=Evaluate('to_date(%1,%2)' as date,'@{pv_mth}'||'@{pv_yr}','MON-YYYY') and Evaluate('to_date(%1,%2)' as date ,SUBSTRING(TIMES.CALENDAR_MONTH_NAME FROM 1 FOR 3)||'-'||cast(TIMES.CALENDAR_YEAR as char(4)),'MON-YYYY') <=timestampadd(sql_tsi_month,4,Evaluate('to_date(%1,%2)' as date,'@{pv_mth}'||'@{pv_yr}','MON-YYYY'))
The statement above works fines with oracle DB with to_date function. The same statement throws an error with MS SQL since to_date is not a built in function.
With MS SQL I tried with CAST, not sure how to pass parameters %1 and %2.
Please help me how to use Evaluate function and passing parameters along with to_date funtion in MS SQL.
Regards!
RRHi,
please refer to this thread for useful information on using to_char and to_date functions of oracle in MS SQL server:
http://database.ittoolbox.com/groups/technical-functional/sql-server-l/how-to-write-to-to_char-and-to_date-sql-server-351831
Hope this helps.
Thanks,
-Amith. -
How to convert columns into rows using transpose function
Hi
anybody tell me how to convert columns values into rows using transpose function.Since BluShadow went to all the trouble to put it together, someone should use it.
See the post titled How do I convert rows to columns? here SQL and PL/SQL FAQ
John -
Need help with query for converting columns to rows
Hello,
I know this is a very common question asked in the forum. I have searched regading this, i did find some threads, but i was not able to achieve what i require from the answers posted. So anybody please help me.
I have a table which is having multiple columns as follows:
Insert into table_1 (X,Y,Z,A,B,C,D,E,F,G,H,I) values (0,0,2,0,0,1,3,0,0,0,0,0);I want to convert the result into a two column, multiple rows i.e., I want the result as follows:
Col1 Col2
X 0
Y 0
Z 2
A 0
B 0
C 1
D 3
E 0
F 0
G 0
H 0
I 0Please anybody help me in writing the query for this..Is this what you are expecting:
SQL> WITH T AS
2 (
3 SELECT 0 X, 0 Y, 2 Z, 0 A, 0 B, 1 C, 3 D, 0 E, 0 F, 0 G, 0 H, 0 I FROM DUAL
4 )
5 SELECT 'X' col1, X col2 FROM T
6 UNION ALL
7 SELECT 'Y' col1, Y col2 FROM T
8 UNION ALL
9 SELECT 'Z' col1, Z col2 FROM T
10 UNION ALL
11 SELECT 'A' col1, A col2 FROM T
12 UNION ALL
13 SELECT 'B' col1, B col2 FROM T
14 UNION ALL
15 SELECT 'C' col1, C col2 FROM T
16 UNION ALL
17 SELECT 'D' col1, D col2 FROM T
18 UNION ALL
19 SELECT 'E' col1, E col2 FROM T
20 UNION ALL
21 SELECT 'F' col1, F col2 FROM T
22 UNION ALL
23 SELECT 'G' col1, G col2 FROM T
24 UNION ALL
25 SELECT 'H' col1, H col2 FROM T
26 UNION ALL
27 SELECT 'I' col1, I col2 FROM T
28 /
C COL2
X 0
Y 0
Z 2
A 0
B 0
C 1
D 3
E 0
F 0
G 0
H 0
C COL2
I 0
12 rows selected. -
How to convert column to row in 10g and calculate the count
876602 wrote:
Hi ,
i need to convert the column to row in my DB 10g , i cant use the Decode method because i have about 2000 items in MDN column
this is sample of my date ,
MDN Date
5C4CA98EABA3 20111205235240
5C4CA98EABA3 20110925121833
5C4CA98EABB0 20111025103700
5C4CA98EABB0 20111124103700
5C4CA98EABB5 20111030175717
5C4CA98EABB8 20110925142653
5C4CA98EABB8 20111126175853i need the result to be ,
MDN Date count
5C4CA98EABA3 20111205235240 ; 20110925121833 2
5C4CA98EABB0 20111025103700 ; 20111124103700 2
5C4CA98EABB5 20111030175717 1
5C4CA98EABB8 20110925142653 ; 20111126175853 2any help please ,
Edited by: 876602 on 15/12/2011 01:33 صSQL> with t as
2 (
3 select '5C4CA98EABA3' MDN ,'20111205235240' Dte from dual
4 union all
5 select '5C4CA98EABA3','20110925121833' from dual
6 union all
7 select '5C4CA98EABB0','20111025103700' from dual
8 union all
9 select '5C4CA98EABB0','20111124103700' from dual
10 union all
11 select '5C4CA98EABB5','20111030175717' from dual
12 union all
13 select '5C4CA98EABB8','20110925142653' from dual
14 union all
15 select '5C4CA98EABB8','20111126175853' from dual
16 )
17 select mdn,ltrim(sys_connect_by_path(dte,';'),';') s,rw as "count"
18 from
19 (
20 select mdn,dte,row_number() over(partition by mdn order by mdn) rw
21 from t
22 )
23 where connect_by_isleaf = 1
24 start with rw = 1
25 connect by prior rw = rw-1
26 and prior mdn = mdn
27 ;
MDN S count
5C4CA98EABA3 20111205235240;20110925121833 2
5C4CA98EABB0 20111025103700;20111124103700 2
5C4CA98EABB5 20111030175717 1
5C4CA98EABB8 20110925142653;20111126175853 2 -
How to convert column to row in 10g
Hi ,
i need to convert the column to row in my DB 10g , i cant use the Decode method because i have about 2000 items in MDN column
this is sample of my date ,
MDN Date
5C4CA98EABA3 20111205235240
5C4CA98EABA3 20110925121833
5C4CA98EABB0 20111025103700
5C4CA98EABB0 20111124103700
5C4CA98EABB5 20111030175717
5C4CA98EABB8 20110925142653
5C4CA98EABB8 20111126175853i need the result to be ,
MDN Date
5C4CA98EABA3 20111205235240 ; 20110925121833
5C4CA98EABB0 20111025103700 ; 20111124103700
5C4CA98EABB5 20111030175717
5C4CA98EABB8 20110925142653 ; 20111126175853any help please ,
Edited by: 876602 on 15/12/2011 01:33 صNote the name of this forum is "SQL Developer *(Not for general SQL/PLSQL questions)*", so only for issues with the SQL Developer tool. Please post these questions under the dedicated {forum:id=75} forum.
Regards,
K. -
How to convert columns to rows
I have 70 columns and I need to convert them into rows. Please help!
Currently, it is showing as listed below
message 1 message 2 message 3 message 4 message 5 .......... message 70
system 1 20 10 40 60 100
system 2 40 30 50 80 110
system 3 60 60 70 90 120
The desire output
system 1 system 2 system 3
message 1 20 40 60
message 2 10 30 60
message 3 40 50 70
message 70Something like...
SQL> ed
Wrote file afiedt.buf
1 select decode(rn,1,'Empno :'||empno
2 ,2,'Ename ('||empno||') :'||ename
3 ,3,'Job ('||empno||') :'||job
4 ) as col
5 from emp
6 cross join (select rownum rn from dual connect by rownum <= 3)
7* order by empno, rn
SQL> /
COL
Empno :7369
Ename (7369) :SMITH
Job (7369) :CLERK
Empno :7499
Ename (7499) :ALLEN
Job (7499) :SALESMAN
Empno :7521
Ename (7521) :WARD
Job (7521) :SALESMAN
Empno :7566
Ename (7566) :JONES
Job (7566) :MANAGER
Empno :7654
Ename (7654) :MARTIN
Job (7654) :SALESMAN
Empno :7698
Ename (7698) :BLAKE
Job (7698) :MANAGER
Empno :7782
Ename (7782) :CLARK
Job (7782) :MANAGER
Empno :7788
Ename (7788) :SCOTT
Job (7788) :ANALYST
Empno :7839
Ename (7839) :KING
Job (7839) :PRESIDENT
Empno :7844
Ename (7844) :TURNER
Job (7844) :SALESMAN
Empno :7876
Ename (7876) :ADAMS
Job (7876) :CLERK
Empno :7900
Ename (7900) :JAMES
Job (7900) :CLERK
Empno :7902
Ename (7902) :FORD
Job (7902) :ANALYST
Empno :7934
Ename (7934) :MILLER
Job (7934) :CLERK
42 rows selected. -
Reformatting Table- convert columns to rows
Hi,
I have data tables that I would like to reformat so that the columns become rows and vice versa, if that is possible.
I've been keyword searching for hours, but unfortunately I am unfamiliar with the terminology... Sorry if this is really stupid; I'd appreciate any help! Thanks!Bev Kehoe wrote:
After calling Apple Tech support & having the tech head tell me 'transpose' only works on charts, we found you!
One more techie which doen't know what he respond about. The transpose function is not as efficient as we where hoping but it works.
Alas, the formulas are so weird that I thougt that the "transpose_live script is a better tool.
I was not able to find a 'menu' script within numbers but opened it on its own and it worked. Is there a way to have it be a choice within numbers itself?
The "Script menu" is a menu of its own.
If you apply what I described at the beginning of the script, you will have an area dedicated to Numbers.
We may organize the menu two ways:
Yvan KOENIG (from FRANCE lundi 29 juin 2009 16:57:49) -
Hi,
I have 6 columns and 4 rows and i want to convert it into 6 rows and 4 columns and want to display in table control.
plz help me in this ....Hi Kinjal,
Question is not clear. You want to change the internal table values with 6 columns & 4 rows to 4 columns & 6 rows?
If my understanding is correct, check the bleow logic:
loop at itab1.
case sy-tabix.
when 1.
itab2-c1 = itab1-c1.
append itab2.
itab2-c1 = itab1-c2.
append itab2.
itab2-c1 = itab1-c3.
append itab2.
itab2-c1 = itab1-c4.
append itab2.
itab2-c1 = itab1-c5.
append itab2.
itab2-c1 = itab1-c6.
append itab2.
when 2.
itab2-c2 = itab1-c1.
modify itab2 transporting c2 index 1.
itab2-c2 = itab1-c2.
modify itab2 transporting c2 index 2.
itab2-c2 = itab1-c3.
modify itab2 transporting c2 index 3.
itab2-c2 = itab1-c4.
modify itab2 transporting c2 index 4.
itab2-c2 = itab1-c5.
modify itab2 transporting c2 index 5.
itab2-c2 = itab1-c6.
modify itab2 transporting c2 index 6.
when 3.
itab2-c3 = itab1-c1.
modify itab2 transporting c3 index 1.
when 4.
itab2-c4 = itab1-c1.
modify itab2 transporting c4 index 1.
Endloop.
Finally you will get 6 rows with 4 coulmns. -
Dear all....I need to convert all columns into rows in a table. For example table has following columns:
Emp_Cod........Val1......Val2......Val3
1 a b c
Now I wish that each column should display as a value like:
Emp_Cod........Val1
1 a
1 b
1 c
Now the one way to solve this job is to write a union statement for each column but for this I'll have to write equal number of select statements as there are columns.
What I need that is there anyway to write minimum code for this job, is there any alternate way???SQL> with t as(select 1 emp_code, 'a' val1, 'b' val2, 'c' val3 from dual)
2 select*from t unpivot(v for c in(val1,val2,val3));
EMP_CODE C V
1 VAL1 a
1 VAL2 b
1 VAL3 c
SQL> col COLUMN_VALUE for a20
SQL> with t as(select 1 emp_code, 'a' val1, 'b' val2, 'c' val3 from dual)
2 select*from t,table(sys.odcivarchar2list(val1,val2,val3));
EMP_CODE V V V COLUMN_VALUE
1 a b c a
1 a b c b
1 a b c c -
Hello,
I am trying to build and SQL to convert columns from multiple rows to the all rows - see below test data and result expected:
CREATE TABLE XX_TEST(NAME VARCHAR2(10),A1 VARCHAR2(10),A2 VARCHAR2(10), A3 VARCHAR2(10),A4 VARCHAR2(10),A5 VARCHAR2(10));
INSERT INTO XX_TEST VALUES('LIST','A','B','C','D','E');
INSERT INTO XX_TEST VALUES('L1','1',NULL,'3',NULL,NULL);
INSERT INTO XX_TEST VALUES('L2','1','5','4',NULL,NULL);
COMMIT
SELECT * FROM XX_TEST;
Result expected:
NAME is Column from table XX_TEST but COLUMN and VALUE are the columns converted to rows-
NAME COLUMN VALUE
L1 A1 1
L1 A2 NULL
L1 A3 3
L1 A4 NULL
L1 A5 NULL
L2 A1 1
L2 A2 5
L2 A3 4
L2 A4 NULL
L2 A5 NULL
Thanks
BSHi,
Thanks for posting the sample data in such a useful form!
Whenever you post a question, you should always say what version of Oracle you're using, too.
Displaying multiple columns from one row as one column on multiple rows is called Unpivoting .
In Oracle 11, you can use the SELECT ... UNPIVOT feature to do that.
In any version of Oracle, you can cross-join your table to a Counter that has as many rows as your original table has columns to be unpivoted.
In this problem, we need a self-join of the unpivoted data, to join the rows with name='LIST' to every other row.
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 5
, unpivoted_xx_test AS
SELECT x.name
, 'A' || c.n AS col
, CASE c.n
WHEN 1 THEN x.a1
WHEN 2 THEN x.a2
WHEN 3 THEN x.a3
WHEN 4 THEN x.a4
WHEN 5 THEN x.a5
END AS val
FROM cntr c
CROSS JOIN xx_test x
SELECT u.name
, u.col
, l.val AS val1
, u.val AS val2
FROM unpivoted_xx_test l
JOIN unpivoted_xx_test u ON l.col = u.col
WHERE l.name = 'LIST'
AND u.name != 'LIST'
ORDER BY name
, col
;Output:
NAME COL VAL1 VAL2
L1 A1 A 1
L1 A2 B
L1 A3 C 3
L1 A4 D
L1 A5 E
L2 A1 A 1
L2 A2 B 5
L2 A3 C 4
L2 A4 D
L2 A5 EThe query above uses some features that were new in Oracle 9, but the basic strategy will work in earlier versions.
If your columns don't have such regular names (A1, A2, A3, ...) then you can use another CASE expression to derive unpivoted_xx_test.col. -
Equivalent of DB2 functions in ORACLE 11g
Hi,
I am trying to convert the SQL queries written in DB2 to ORACLE. There are some db2 specific functions are used in the queries.I am not able to find the equivalent function in ORACLE. The function names are written below:
1) DateDD()
2) SELECT @@IDENTITY
3) SELECT *
FROM (
SELECT ROWNUMBER() OVER() AS rowId_1, INNER_TABLE.*
FROM (----)as innertable
) AS Outertable
Error is: ROWNUMBER is INVALID identifier.
4) DAYOFWEEK()
5) DAYS()
6) dayofyear()
Please help me in finding the equivalent function in ORACLE.
Thanks in advance!!You probably don't need a DateAdd function in Oracle. You can add a number to a date in Oracle-- that adds the number of days to the date.
SELECT sysdate today, sysdate+1 tomorrow
FROM dualWhy are you using DAYS()? If, as in the example, you're just trying to determine the number of days between two dates, you can subtract dates in Oracle and the difference will be a number of days (including a fractional component if applicable)
SELECT date '2011-09-27' - date '2011-09-25' difference_in_days
FROM dualIf you really need the number of days since January 1, 0001, you could subtract the date from Jan 1, 0001, i.e.
SELECT date '2011-09-27' - date '0001-01-01'
FROM dualI would assume that Oracle and DB2 would return the same number but there could well be some differences since the current calendar didn't exist in the year 1 and I know there were issues in the transition from the Gregorian to the Julian calendar where some days were decreed not to exist. It wouldn't shock me if Oracle and DB2 counted some of the days in the 1500's differently.
Justin -
Recursive functions in Oracle SQL.
OK,
Here is the pivot problem. Only this time were on an Oracle 11.1.0.7.0.
I've created a table called:
CREATE TABLE STAGING.MY_TAB_COLS
TABLE_NAME VARCHAR2(30 BYTE),
COLUMN_NAME VARCHAR2(30 BYTE)
)And I've put the following data into it:
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'SSN_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'FIRST_NAME_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'LAST_NAME_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'DOB_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'EMP_SSN_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'EMP_FIRST_NAME_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'EMP_LAST_NAME_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'EMP_DOB_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'MPI');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'SSN_STANDARDIZED');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'FIRST_NAME_A_STANDARDIZED');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'FIRST_NAME_B_STANDARDIZED');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'LAST_NAME_A_STANDARDIZED');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'LAST_NAME_B_STANDARDIZED');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'EMP_SSN_STANDARDIZED');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'EMP_FIRST_NAME_A_STANDARDIZED');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'EMP_FIRST_NAME_B_STANDARDIZED');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'EMP_LAST_NAME_A_STANDARDIZED');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAEADP1', 'EMP_LAST_NAME_B_STANDARDIZED');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'MPI_OLD');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'EMPLOYEE_SSN_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'EMPLOYEE_LAST_NAME_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'EMPLOYEE_FIRST_NAME_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'EMPLOYEE_DOB');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'PATIENT_SSN');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'PATIENT_LAST_NAME_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'PATIENT_FIRST_NAME_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'PATIENT_DOB');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'EMPLOYEE_NON_SSN_ID');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'EMPLOYEE_SSN_STANDARDIZED');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'PATIENT_FIRST_NAME_A_STD');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'PATIENT_FIRST_NAME_B_STD');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'PATIENT_LAST_NAME_A_STD');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'PATIENT_LAST_NAME_B_STD');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'EMPLOYEE_FIRST_NAME_A_STD');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'EMPLOYEE_FIRST_NAME_B_STD');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'EMPLOYEE_LAST_NAME_A_STD');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'EMPLOYEE_LAST_NAME_B_STD');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'PATIENT_SSN_STANDARDIZED');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAMAET1', 'MPI');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'EMPLOYEE_SSN');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'EMPLOYEE_FIRST_NAME');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'EMPLOYEE_LAST_NAME');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'EMPLOYEE_DATE_OF_BIRTH');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'PATIENT_SSN');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'PATIENT_FIRST_NAME_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'PATIENT_LAST_NAME_SOURCE');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'PATIENT_DOB');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'PRESCRIBER_LAST_NAME');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'MPI');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'PATIENT_FIRST_NAME_A_STD');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'PATIENT_FIRST_NAME_B_STD');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'PATIENT_LAST_NAME_A_STD');
Insert into STAGING.MY_TAB_COLS
(TABLE_NAME, COLUMN_NAME)
Values
('AAPMED1', 'PATIENT_LAST_NAME_B_STD');My task for this problem is pretty straight forward.
Using the ALL_TAB_COLS table I want to list out certain fields for each table name specified.
All the tables have fields that are similar. I just want these table names to go across the top.
And the field names to list out under each table.
So you would have something like:
AAEADP1 AAMAET1 AAPMED1
SSN_SOURCE MPI_OLD EMPLOYEE_SSN
FIRST_NAME_SOURCE EMPLOYEE_SSN_SOURCE EMPLOYEE_FIRST_NAME
LAST_NAME_SOURCE EMPLOYEE_LAST_NAME_SOURCE EMPLOYEE_LAST_NAME
DOB_SOURCE EMPLOYEE_FIRST_NAME_SOURCE EMPLOYEE_DATE_OF_BIRTH
EMP_SSN_SOURCE EMPLOYEE_DOB PATIENT_SSN
EMP_FIRST_NAME_SOURCE PATIENT_SSN PATIENT_FIRST_NAME_SOURCE
EMP_LAST_NAME_SOURCE PATIENT_LAST_NAME_SOURCE PATIENT_LAST_NAME_SOURCE
EMP_DOB_SOURCE PATIENT_FIRST_NAME_SOURCE PATIENT_DOB
MPI PATIENT_DOB PRESCRIBER_LAST_NAME
SSN_STANDARDIZED EMPLOYEE_NON_SSN_ID MPI
FIRST_NAME_A_STANDARDIZED EMPLOYEE_SSN_STANDARDIZED PATIENT_FIRST_NAME_A_STD
FIRST_NAME_B_STANDARDIZED PATIENT_FIRST_NAME_A_STD PATIENT_FIRST_NAME_B_STD
LAST_NAME_A_STANDARDIZED PATIENT_FIRST_NAME_B_STD PATIENT_LAST_NAME_A_STD
LAST_NAME_B_STANDARDIZED PATIENT_LAST_NAME_A_STD PATIENT_LAST_NAME_B_STD
EMP_SSN_STANDARDIZED PATIENT_LAST_NAME_B_STD
EMP_FIRST_NAME_A_STANDARDIZED EMPLOYEE_FIRST_NAME_A_STD
EMP_FIRST_NAME_B_STANDARDIZED EMPLOYEE_FIRST_NAME_B_STD
EMP_LAST_NAME_A_STANDARDIZED EMPLOYEE_LAST_NAME_A_STD
EMP_LAST_NAME_B_STANDARDIZED EMPLOYEE_LAST_NAME_B_STD
PATIENT_SSN_STANDARDIZED
MPI Where each table name lists across with all of their column names under each table.
The query that I have so far is The following:
SELECT T1.TABLE_NAME, T1.COLUMN_NAME
FROM my_tab_cols t1, ( SELECT DISTINCT UPPER (SOURCE_TABLE) TABLE_NAME
FROM MPI_DEMOGRAPHICS_TEST
ORDER BY UPPER (SOURCE_TABLE)) tn
WHERE T1.TABLE_NAME = tn.TABLE_NAME
AND ( T1.column_name LIKE '%MPI%'
OR T1.column_name LIKE '%SSN%'
OR T1.column_name LIKE '%E%SSN%'
OR T1.column_name LIKE '%R%SSN%'
OR T1.column_name LIKE '%P%SSN%'
OR T1.column_name LIKE '%BIRTH%'
OR T1.column_name LIKE '%DOB%'
OR T1.column_name LIKE '%FIRST%NAME%'
OR T1.column_name LIKE '%LAST%NAME%'
OR T1.column_name LIKE '%CLIENT%NAME%'
OR T1.column_name LIKE '%SOURCE_TABLE%'
OR T1.column_name LIKE '%TABLE%')
ORDER BY T1.TABLE_NAME, t1.column_id; It just list everything straight down.
Where the MPI_DEMOGRAPHIES_TEST table feeds the relevant table names to the ALL_TAB_COLS table.
It can just be substituted with a list of the values, AAEADP1, AAMAET1, AAPMED1.
Is there a 'pivot' function in Oracle 11.1 that I can use to list these column names out sideways as opposed to what I did before?
Thanks,Hi,
Here's one way to get results like that using the Oracle 11 SELECT ... PIVOT feature:
WITH got_nums AS
SELECT column_name
, ROW_NUMBER () OVER ( PARTITION BY table_name
ORDER BY column_name
) AS r_num
, DENSE_RANK () OVER ( ORDER BY table_name) AS c_num
FROM my_tab_cols
WHERE column_name LIKE '%MPI%'
OR column_name LIKE '%SSN%'
-- OR column_name LIKE '%E%SSN%' -- Included in '%SSN% above
-- OR column_name LIKE '%R%SSN%' -- Included in '%SSN% above
-- OR column_name LIKE '%P%SSN%' -- Included in '%SSN% above
OR column_name LIKE '%BIRTH%'
OR column_name LIKE '%DOB%'
OR column_name LIKE '%FIRST%NAME%'
OR column_name LIKE '%LAST%NAME%'
OR column_name LIKE '%CLIENT%NAME%'
-- OR column_name LIKE '%SOURCE_TABLE%' -- Included in %TABLE% below
OR column_name LIKE '%TABLE%'
UNION
SELECT table_name AS colum_name
, 0 AS r_num
, DENSE_RANK () OVER ( ORDER BY table_name) AS c_num
FROM my_tab_cols
SELECT *
FROM got_nums
PIVOT ( MIN (column_name)
FOR c_num IN ( 1 AS table_1
, 2 AS table_2
, 3 AS table_3
ORDER BY r_num
;Output:
R_NUM TABLE_1 TABLE_2 TABLE_3
0 AAEADP1 AAMAET1 AAPMED1
1 DOB_SOURCE EMPLOYEE_DOB EMPLOYEE_DATE_OF_BIRTH
2 EMP_DOB_SOURCE EMPLOYEE_FIRST_NAME_A_STD EMPLOYEE_FIRST_NAME
3 EMP_FIRST_NAME_A_STANDARDIZED EMPLOYEE_FIRST_NAME_B_STD EMPLOYEE_LAST_NAME
4 EMP_FIRST_NAME_B_STANDARDIZED EMPLOYEE_FIRST_NAME_SOURCE EMPLOYEE_SSN
5 EMP_FIRST_NAME_SOURCE EMPLOYEE_LAST_NAME_A_STD MPI
6 EMP_LAST_NAME_A_STANDARDIZED EMPLOYEE_LAST_NAME_B_STD PATIENT_DOB
7 EMP_LAST_NAME_B_STANDARDIZED EMPLOYEE_LAST_NAME_SOURCE PATIENT_FIRST_NAME_A_STD
8 EMP_LAST_NAME_SOURCE EMPLOYEE_NON_SSN_ID PATIENT_FIRST_NAME_B_STD
9 EMP_SSN_SOURCE EMPLOYEE_SSN_SOURCE PATIENT_FIRST_NAME_SOURCE
10 EMP_SSN_STANDARDIZED EMPLOYEE_SSN_STANDARDIZED PATIENT_LAST_NAME_A_STD
11 FIRST_NAME_A_STANDARDIZED MPI PATIENT_LAST_NAME_B_STD
12 FIRST_NAME_B_STANDARDIZED MPI_OLD PATIENT_LAST_NAME_SOURCE
13 LAST_NAME_A_STANDARDIZED PATIENT_DOB PATIENT_SSN
14 LAST_NAME_B_STANDARDIZED PATIENT_FIRST_NAME_A_STD PRESCRIBER_LAST_NAME
15 LAST_NAME_SOURCE PATIENT_FIRST_NAME_B_STD
16 MPI PATIENT_FIRST_NAME_SOURCE
17 SSN_SOURCE PATIENT_LAST_NAME_A_STD
18 SSN_STANDARDIZED PATIENT_LAST_NAME_B_STD
19 PATIENT_LAST_NAME_SOURCE
20 PATIENT_SSN
21 PATIENT_SSN_STANDARDIZEDIf you don't want to see the r_num column, use your front-end to hide it (e.g., in SQL*Plus: "COLUMN r_num NOPRINT"), or do the pivot in a sub-query, and only select table_1, table_2 and table_3 in the main query.
As with all pivots, you have to hard-code an upper bound to the number of pivoted columns. I used 3 above. You could use 4, or 5, or 45, but you must specify exactly how many columns to display.
If you say there will be 3 tables, and there actually are more than 3, then the query will still run, but all the tables after the first 3 will be ignored.
If you say there will be 3 tables, and there actually are fewer, then the query will still run, but you will have NULL columns at the end of each output row.
This sorts the output alphabetically by table_name and column_name. You can sort by anything you want by changing the analytic ORDER BY clauses. For example, if you are using all_tabl_columns and you want the columns to appear in order by column_id, then you would say:
, ROW_NUMBER () OVER ( PARTITION BY table_name
ORDER BY column_id -- instead of column_name
) AS r_num -
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.
Maybe you are looking for
-
My second internal hard drive disappeared
My second internal hard drive disappeared a couple of days ago taking many gigs of iTunes with it. It does not appear on the desktop, finder or anywhere else. When I click on a song in iTunes I get "The song "Cowell: Set Of Five - 4" could not be use
-
I am trying to run a program called XMLCopyEditor and I keep getting this in the terminal: *** glibc detected *** xmlcopyeditor: double free or corruption (out): 0x086646e8 *** ======= Backtrace: ========= /lib/libc.so.6(+0x6b6c1)[0xb67186c1] /lib/li
-
Hi, how to combine or merge the 1D array of digital waveform with a simple digital waveform. As givien in the attached VI, I want to display the datalines graph & control lines graph in a single digital waveform graph. Thanks in advance Solved! Go t
-
HT4245 Downloaded mountain lion primarily to use iMessage.
Cannot activate iMessage with my Apple ID and password. I know the ID and password are correct but keep getting error message "The server encountered an error processing registration. Please try again. Have tried deleting and reinstating iCloud and e
-
Edit item in list doesn't allow save changes (save disabled on a custom form)
Hi folks I created a custom form in InfoPath. The form works fine when adding a new item. It adds everything entered into the fields, and you can see it in the list. When I click on the one of the existing entries, it shows OK. I click "edit item" an