I have a table salary, and when I select it, it looks like this - Name Year Income
-
-------------------------------
-
James Smith 2008 33000
-
James Smith 2007 32000
-
James Smith 2006 31300
-
James Smith 2005 30100
-
James Smith 2004 28000
-
Oliver M. 2008 19700
-
Oliver M. 2006 18000
-
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... - Name 2008 2007 2006 2005 2004
-
---------------------------------------------
-
James Smith 33000 32000 31300 30100 28000
-
Oliver M. 19700 null 18000 16000 null
4 1536
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.
It's a fairly complicated query, but here is how you would do it.
Simply Replace `helper` with the name of your table. - SET @y1 = YEAR(CURDATE());
-
SET @y2 = (YEAR(CURDATE()) - (1));
-
SET @y3 = (YEAR(CURDATE()) - (2));
-
SET @y4 = (YEAR(CURDATE()) - (3));
-
SET @y5 = (YEAR(CURDATE()) - (4));
-
-
CREATE TEMPORARY TABLE `temporary`
-
(
-
`name` VARCHAR(255) NOT NULL,
-
`1` BIGINT(12) UNSIGNED,
-
`2` BIGINT(12) UNSIGNED,
-
`3` BIGINT(12) UNSIGNED,
-
`4` BIGINT(12) UNSIGNED,
-
`5` BIGINT(12) UNSIGNED
-
)
-
ENGINE = MEMORY;
-
-
INSERT INTO `temporary`
-
(
-
`name`
-
)
-
SELECT DISTINCT `name`
-
FROM `helper`;
-
-
UPDATE `temporary`
-
SET
-
`temporary`.`1` =
-
(
-
SELECT `helper`.`income`
-
FROM `helper`
-
WHERE
-
`helper`.`year` = @y1
-
AND `helper`.`name` = `temporary`.`name`
-
),
-
`temporary`.`2` =
-
(
-
SELECT `helper`.`income`
-
FROM `helper`
-
WHERE
-
`helper`.`year` = @y2
-
AND `helper`.`name` = `temporary`.`name`
-
),
-
`temporary`.`3` =
-
(
-
SELECT `helper`.`income`
-
FROM `helper`
-
WHERE
-
`helper`.`year` = @y3
-
AND `helper`.`name` = `temporary`.`name`
-
),
-
`temporary`.`4` =
-
(
-
SELECT `helper`.`income`
-
FROM `helper`
-
WHERE
-
`helper`.`year` = @y4
-
AND `helper`.`name` = `temporary`.`name`
-
),
-
`temporary`.`5` =
-
(
-
SELECT `helper`.`income`
-
FROM `helper`
-
WHERE
-
`helper`.`year` = @y5
-
AND `helper`.`name` = `temporary`.`name`
-
);
-
-
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: - name 1 2 3 4 5
-
-----------------------------------------
-
James Smith 33000 32000 31300 30100 28000
-
Oliver M. 19700 NULL 18000 16000 NULL
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?
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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: windandwaves |
last post by:
Hi Folk
can someone point me in the direction for a general mysql group?
TIA
- Nicolaas
|
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.
...
|
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...
|
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:...
|
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||', '||...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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,...
|
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...
|
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,...
|
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...
| |