我有一个看起来像这样的 SQL 查询
SELECT
`table`.Property AS 'Property',
AVG(`table`.`Value`) AS 'Average Sold Price'
FROM
`table`
WHERE
`table`.`Area` LIKE '%NW1%'
GROUP BY
`table`.Property
ORDER BY
`table`.Property ASC
这会返回如下结果:
| Property | Average Sold Price |
| D | 1890895.381275497 |
| F | 528221.9917672797 |
| S | 985241.5226100162 |
| T | 941906.3221196578 |
我将如何调整查询以每年平均一次来创建这样的输出——有一个名为“Year”的列
| Property | Avg Sold Price 2016 | Avg Sold Price 2015 | Avg Sold Price 2014 |
| D | 1890895.381275497 | 1690895.381275497 | 1490895.381275497 |
| F | 528221.9917672797 | 518221.9917672797 | 618221.9917672797 |
| S | 985241.5226100162 | 955241.5226100162 | 755241.5226100162 |
| T | 941906.3221196578 | 911901.3221196578 | 781901.3221196578 |
最佳答案
SELECT
`table`.Property AS 'Property',
`table`.Year AS 'Year',
AVG(`table`.`Value`) AS 'Average Sold Price'
FROM
`table`
WHERE `table`.`Area` LIKE '%NW1%'
GROUP BY `table`.Property,`table`.Year
ORDER BY `table`.Property ASC
关于mysql - 每年的 SQL 分组平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39122841/