简体   繁体   中英

Selecting data from database and creating new columns in MYSQL

I have a Table with three columns: countrycode , year and values . Year consists of a number of years ( 2000, 2001 and 2002) . I need to select data for a particular year and display it as a new column.

    SELECT g.countrycode, g.values AS '2000'
      FROM `gainfinal` g 
     WHERE `year` = '2000' 

I used the query above which returned two columns year and 2000, with column '2000' containing the values for year 2000. Now I need to select data from other year 2001 and 2002 and display it in the similar ways. How can I create two more columns for two more years.

You would use conditional aggregation. Combine aggregate functions with CASE statements.

Here is an example for summing up your values for years between 2000 and 2003.

SELECT  g.countrycode,
        sum(case when `year` = '2000' then g.values else 0 end) AS "2000",
        sum(case when `year` = '2001' then g.values else 0 end) AS "2001",
        sum(case when `year` = '2002' then g.values else 0 end) AS "2002",
        sum(case when `year` = '2003' then g.values else 0 end) AS "2003"
FROM    `gainfinal` g
WHERE   `year` between '2000' and '2003'
group by g.countrycode

Your sample data does not contain any rows where the year is 2002 or 2003. So they should be zeros.

If you want data for 2004, you have to add 2004 to the range in the WHERE clause, and add a CASE statement for it to the SELECT list, like below.

However 2002 and 2003 will still be all zeros, because they are zeros.

SELECT  g.countrycode,
        sum(case when `year` = '2000' then g.values else 0 end) AS "2000",
        sum(case when `year` = '2001' then g.values else 0 end) AS "2001",
        sum(case when `year` = '2002' then g.values else 0 end) AS "2002",
        sum(case when `year` = '2003' then g.values else 0 end) AS "2003",
        sum(case when `year` = '2004' then g.values else 0 end) AS "2004"
FROM    `gainfinal` g
WHERE   `year` between '2000' and '2004'
group by g.countrycode

You do have to specify the range of years, and have one case statement in the select list for every year that you want output for. There is no purely sql solution for the columns to be dynamically determined. For that you would probably be better served just making a pivot table in Excel. The above assumes that you know what years you want to show data for ahead of time.

So if you wanted 1995 to 2012, you would have to have a CASE statement for every year, like so:

SELECT  g.countrycode,
        sum(case when `year` = '1995' then g.values else 0 end) AS "1995",
        sum(case when `year` = '1996' then g.values else 0 end) AS "1996",
        sum(case when `year` = '1997' then g.values else 0 end) AS "1997",
        sum(case when `year` = '1998' then g.values else 0 end) AS "1998",
        sum(case when `year` = '1999' then g.values else 0 end) AS "1999",
        sum(case when `year` = '2000' then g.values else 0 end) AS "2000",
        sum(case when `year` = '2001' then g.values else 0 end) AS "2001",
        sum(case when `year` = '2002' then g.values else 0 end) AS "2002",
        sum(case when `year` = '2003' then g.values else 0 end) AS "2003",
        sum(case when `year` = '2005' then g.values else 0 end) AS "2004",
        sum(case when `year` = '2006' then g.values else 0 end) AS "2005",
        sum(case when `year` = '2007' then g.values else 0 end) AS "2006",
        sum(case when `year` = '2008' then g.values else 0 end) AS "2007",
        sum(case when `year` = '2009' then g.values else 0 end) AS "2008",
        sum(case when `year` = '2010' then g.values else 0 end) AS "2009",
        sum(case when `year` = '2011' then g.values else 0 end) AS "2010",
        sum(case when `year` = '2012' then g.values else 0 end) AS "2011",
        sum(case when `year` = '2012' then g.values else 0 end) AS "2012"
FROM    `gainfinal` g
WHERE   `year` between '1995' and '2012'
group by g.countrycode

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM