sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
luttkens's Avatar

Help with MySQL Group function (I think)


Question posted by: luttkens (Newbie) on August 25th, 2008 12:44 PM
I have a table salary, and when I select it, it looks like this

Expand|Select|Wrap|Line Numbers
  1. Name          Year   Income
  2. -------------------------------
  3. James Smith   2008   33000
  4. James Smith   2007   32000
  5. James Smith   2006   31300
  6. James Smith   2005   30100
  7. James Smith   2004   28000
  8. Oliver M.     2008   19700
  9. Oliver M.     2006   18000
  10. Oliver M.     2005   16000


How can I make the result look like this instead? I guess there some kind of group function included in the answer...

Expand|Select|Wrap|Line Numbers
  1. Name          2008  2007  2006  2005  2004  
  2. ---------------------------------------------
  3. James Smith   33000 32000 31300 30100 28000
  4. Oliver M.     19700 null  18000 16000 null
4 Answers Posted
coolsti's Avatar
coolsti August 25th, 2008 03:09 PM
Needs Regular Fix - 304 Posts
#2: Re: Help with MySQL Group function (I think)

You are asking MySQL to transform rows into columns. I suppose you could do this with a query but it would be a little complicated, and probably needs to know in advance that the columns you wish to show are for the 5 years that you are showing. In other words, it would maybe not be generalized to another set of years.

Usually, to do a presentation like this, you would take the results out of MySQL in a table format like what you have now, and use a scripting or other language to place the results into the column form that you want.

What may help you here is the group_concat function with a group by clause.
moltendorf's Avatar
moltendorf August 25th, 2008 08:15 PM
Newbie - 30 Posts
#3: Re: Help with MySQL Group function (I think)

It's a fairly complicated query, but here is how you would do it.

Simply Replace `helper` with the name of your table.

Expand|Select|Wrap|Line Numbers
  1. SET @y1 = YEAR(CURDATE());
  2. SET @y2 = (YEAR(CURDATE()) - (1));
  3. SET @y3 = (YEAR(CURDATE()) - (2));
  4. SET @y4 = (YEAR(CURDATE()) - (3));
  5. SET @y5 = (YEAR(CURDATE()) - (4));
  6.  
  7. CREATE TEMPORARY TABLE `temporary`
  8.     (
  9.         `name` VARCHAR(255) NOT NULL,
  10.         `1` BIGINT(12) UNSIGNED,
  11.         `2` BIGINT(12) UNSIGNED,
  12.         `3` BIGINT(12) UNSIGNED,
  13.         `4` BIGINT(12) UNSIGNED,
  14.         `5` BIGINT(12) UNSIGNED
  15.     )
  16.     ENGINE = MEMORY;
  17.  
  18. INSERT INTO `temporary`
  19.     (
  20.         `name`
  21.     )
  22.     SELECT DISTINCT `name`
  23.         FROM `helper`;
  24.  
  25. UPDATE `temporary`
  26.     SET
  27.         `temporary`.`1` =
  28.             (
  29.                 SELECT `helper`.`income`
  30.                     FROM `helper`
  31.                     WHERE 
  32.                             `helper`.`year` = @y1
  33.                         AND `helper`.`name` = `temporary`.`name`
  34.             ),
  35.         `temporary`.`2` =
  36.             (
  37.                 SELECT `helper`.`income`
  38.                     FROM `helper`
  39.                     WHERE 
  40.                             `helper`.`year` = @y2
  41.                         AND `helper`.`name` = `temporary`.`name`
  42.             ),
  43.         `temporary`.`3` =
  44.             (
  45.                 SELECT `helper`.`income`
  46.                     FROM `helper`
  47.                     WHERE 
  48.                             `helper`.`year` = @y3
  49.                         AND `helper`.`name` = `temporary`.`name`
  50.             ),
  51.         `temporary`.`4` =
  52.             (
  53.                 SELECT `helper`.`income`
  54.                     FROM `helper`
  55.                     WHERE 
  56.                             `helper`.`year` = @y4
  57.                         AND `helper`.`name` = `temporary`.`name`
  58.             ),
  59.         `temporary`.`5` =
  60.             (
  61.                 SELECT `helper`.`income`
  62.                     FROM `helper`
  63.                     WHERE 
  64.                             `helper`.`year` = @y5
  65.                         AND `helper`.`name` = `temporary`.`name`
  66.             );
  67.  
  68. SELECT * FROM `temporary`;


Please note that this query only selects results from the past 5 years.
To do more then that, you'd have to adapt it a bit.

I have no clue to make it into a dynamic form as of right now.
-You'd probably need some advanced loops to be used.

The result table is:
Expand|Select|Wrap|Line Numbers
  1. name        1     2     3     4     5
  2. -----------------------------------------
  3. James Smith 33000 32000 31300 30100 28000
  4. Oliver M.   19700 NULL  18000 16000 NULL
Atli's Avatar
Atli August 25th, 2008 11:05 PM
Moderator - 2,760 Posts
#4: Re: Help with MySQL Group function (I think)

This can be done with MySQL (like shown in the previous post), but I wouldn't advice it.
You would be far better of using some scripting language or some other API to format the data.

MySQL is a database server, meant to store data, not re-arrange it in pretty formations. That is the job of an API.

How would you be executing this query?
Is this meant to be a part of some application?
Would it not be possible to do this using the language in which that application is written?
luttkens's Avatar
luttkens August 26th, 2008 09:05 PM
Newbie - 16 Posts
#5: Re: Help with MySQL Group function (I think)

Quote:
Originally Posted by Atli
This can be done with MySQL (like shown in the previous post), but I wouldn't advice it.
You would be far better of using some scripting language or some other API to format the data.

MySQL is a database server, meant to store data, not re-arrange it in pretty formations. That is the job of an API.

How would you be executing this query?
Is this meant to be a part of some application?
Would it not be possible to do this using the language in which that application is written?


Thanks for your answers!

Well I need it to be dynamic so I used the group_concat function and combined it with a PHP-script.

It works very nice!
Reply
Not the answer you were looking for? Post your question . . .
197,039 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,039 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top MySQL Contributors