SQL分组平均每年

时间:2016-08-24 12:04:59

标签: mysql group-by

我有一个看起来像这样的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   |

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

更新:我在oracle中尝试过它并且有效。所以你尝试下面的东西。

SELECT 
`table`.Property AS 'Property',
AVG(case when Year=2016 then `table`.`Value` end) AS 'Average Sold Price 2016',
AVG(case when Year=2015 then `table`.`Value` end) AS 'Average Sold Price 2015',
AVG(case when Year=2014 then `table`.`Value` end) AS 'Average Sold Price 2014'
FROM
`table`
WHERE `table`.`Area` LIKE '%NW1%'
GROUP BY `table`.Property,`table`.Year
ORDER BY `table`.Property ASC

如果它不起作用,您可以使用下面的替代方法。

然后想法是分别计算average的每个year,然后使用此结果集基于property加入。 Full outer join用于确保我们还为隔离组合添加条目。 coalesce只是为了处理null,如果发生这种情况,则使用其他year

select coalesce(y2014.'Property',y2015.'Property',y2016.'Property') as Property
,y2014.'Average Sold Price'
,y2015.'Average Sold Price'
,y2016.'Average Sold Price' from
    (
    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`.Year=2014
    GROUP BY `table`.Property,`table`.Year
    ) y2014
full outer join
    (
    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`.Year=2015
    GROUP BY `table`.Property,`table`.Year
    ) y2015
on y2014.'Property'=y2015.'Property'
    full outer join
    (
    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`.Year=2016
    GROUP BY `table`.Property,`table`.Year
    ) y2016
on y2015.'Property'=y2016.'Property'

注意:我假设Year是字符串,但如果不是,则使用适当的转换。

答案 2 :(得分:0)

你可以使用sum()函数获取数据,查询将是这样的:

     SELECT 
`table`.Property AS 'Property',
AVG(`table`.`Value`) AS 'Average Sold Price',
AVG(SUM(IF(table.Year=2016,`table`.`Value`,0))) AS 'Average Sold Price 2016',
AVG(SUM(IF(table.Year=2015,`table`.`Value`,0))) AS 'Average Sold Price 2015',
AVG(SUM(IF(table.Year=2014,`table`.`Value`,0))) AS 'Average Sold Price 2014',
FROM
`table`
WHERE `table`.`Area` LIKE '%NW1%'
GROUP BY `table`.Property
ORDER BY `table`.Property ASC;

答案 3 :(得分:0)

此答案可让您获得动态列结果:

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'AVG(IF(Year = ''',Year,
      ''',`Value`,0)) AS AvgSoldPrice', Year)
    )
  ) INTO @sql
from `table`;

SET @sql = CONCAT('SELECT `table`.Property,', @sql, ' WHERE `table`.`Area` LIKE ''%NW1%''
GROUP BY `table`.Property
ORDER BY `table`.Property ASC');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;