MySQL - How to SELECT based on value of another SELECT

前端 未结 3 1362
庸人自扰
庸人自扰 2021-01-30 06:22

I have a table that looks something like this:

Name    Year   Value
 A      2000     5
 A      2001     3
 A      2002     7
 A      2003     1
 B      2000              


        
3条回答
  •  梦如初夏
    2021-01-30 07:02

    SELECT x.name, x.summary, (x.summary / COUNT(*)) as percents_of_total
    FROM tbl t
    INNER JOIN 
    (SELECT name, SUM(value) as summary
    FROM tbl
    WHERE year BETWEEN 2000 AND 2001
    GROUP BY name) x ON x.name = t.name
    GROUP BY x.name, x.summary
    

提交回复
热议问题