[Solved] Oracle SQL query rows to columns

EverSQL Database Performance Knowledge Base

Oracle SQL query rows to columns

Database type:

table

i have the above data in a table and what im trying to achieve is the following

DESIRED RESULT

ITMCOD    BUOMNUM     BUOMDEN   BUOMNUM_CS
80240277  10000        4444        200

Sorry im not able to format this as a table for some reason. MNy current sql is only able to return the results shown in the image.

Im only interested in the rows where UOM is CS or SU, but then the BUOMNUM field contains two entries which i need to have in separate columns. One for where UOM is CS and the other for where UOM is SU.

Whats the sql code to do this please? My colleague suggested a PIVOT function but i cant get that to work. Im using Oracle 11g.

cheers

EDIT:

Here is my SQL that produces the list of products that im trying to query further to produce the desired result shown above:

SELECT * 
FROM   ( 
              SELECT     m.itmcod, 
                         m.itmgrp, 
                         m.itmdsc, 
                         u.buomden, 
                         u.buomnum, 
                         u.uom AS unit 
              FROM       itmmst m 
              inner join item_uom u 
              ON         m.itmcod = u.itmcod 
              WHERE      u.uom IN ('CS','SU') 
    ) 
        PIVOT (count(unit) FOR unit IN ('CS' cs,'SU' su) )

This code works, what im struggling with is separating the CS and SU to separate columns as explained above.

cheers

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Subqueries (query line: 4): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
CREATE INDEX item_uom_idx_uom ON item_uom (uom);
CREATE INDEX itmmst_idx_itmcod ON itmmst (itmcod);
The optimized query:
SELECT
        * 
    FROM
        (SELECT
            m.itmcod,
            m.itmgrp,
            m.itmdsc,
            u.buomden,
            u.buomnum,
            u.uom AS unit 
        FROM
            itmmst m 
        INNER JOIN
            item_uom u 
                ON m.itmcod = u.itmcod 
        WHERE
            u.uom IN (
                'CS', 'SU'
            )) PIVOT (count(unit) FOR unit IN ('CS' cs,
        'SU' su))

Related Articles



* original question posted on StackOverflow here.