473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with MySQL Group function (I think)

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
Aug 25 '08 #1
4 1536
coolsti
310 100+
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.
Aug 25 '08 #2
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
Aug 25 '08 #3
Atli
5,058 Expert 4TB
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?
Aug 25 '08 #4
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!
Aug 26 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Karzy | last post by:
I'm having trouble coming up with what I would assume is a rather simple mysql query about retrieving records that haven't been updated after a certain time. Does anyone know of a mysql group...
3
by: moon | last post by:
Hi all, Im trying to execute this statement in SQL Plus but am getting the error not a single-group group function. My code is below. To explain what Im trying to get at, I want to return the...
1
by: windandwaves | last post by:
Hi Folk can someone point me in the direction for a general mysql group? TIA - Nicolaas
1
by: windandwaves | last post by:
Hi Folk Some of you may be interested in a function that allows you to add/substract from a date in PHP, using a MySql Dateformat (e.g. 2005-10-31) (NOT TESTED): Any comments appreciated. ...
5
by: Jim | last post by:
Need help with aggregate function...for each unique product, I need the provider with the cheapest cost factor Here't the table (Table1) ID product provider factor 1 123456 abc...
3
by: auron | last post by:
Hi there, I have a really stupid and banal problem with showing the results of a MySQL query in PHP, preciselly with MySQL count() function that gives to a variable in PHP the result. NOTE:...
1
by: Hollywood_Jack | last post by:
Peeps, I'm getting an "ORA-00937: not a single group group function error" when I try and run the following query. Any help is appreciated: SELECT tbt.major_cov_cd||', '|| tbt.claim_id||', '||...
7
by: tom_b | last post by:
Hi, I have some php scripts to use with a mysql database. I've tested them locally on my computer and they work fine, but when I put them on line I get the message "Invalid use of group...
8
by: tom_b | last post by:
Hi, I'm not really sure if this is a PHP or Mysql issue, but I need some help!! I have some php scripts to use with a mysql database. I've tested everything locally on my computer and they...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.