Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In Oracle, there are different ways to make multiple lines horizontal. One way is to use the PIVOT function, which rotates rows into columns. Here's a procedure using the PIVOT function:

  1. Use the SELECT statement to gather the data needed for the result set.
  2. Use the PIVOT function with the required column set and the aggregation function to convert values from multiple rows into horizontal columns.
  3. Alias the resulting columns as needed to make them easier to understand and use.
  4. Use the WHERE clause to filter the data if necessary.
  5. Order the result set using the ORDER BY clause if needed.

For example, suppose you have a table named "sales" with columns "region," "product," and "sales_amount." To pivot the data and make the regions horizontal, you can use the following query:

SELECT * FROM ( SELECT region, product, salesamount FROM sales ) PIVOT ( SUM(salesamount) FOR region IN ('North' AS "North", 'South' AS "South", 'East' AS "East", 'West' AS "West") ) ORDER BY product;

This query selects the columns needed for the result set, pivots the rows to columns using the PIVOT function, and aliases the resulting columns with the corresponding region name. Finally, the result set is sorted by product.