SQL获得多年的平均销售成本

时间:2016-08-24 14:38:44

标签: mysql

我有一个sql查询,它将返回特定年份的给定房产类型的平均售价。

SELECT 
            `table`.Property AS 'Property',
            `table`.Year AS 'Year',
            AVG(`table`.`Value`) AS 'Average Sold Price'
            FROM
            `table`
            WHERE `table`.`Area` LIKE '%NW1%'
            AND `table`.`Property` LIKE '%F%'
            AND `table`.`Year` = '2011'
            GROUP BY `table`.Property,`table`.Year
            ORDER BY
            `table`.Property ASC

输出看起来像这样

| Property | Year | Average Sold Price
|    F     | 2011 | 440242.18137204903

查看2016年

| Property | Year | Average Sold Price
|    F     | 2016 | 702453.9544754727

- 我如何改变查询以获得合并输出 - 所以像这样,尝试减少调用次数等。

| Property | Average Sold Price (2011) | Average Sold Price (2016)
|    F     | 440242.18137204903        | 702453.9544754727

1 个答案:

答案 0 :(得分:0)

SELECT 
            `table`.Property AS 'Property',
            `table`.Year AS 'Year',
            AVG(`table`.`Value`) AS 'Average Sold Price'
            FROM `table`
            WHERE `table`.`Area` LIKE '%NW1%'
            AND `table`.`Property` LIKE '%F%'
            AND `table`.`Year` IN(2011,2016)
            GROUP BY `table`.Property,`table`.Year
            ORDER BY `table`.Property ASC

^这产生了可用的输出。

| Property | Year | Average Sold Price
|    F     | 2011 | 440242.18137204903
|    F     | 2016 | 702453.9544754727