我无法做到这一点
+------------+------------+------------+
| Name | year | Value |
+------------+------------+------------+
| Name1 | 2015 | 100 |
| Name1 | 2014 | 150 |
| Name1 | 2013 | 200 |
| Name2 | 2015 | 130 |
| Name2 | 2014 | 160 |
| Name2 | 2013 | 220 |
+------------+------------+------------+
(2015年至2013年)的价值减去(2014年至2013年)的价值 按名称分组
将输出
+------------+------------+
| Name | Value |
+------------+------------+
| Name1 | 100 |
| Name2 | 130 |
+------------+------------+
或者无论如何我可以使用SUM和WHERE类似
(SUM(Name1 WHERE year <= "2014") - SUM(Name1 WHERE year <= "2015") ) AS Value
GROUP BY Name
答案 0 :(得分:2)
你需要像这样的东西
SELECT (SUM(IF(year <= 2015 AND year >= 2013,value,0) )
- SUM(IF(year <= 2014 and year >= 2013,value,0) )) AS total_sum
FROM your_table_name
GROUP BY Name
这样就可以了解
答案 1 :(得分:0)
你可以这样使用
SELECT (SUM(IF(`year` <= 2015 and `year` >= 2013,value,0) ) ) as total_sum_2015 FROM `table`
2014年做同样的事情 像这样
SELECT (
SUM( IF( `year` <=2014
AND `year` >=2013, value, 0 ) )
) AS total_sum_2014
FROM `table`