Skip to main content

Transposing Column into Rows

There might be cases when we would like to aggregate the data into a single row for the set of records for a particular value, for example say i have following data in my table


FILTERID               CRITERIA           
---------------------- --------------------
28900                  CRIT1              
28901                  CRIT1              
28902                  CRIT1              
28903                  CRIT1              
28904                  CRIT2              
28905                  CRIT2              
28906                  CRIT2              
28907                  CRIT3              
28908                  CRIT3  


I would like to display it as :


CRITERIA             FILTERIDS
------------------------------------
CRIT1                28900,28901,28902,28903
CRIT2                28904,28905,28906
CRIT3                28907,28908    

You can do it using the following ways :

1. WM_CONCAT Built-in Function
2. Using analytic function with SYS_CONNECT_BY_PATH


Creating sample data

create table test_data
(
filterid number,
criteria varchar2(20),
val    number
);

insert into test_data values (28900,'CRIT1',1);
insert into test_data values (28901,'CRIT1',2);
insert into test_data values (28902,'CRIT1',3);
insert into test_data values (28903,'CRIT1',4);
insert into test_data values (28904,'CRIT2',1);
insert into test_data values (28905,'CRIT2',2);
insert into test_data values (28906,'CRIT2',3);
insert into test_data values (28907,'CRIT3',1);
insert into test_data values (28908,'CRIT3',2);
commit;

1. Using WM_CONCAT function .

select criteria,wm_concat(filterid) FILTERIDS from test_data   group by criteria

2. Using analytic function with SYS_CONNECT_BY_PATH


SELECT     criteria
        , SUBSTR(MAX (SYS_CONNECT_BY_PATH (filterid, ',')),2)
                 filterid_final
      FROM (SELECT criteria, filterid
                , RANK () OVER (PARTITION BY criteria ORDER BY filterid) rn
             FROM test_data)
START WITH rn = 1
CONNECT BY PRIOR rn = rn - 1 AND PRIOR criteria = criteria
  GROUP BY criteria
  ORDER BY criteria

Comments

Popular posts from this blog

Check Whether File Exists on the Server Using PLSQL

There are different packages using which you can check whether a file exists on the database / application server. The different ways are 1. Using UTL_HTTP from Web Server 2. Using UTL_FILE from Database Server 3. Using DBMS_LOB from Database Server 1. You can also use UTL_HTTP package to check whether a file exists on a web server from the database. SET SERVEROUTPUT ON DECLARE   url       VARCHAR2(256) := 'http://www.oracle.com/index1.html';   username  VARCHAR2(256);   password  VARCHAR2(256);   req       UTL_HTTP.REQ;   resp      UTL_HTTP.RESP; BEGIN   req := UTL_HTTP.BEGIN_REQUEST(url);   IF (username IS NOT NULL) THEN     UTL_HTTP.SET_AUTHENTICATION(req, username, password);   END IF;   resp := UTL_HTTP.GET_RESPONSE(req);   DBMS_OUTPUT.PUT_LINE('response -->' || resp.status_code); END; / If the f...

11.2 PLSQL New Feature : How to use DBMS_PARALLEL_EXECUTE to Update Large Tables in Parallel

The DBMS_PARALLEL_EXECUTE package enables you to incrementally update the data in a large table in parallel, in two high-level steps: 1. Group sets of rows in the table into smaller chunks. 2. Apply the desired UPDATE statement to the chunks in parallel, committing each time you have finished processing a chunk. This technique is recommended whenever you are updating a lot of data. Its advantages are: o You lock only one set of rows at a time, for a relatively short time, instead of locking the entire table. o You do not lose work that has been done if something fails before the entire operation finishes. o You reduce rollback space consumption. o You improve performance Different Ways to Spilt Workload : 1. CREATE_CHUNKS_BY_NUMBER_COL: Chunks the table associated with the given task by the specified column. 2. CREATE_CHUNKS_BY_ROWID : Chunks the table associated with the given task by ROWID 3. CREATE_CHUNKS_BY_SQL : Chunks the table associated with the given task by me...

PRAGMA AUTONOMOUS_TRANSACTION with Transaction Isolation

What is PRAGMA ? PRAGMA is a language construct that specifies how a compiler (or assembler or interpreter) should process its input. PRAGMA keyword in PLSQL is used to direct compiler to work differently when compared to the normal code. Pragmas are processed at compile time, not at run time. What is AUTONOMOUS_TRANSACTION ? The AUTONOMOUS_TRANSACTION pragma changes the way a subprogram works within a transaction. A subprogram marked with this pragma can do SQL operations and commit or roll back those operations, without committing or rolling back the data in the main transaction. The statement clearly states that autonomous transaction executes independent of main transaction. The concept was crystal clear to me and I had used it extensively in designing some of the solution. Very recently a question raised in my mind. Will the changes made by autonomous transaction be visible in the main transaction ?  I didn't have an answer and hence tried to do a research. L...