Home > MySQL > MySQL Transpose Row Into Column

MySQL Transpose Row Into Column

Image representing MySQL as depicted in CrunchBase

Image via CrunchBase

This article explain how to transpose a MySQL table from row to column. Suppose that you have a sales table per item per retail outlet per sales date like this:

Original Sales Table

Then, you need to show the total sales per item per retail outlet like the following:

The Transposed Sales Data

For that case, you need to transpose the original table into the second table. Unfortunately, MySQL does not support a function for transposing row into column of a table, so we need to create a query to do so.

For the example above, we need a query like the following:

SELECT ITEM_CODE,
SUM(IF(RETAIL_ID=’A',SALES,0)) AS ‘A’,
SUM(IF(RETAIL_ID=’B',SALES,0)) AS ‘B’,
SUM(IF(RETAIL_ID=’C',SALES,0)) AS ‘C’
FROM RETAIL_INFO
GROUP BY ITEM_CODE

The query above will sum the total sales for each retail outlet ID grouped by item code and shows the result on it’s own column. If you have many retail outlets, then you must create the column for each of them.

Technorati Tags: ,,

Akhmad Daniel Sembiring

vITraining.com – Qualified IT Products, Outsourcing, and Services
Ligarwangi.com – Linux, E-book, Coffee, Gift, etc

 

Reblog this post [with Zemanta]

  • Share/Bookmark
Categories: MySQL Tags: , ,
  1. July 10th, 2009 at 01:49 | #1

    Nice query pak.
    Siiip…

  2. August 14th, 2009 at 19:12 | #2

    This is a very static query which will only work if you always have a fixed retail_id. Anytime you have a new one you need to change it. Next if retail_id was not a retail but e.g. a date it is even unusable.

  3. Stas Zh
    November 17th, 2009 at 23:06 | #3

    had the same issue but with date – transposing date column into rows

    we will create dynamic sql request (with lets say last ten days), and then loop through those days adding new column.

    DELIMITER $$

    DROP PROCEDURE IF EXISTS `a`.`days`$$
    CREATE PROCEDURE `a`.`days` ()
    BEGIN

    DECLARE l_sql VARCHAR(4000);
    DECLARE l_date VARCHAR(10);

    # select for dates (2009-11-01, 2009-11-02, etc)
    DECLARE cur1 CURSOR FOR
    SELECT DISTINCT(d.name) as `date`
    FROM `date` d
    LIMIT 10;

    OPEN cur1;
    #start your query
    SET l_sql=”SELECT kn.name “;
    BEGIN
    DECLARE EXIT HANDLER FOR NOT FOUND BEGIN END;
    LOOP
    FETCH cur1 INTO l_date;
    # dynamic add to l_sql as many times as many dates you have in cur1
    SET l_sql=CONCAT(l_sql,”, SUM(IF(d.name=’”,l_date,”‘,value,0)) AS ‘”,l_date,”‘ “);
    END LOOP;
    END;

    CLOSE cur1;

    #finish l_sql
    SET l_sql=CONCAT(l_sql,” FROM samples GROUP BY something_id”);

    #uncomment to see generated query
    #SELECT l_sql;

    #now prepare query and execute
    SET @sql=l_sql;
    PREPARE s1 FROM @sql;
    EXECUTE s1;
    DEALLOCATE PREPARE s1;

    END;
    $$
    DELIMITER

  4. thiyagi
    February 12th, 2011 at 01:12 | #4

    thanks guys, that was really useful..

  5. thiyagi
    February 22nd, 2011 at 04:26 | #5

    thanks guys…

  6. patrick paul rah
    May 19th, 2011 at 19:19 | #6

    thank you its really helpful for me and saved my life thank you once more and god bless you for all your needs in your life

    T/R
    patrick

  7. July 6th, 2011 at 21:06 | #7

    Thanks for this article.

    Tukeran back link yah :D

  1. February 9th, 2011 at 03:04 | #1
This site uses a Hackadelic PlugIn, Hackadelic SEO Table Of Contents 1.6.0.