dongwei6457 2014-08-23 12:26
浏览 110
已采纳

从数据库中选择数据并在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.

  • 写回答

1条回答 默认 最新

  • dousou1878 2014-08-23 12:33
    关注

    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
    

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?