0% found this document useful (0 votes)
8 views

Converting Rows To Columns - SQL Query

Uploaded by

RaJu Bhai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Converting Rows To Columns - SQL Query

Uploaded by

RaJu Bhai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

11/29/2018 Converting Rows to Columns - Sql Query

Home Data Warehouse Informatica Informatica Scenarios Informatica Cloud Oracle Unix Hadoop

Search... Search
Converting Rows to Columns - Sql Query
Lets see the conversion of rows to columns with an example. Suppose we have a products table
Popular Posts
which looks like Informatica Scenario Based Interview Questions with
Answers - Part 1

Table: products
Unix Sed Command to Delete Lines in File - 15 Examples

product_id product_name String Functions in Hive

1 AAA
Top Examples of Awk Command in Unix
1 BBB
Sed Command in Unix and Linux Examples
1 CCC
2 PPP Design/Implement/Create SCD Type 2 Effective Date
Mapping in Informatica
2 QQQ
2 RRR Date Functions in Hive

Now we want to convert the data in the products table as SQL Queries Interview Questions - Oracle Part 1

Top Unix Interview Questions - Part 1


product_id prodcut_name_1 prodcut_name_2 prodcut_name_3
1 AAA BBB CCC Update Strategy Transformation in Informatica

2 PPP QQQ RRR


Have Questions? Follow Me
The following query converts the rows to columns:

SELECT product_id,
MAX(DECODE(pivot,1,product_name,NULL))
product_name_1,

https://www.folkstalk.com/2010/04/converting-rows-to-columns.html 1/3
11/29/2018 Converting Rows to Columns - Sql Query

MAX(DECODE(pivot,2,product_name,NULL)) vijay bhaskar


product_name_2, Add to circles
MAX(DECODE(pivot,3,product_name,NULL))
product_name_3
FROM
(
SELECT product_id,
product_name,
row_number() over (partition by product_id order by product_name) pivot
FROM products
) a
GROUP BY product_id;

Also check how to convert columns to rows.


994 have me in circles View all

No comments:

Post a Comment

Enter your comment...

Comment as: Google Accoun

Publish Preview

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

https://www.folkstalk.com/2010/04/converting-rows-to-columns.html 2/3
11/29/2018 Converting Rows to Columns - Sql Query

https://www.folkstalk.com/2010/04/converting-rows-to-columns.html 3/3

You might also like