MySQL Forums
Forum List  »  Stored Procedures

Re: Pivot Table Rows into Columns stored procedure
Posted by: Devart Team
Date: August 03, 2010 03:55AM

Try this one -

INSERT INTO t2
SELECT
  Product,
  SUM(CASE WHEN Measures = 'dollar_Sales' THEN value END) AS Dollar_Sales,
  SUM(CASE WHEN Measures = 'avg_sales' THEN value END) AS Avg_sales,
  SUM(CASE WHEN Measures = 'Base_Price' THEN value END) AS Base_Price
FROM
  T1
GROUP BY
  Product;

SELECT * FROM t2;
+----------+--------------+-----------+------------+
| Product  | Dollar_Sales | Avg_Sales | Base_Price |
+----------+--------------+-----------+------------+
| product1 |           15 |        20 |          2 |
| product2 |           25 |      NULL |       NULL |
+----------+--------------+-----------+------------+

Devart Company,
MySQL management tools
http://www.devart.com/dbforge/mysql/

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: Pivot Table Rows into Columns stored procedure
2694
August 03, 2010 03:55AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.